Home > other >  How can I get multiple averages from different values in a dataframe column?
How can I get multiple averages from different values in a dataframe column?

Time:09-02

I have a dataframe made up of all sorts of data. However, the two most important columns are a score column and week column:

df = 

    score      week
    6          21-06-2021
    4          21-06-2021
    9          21-06-2021
    8          28-06-2021
    2          28-06-2021
    ...

I want to be able to get the average score for each week in a new column, initially the dataframe started with an exact date column, but I converted it to round to the week instead. Ideally, the output would look like this:

df = 

    score      week          average
    6          21-06-2021    6.33
    4          21-06-2021    6.33
    9          21-06-2021    6.33
    8          28-06-2021    5
    2          28-06-2021    5

CodePudding user response:

Use pd.groupby() for this:

df['average'] = df.groupby('week').score.transform('mean')

Output:

   score        week   average
0      6  21-06-2021  6.333333
1      4  21-06-2021  6.333333
2      9  21-06-2021  6.333333
3      8  28-06-2021  5.000000
4      2  28-06-2021  5.000000

If you want to round it as your desired outcome suggests, you can extend the line with .round(2).

CodePudding user response:

You can use

df.groupby([Col1])[Col2].agg('mean')

this case

df.groupby(['week'])['score'].agg('mean')
  • Related