I have a dataframe that looks something like this:
Birthyear | Weight |
---|---|
1992 | 2 |
1993 | 2.2 |
1992 | 3 |
1993 | 2.5 |
1994 | 2.4 |
1993 | 1.8 |
1994 | 2.1 |
Note: This is an example, I have +100k of rows and years
I want to get a new DataFrame in which I have every year and its average weight, mapping automatically every year because it would take a lot of work and code lines to do it manually.
Birthyear | Weight |
---|---|
1992 | 2.5 |
1993 | 2.1 |
1994 | 2.25 |
I've tried mapping, which worked before to use a reference table, but didn't work for this
dfaverage = df
dfaverage['Weight'] = dfaverage['Birthyear'].map(df.set_index('Year')[df['Weight'].mean])
TypeError: unhashable type: 'DataFrame'
Any ideas on how can I make this idea work?
Use a groupby:
dfaverage.groupby('Birthyear')['Weight'].mean().to_frame()