AttributeError: ‘Series’ 对象没有 ‘iterrows’ 属性 – Python

huangapple go评论82阅读模式
英文:

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)

huangapple
  • 本文由 发表于 2023年4月10日 23:51:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/75978587.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定