英文:
AttributeError: 'Series' object has no attribute 'iterrows' - Python
问题
Below is my code. I do not understand why it has error: AttributeError: 'Series' object has no attribute 'iterrows'.
Could someone advise me on a way to correct it?
a = pd.DataFrame(index=['D1', 'D2', 'D3', 'D4'], columns=[x for x in range(0, 10)])
a.loc[:] = 5
for index, row in a.loc['D1', :].iterrows():
for col in a.columns:
if a.at[index, col] > 0:
print(True)
英文:
Below is my code. I do not understand why it has error: ttributeError: 'Series' object has no attribute 'iterrows'
Could someone advise me on a way to correct it?
a=pd.DataFrame(index=['D1','D2','D3','D4'], columns=[x for x in range(0,10)])
a.loc[:]=5
for index,row in a.loc['D1',].iterrows():
for col in a.columns:
if a.at[index,col]>0:
print(True)
答案1
得分: 1
使用数据框架本身,而不是单独的行:
a=pd.DataFrame(index=['D1','D2','D3','D4'], columns=[x for x in range(0,10)])
a.loc[:]=5
for index,row in a.iterrows():
for col in a.columns:
if a.at[index,col]>0:
print(True)
英文:
Use the dataframe as such, not a single row:
a=pd.DataFrame(index=['D1','D2','D3','D4'], columns=[x for x in range(0,10)])
a.loc[:]=5
for index,row in a.iterrows():
for col in a.columns:
if a.at[index,col]>0:
print(True)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论