0

Having the following dataset:

+-----+-----+-----+
|col1 |col2 |score|
+-----+-----+-----+
|"A"  |"B"  |6    |
+-----+-----+-----+
|"A"  |"B"  |4    |
+-----+-----+-----+
|"C"  |"D"  |4    |
+-----+-----+-----+
|"C"  |"D"  |3    |
+-----+-----+-----+

I'm grouping by col1 and col2 and getting the mean like so()

df.groupby(['col1','col2']).mean()

Now I want to distribute the mean result to each of the group rows and replace the score coliumn, to get:

+-----+-----+-----+
|col1 |col2 |mean |
+-----+-----+-----+
|"A"  |"B"  |5    |
+-----+-----+-----+
|"A"  |"B"  |5    |
+-----+-----+-----+
|"C"  |"D"  |3.5  |
+-----+-----+-----+
|"C"  |"D"  |3.5  |
+-----+-----+-----+

How can I do it?

Shlomi Schwartz
  • 8,912
  • 29
  • 101
  • 169
  • 1
    `df.groupby(['col1','col2']).transform('mean')` instead – yatu Apr 21 '20 at 14:42
  • related : [How do I create a new column from the output of pandas groupby.sum](https://stackoverflow.com/questions/30244952/how-do-i-create-a-new-column-from-the-output-of-pandas-groupby-sum) – anky Apr 21 '20 at 14:43

0 Answers0