Attributeerror: 'Series' Object Has No Attribute 'Iterrows'

Attributeerror: 'Series' Object Has No Attribute 'Iterrows'
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'

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

0

1 Answer

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

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():

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

James H. Sterling
Author

James H. Sterling

James Sterling reports on renewable energy developments, climate policy, ecological conservation, and green tech innovations around the globe.