1、使用DataFrame.iterrows遍历
相关文档:DataFrame.iterrows
import pandas as pddf = pd.DataFrame({'c1': [10, 11, 12], 'c2': [100, 110, 120]})for index, row in df.iterrows(): print(row['c1'], row['c2'])
输出:
10 100
11 110
12 120
2、使用iloc[]实现
import pandas as pddf = pd.DataFrame({'A':[1, 2, 3], 'B':[4, 5, 6], 'C':[7, 8, 9]})print(df)for i in range(df.shape[0]): print(df.iloc[i, 1]) print(df.iloc[i, [0, 2]])
3、使用DataFrame.itertuples()遍历
import pandas as pddf = pd.DataFrame({'A':[1, 2, 3], 'B':[4, 5, 6], 'C':[7, 8, 9]})print(df)for row in df.itertuples(index=True, name='Pandas'): print(row.c1, row.c2)
4、使用DataFrame.apply()遍历
相关文档:DataFrame.apply()
import pandas as pddef print_row(row): print row['c1'], row['c2']df.apply(lambda row: print_row(row), axis=1)