pandas——iterrows和iteritems

import pandas as pd
import numpy as np
df1= pd.DataFrame({'第一列':[111,222,333,444,555,666],'第二列':[11,22,33,44,55,66],'第三列':['shu1','shun2','shun3','shun4','shun5','shun6']},index=np.arange(0,6))
#循环index 和对应的行
for row_index,row in df1.iterrows():
    print(row_index)
    print(row)
print("-"*50)
#循环列,和对应的列名
for key ,values in df1.iteritems():
    print(key)
    print(values)
print("-"*50)
#和iterrows功能类似,只不过返回的是元组
for row in df1.itertuples():
    print(row)

输出的结果依次是

0
第一列 111
第二列 11
第三列 shu1
Name: 0, dtype: object
1
第一列 222
第二列 22
第三列 shun2
Name: 1, dtype: object
2
第一列 333
第二列 33
第三列 shun3
Name: 2, dtype: object
3
第一列 444
第二列 44
第三列 shun4
Name: 3, dtype: object
4
第一列 555
第二列 55
第三列 shun5
Name: 4, dtype: object
5
第一列 666
第二列 66
第三列 shun6
Name: 5, dtype: object
--------------------------------------------------
第一列
0 111
1 222
2 333
3 444
4 555
5 666
Name: 第一列, dtype: int64
第二列
0 11
1 22
2 33
3 44
4 55
5 66
Name: 第二列, dtype: int64
第三列
0 shu1
1 shun2
2 shun3
3 shun4
4 shun5
5 shun6
Name: 第三列, dtype: object
--------------------------------------------------
Pandas(Index=0, 第一列=111, 第二列=11, 第三列='shu1')
Pandas(Index=1, 第一列=222, 第二列=22, 第三列='shun2')
Pandas(Index=2, 第一列=333, 第二列=33, 第三列='shun3')
Pandas(Index=3, 第一列=444, 第二列=44, 第三列='shun4')
Pandas(Index=4, 第一列=555, 第二列=55, 第三列='shun5')
Pandas(Index=5, 第一列=666, 第二列=66, 第三列='shun6')

原文地址:https://www.cnblogs.com/shunguo/p/14563531.html