1

image: https://i.stack.imgur.com/nljvJ.png It won't let me upload because I don't have the reputation points yet, but its a screenshot of the dataframe

I have a pandas dataframe which has each many locations (rows) each with the same years (1990 - 2020) and columns and then many attribute columns with data for each year (row). I want to find the mean of all the years of each column for each location.

This is what I'm tryin but it doesn't work.

location_names_list = ['Central Europe and the Baltics','Caribbean small states'...

world_bank is a pandas dataframe.

world_bank = wb.download(indicator=indicators_list, country=countries, start=1990, end=2019)

for location in location_names_list: 
   for column in world_bank:
       mean_variable = world_bank.loc[location, column].mean()
       world_bank.loc[location, column].fillna(mean_variable, inplace=True)
Mattie
  • 11
  • 2
  • Welcome to Stack Overflow. Please read [How to Ask](https://stackoverflow.com/help/how-to-ask), and [do not upload images of code/errors when asking a question.](https://meta.stackoverflow.com/questions/285551/why-not-upload-images-of-code-errors-when-asking-a-question) Please edit your question to include a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) showing samples of your input dataframes and expected output so that we can better understand what you're trying to do. – Paul Dec 29 '21 at 13:46
  • Does this answer your question? [Fill missing values with the most common value in the grouped form](https://stackoverflow.com/questions/65950425/fill-missing-values-with-the-most-common-value-in-the-grouped-form) – Paul Dec 29 '21 at 13:51

1 Answers1

0

Please check Paul's instructions on the comments section on best practices for posting.

Anyway, I understand that you just want a fillna using grouped mean values. Is it?

If so, I believe a groupby + transform + lambda can help you in just one line.

e.g.:

df['Central Europe and the Baltics'] = df.groupby('country')['Central Europe and the Baltics'].transform(lambda x: x.fillna(x.mean()))

So, to iterate over all columns, you can do:

for col in df.columns:
    df[col] = df.groupby('country')[col].transform(lambda x: x.fillna(x.mean()))
Renato Aranha
  • 300
  • 1
  • 10