转载:https:///article/
- iteritems():按列遍历,将DataFrame的每一列迭代为(列名, Series)对,可以通过row[index]对元素进行访问。
示例数据
1 2 3 4 5 6 |
|
import pandas as pd inp = [{'c1':10, 'c2':100}, {'c1':11, 'c2':110}, {'c1':12, 'c2':123}] df = (inp) print(df)
按行遍历iterrows():
1 2 |
|
for index, row in (): print(index) # 输出每行的索引值
row[‘name']
1 2 3 |
|
# 对于每一行,通过列名name访问对应的元素 for row in (): print(row['c1'], row['c2']) # 输出每一行
按行遍历itertuples():
getattr(row, ‘name')
?
1 2 |
|
for row in (): print(getattr(row, 'c1'), getattr(row, 'c2')) # 输出每一行
按列遍历iteritems():
1 2 |
|
for index, row in (): print(index) # 输出列名
1 2 |
|
for row in (): print(row[0], row[1], row[2]) # 输出各列