iterrows cannot iterate over DataFrame Eror: touple object has no attribute “A”

荒凉一梦 提交于 2021-02-05 11:13:50

问题


When I try to iterate over a dataframe, somehow dtype is changed.

dates = pd.date_range('20130101',periods=6)
df = pd.DataFrame(np.random.randn(6,4),index=dates,columns=list('ABCD'))

df 
                A           B           C         D
2013-01-01  -1.328046   -0.545127   -0.033153   1.190336
2013-01-02  -0.549147   0.447161    1.179931    0.397521
2013-01-03  -0.106707   -0.327574   -0.933817   -1.032949
2013-01-04  -0.519988   -1.007374   -0.794482   -1.757222
2013-01-05  -0.739735   1.220599    -1.387994   -0.116178
2013-01-06  0.262876    -0.679471   -0.568768   -0.277880

now when I try to iterate over rows

for row in df.iterrows():
    print (row.A)

I get error

AttributeError: 'tuple' object has no attribute 'A'

my current version of pandas is 0.20.1

Thank you.


回答1:


itterrows creates a tuple, so try this:

for pos, row in df.iterrows():
    print (row.A)


来源:https://stackoverflow.com/questions/44757065/iterrows-cannot-iterate-over-dataframe-eror-touple-object-has-no-attribute-a

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!