pandas使用教程:迭代、填充和合并-灵析社区

清晨我上码

pandas迭代

item迭代

items() 通过键值对进行迭代:

Series:(Index,标量值)对

DataFrame:(列,Series)对

team
name
Liver       E
Arry        C
Ack         A
Eorge       C
Oah         D
           ..
Gabriel     C
Austin7     C
Lincoln4    C
Eli         E
Ben         E
Name: team, Length: 100, dtype: object

itemrows迭代

iterrows() 迭代 DataFrame 或 Series 里的每一行数据。这个操作返回一个迭代器,生成索引值及包含每行数据的 Series。

for index,row in df.iterrows():
    print(index)
    print(row)

Liver
team     E
Q1      89
Q2      21
Q3      24
Q4      64
Name: Liver, dtype: object

itertuples迭代

itertuples() 方法返回为 DataFrame 里每行数据生成命名元组的迭代器。该元组的第一个元素是行的索引值,其余的值则是行的值。

for row in df.itertuples():
    print(row)

Output exceeds the size limit. Open the full output data in a text editorPandas(Index='Liver', team='E', Q1=89, Q2=21, Q3=24, Q4=64)
Pandas(Index='Arry', team='C', Q1=36, Q2=37, Q3=37, Q4=57)
Pandas(Index='Ack', team='A', Q1=57, Q2=60, Q3=18, Q4=84)
Pandas(Index='Eorge', team='C', Q1=93, Q2=96, Q3=71, Q4=78)
Pandas(Index='Oah', team='D', Q1=65, Q2=49, Q3=61, Q4=86)
Pandas(Index='Harlie', team='C', Q1=24, Q2=13, Q3=87, Q4=43)


rng = pd.date_range('1/3/2000', periods=8)

ts = pd.Series(np.random.randint(0,100,8), index=rng)

ts2 = ts[[0, 3, 6]]
ts2.reindex(ts.index)

2000-01-03    20.0
2000-01-04     NaN
2000-01-05     NaN
2000-01-06    94.0
2000-01-07     NaN
2000-01-08     NaN
2000-01-09    24.0
2000-01-10     NaN
Freq: D, dtype: float64

先前填充

#先前填充
ts2.reindex(ts.index, method='ffill')

2000-01-03    20
2000-01-04    20
2000-01-05    20
2000-01-06    94
2000-01-07    94
2000-01-08    94
2000-01-09    24
2000-01-10    24
Freq: D, dtype: int32

向后填充

ts2.reindex(ts.index, method='bfill')

2000-01-03    20.0
2000-01-04    94.0
2000-01-05    94.0
2000-01-06    94.0
2000-01-07    24.0
2000-01-08    24.0
2000-01-09    24.0
2000-01-10     NaN
Freq: D, dtype: float64

就近填充

ts2.reindex(ts.index, method='nearest')

2000-01-03    20
2000-01-04    20
2000-01-05    94
2000-01-06    94
2000-01-07    94
2000-01-08    24
2000-01-09    24
2000-01-10    24
Freq: D, dtype: int32

合并merge

结合 concat

Pandas 提供了多种将 Series、DataFrame 对象组合在一起的功能,用索引与关联代数功能的多种设置逻辑可执行连接(join)与合并(merge)操作。

piece = [df[:2],df[5:7],df[95:]]
pd.concat(piece)

team	Q1	Q2	Q3	Q4
name					
Liver	E	89	21	24	64
Arry	C	36	37	37	57
Harlie	C	24	13	87	43
Acob	B	61	95	94	8
Gabriel	C	48	59	87	74
Austin7	C	21	31	30	43
Lincoln4	C	98	93	1	20
Eli	E	11	74	58	91
Ben	E	21	43	41	74

数据透视表(Pivot Tables)


#name为行,team为列
df.pivot(index='name',columns='team',values='Q1')

team	A	B	C	D	E
name					
Aaron	96.0	NaN	NaN	NaN	NaN
Ack	57.0	NaN	NaN	NaN	NaN
Acob	NaN	61.0	NaN	NaN	NaN
Adam	NaN	NaN	90.0	NaN	NaN
Aiden	NaN	NaN	NaN	20.0	NaN
...	...	...	...	...	...
Toby	52.0	NaN	NaN	NaN	NaN
Tommy	NaN	NaN	29.0	NaN	NaN
Tyler	75.0	NaN	NaN	NaN	NaN
William	NaN	NaN	80.0	NaN	NaN
Zachary	NaN	NaN	NaN	NaN	12.0


阅读量:675

点赞量:0

收藏量:0