AttributeError:“系列”对象没有属性“ iterrows”

时间:2019-03-04 20:25:20

标签: python-3.x pandas loops

accounts = pd.read_csv('C:/*******/New_export.txt', sep=",", dtype={'number': object})
accounts.columns = ["Number", "F"]

for i, j in accounts["Number"].iterrows(): #i represents the row(index number), j is the number
    if (str(j) == "27*******5"):
        print(accounts["F"][i], accounts["Number"][i])

我收到以下错误:

  

AttributeError:“系列”对象没有属性“ iterrows”

我不太理解该错误,因为“帐户”是一个熊猫数据框。请协助。

1 个答案:

答案 0 :(得分:3)

accounts["Number"]是一个 Series 对象,而不是DataFrame。遍历accounts.iterrows()并从每一行中提取Number列,或使用Series.iteritems() method

遍历数据框:

for i, row in accounts.iterrows():
    if str(row['Number']) == "27*******5":
        print(row["F"], row["Number"])

或超过Series.iteritems()

for i, number in accounts['Number'].iteritems():
    if str(number) == "27*******5":
        print(accounts["F"][i], number)