wowee .....如何使用python和pandas的iterrows?如果我进行行迭代,我是否应该无法访问带有['COL_NAME']行的col?
以下是col名称:
print df
Int64Index: 152 entries, 0 to 151
Data columns:
Date 152 non-null values
Time 152 non-null values
Time Zone 152 non-null values
Currency 152 non-null values
Event 152 non-null values
Importance 152 non-null values
Actual 127 non-null values
Forecast 86 non-null values
Previous 132 non-null values
dtypes: object(9)
for row in df.iterrows():
print row['Date']
Traceback (most recent call last):
File "/home/ubuntu/workspace/calandar.py", line 34, in <module>
print row['Date']
TypeError: tuple indices must be integers, not str
如果我打印1行:
(0, Date Sun Apr 13
Time 17:30
Time Zone GMT
Currency USD
Event USD Fed's Stein Speaks on Financial Stability ...
Importance Low
Actual NaN
Forecast NaN
Previous NaN
Name: 0)
答案 0 :(得分:48)
iterrows
为您提供(index, row)
元组而不仅仅是行,因此您应该能够以与您想的相同的方式访问列:
for index, row in df.iterrows():
print row['Date']
答案 1 :(得分:1)
如果要遍历数据库并将函数应用于每一行,您可能还需要考虑应用函数
def print_row(r):
print r['Date']
df.apply(print_row, axis = 1)