[英]python and pandas - how to access a column using iterrows
wowee.....how to use iterrows with python and pandas? wowee .....如何使用python和pandas的iterrows? If I do a row iteration should I not be able to access a col with row['COL_NAME']? 如果我进行行迭代,我是否应该无法使用行['COL_NAME']访问col?
Here are the col names: 这是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
if I print 1 row: 如果我打印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)
iterrows
gives you (index, row)
tuples rather than just the rows, so you should be able to access the columns in basically the same way you were thinking if you just do: iterrows
为你(index, row)
元组而不仅仅是行,所以你应该能够以与你想的相同的方式访问列:
for index, row in df.iterrows():
print row['Date']
If you want to iterate across your database and apply a function to each row, you might also want to consider the apply function 如果要遍历数据库并将函数应用于每一行,您可能还需要考虑apply函数
def print_row(r):
print r['Date']
df.apply(print_row, axis = 1)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.