Home > Blockchain >  DataFrame.at function not working after copying dataframe
DataFrame.at function not working after copying dataframe

Time:02-16

I'm using the .at function to try and save all columns under one header in a list. The file contains entries for country and population.

df = pandas.read_csv("file.csv")
population_list = []
df2 = df[df['country'] == "India"]
for i in range(len(df2)):
   population_list = df2.at[i, 'population']

This is throwing a KeyError. However, the df.at seems to be working fine for the original dataframe. Is .at just not allowed in this case?

CodePudding user response:

IIUC, you don't need to loop over your dataframe to get what you need. Simply use:

population_list = df2["population"].tolist()

If you really want to use the loop (not recommended when unnecessary), note that the index has likely changed after your filter, i.e not consecutive integers. Try:

for i in df2.index:
    population_list.append(df2.at[i, 'population'])

Note: In your code you keep trying to reassign the entire list to one value instead of appending.

CodePudding user response:

In at you pass the index value and the column name.

In the case of the "original" DataFrame all is OK, because probably the index contains consecutive values starting from 0.

But when you run df2 = df[df['country'] == "India"] then df2 contains only a subset of original rows, so the index does not contain consecutive numbers.

One of possible solutions is to run reset_index() on df2. Then the index will again contain consecutive numbers and your code should raise no exception.

Edit

But your code raises other doubts. Remember that at returns a single value, taken from a cell with particular index value and column, not a list.

So maybe it is enough to run:

population_India = df.set_index('country').at['India', 'population']

You don't need any list. You want to find just the popupation of India, a single value.

  • Related