pandas按行遍历Dataframe,iterrows()方法

初始化一个Dataframe

import pandas as pd

List=[['Yes', None, None, None],
 [None, None, None, None],
 ['Yes', None, None, None],
 [None, None, None, None]]

df=pd.DataFrame(List,columns=['A','B','C','D'])

结果:

      A     B     C     D
0   Yes  None  None  None
1  None  None  None  None
2   Yes  None  None  None
3  None  None  None  None

遍历行,输出索引,A列内容:

for index,row in df.iterrows():
    print(index,row['A'])

输出:

0 Yes
1 None
2 Yes
3 None

如果,数据是筛选后,索引不是连续的,

     A     B     C     D
0  Yes  None  None  None
2  Yes  None  None  None
for index,row in temp.iterrows():
    print(index,row['A'])

输出:

0 Yes
2 Yes

 

你可能感兴趣的:(pandas按行遍历Dataframe,iterrows()方法)