use*_*481 0 python dataframe pandas
当我尝试遍历数据帧时,dtype有所更改。
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
Run Code Online (Sandbox Code Playgroud)
现在,当我尝试遍历行时
for row in df.iterrows():
print (row.A)
Run Code Online (Sandbox Code Playgroud)
我得到错误
AttributeError: 'tuple' object has no attribute 'A'
Run Code Online (Sandbox Code Playgroud)
我当前的熊猫版本是0.20.1
谢谢。
itterrows创建一个元组,因此请尝试以下操作:
for pos, row in df.iterrows():
print (row.A)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3978 次 |
| 最近记录: |