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

```none
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:

```none
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

```

Answer: 
Use [pd.groupby()](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.groupby.html) for this:

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

```

Output:

```none
   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)`.
You can use

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

```

this case

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

```