pandas AttributeError: 'Series' 对象没有属性 'iterrows'

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/54991008/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 06:20:10  来源:igfitidea点击:

AttributeError: 'Series' object has no attribute 'iterrows'

python-3.xpandasloops

提问by Kusi

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])

I get the following error:

我收到以下错误:

AttributeError: 'Series' object has no attribute 'iterrows'

AttributeError: 'Series' 对象没有属性 'iterrows'

I don't quite understand the error since "accounts" is a pandas dataframe. Please assist.

我不太明白这个错误,因为“帐户”是一个Pandas数据框。请协助。

回答by Martijn Pieters

accounts["Number"]is a Seriesobject, not a DataFrame. Either iterate over accounts.iterrows()and take the Numbercolumn from each row, or use the Series.iteritems()method.

accounts["Number"]是一个Series对象,而不是一个 DataFrame。要么迭代accounts.iterrows()Number从每一行中取出列,要么使用Series.iteritems()方法

Iterating over the dataframe:

迭代数据帧:

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

or over Series.iteritems():

或结束Series.iteritems()

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