I have a dataframe like the the one below:
import numpy as np
import pandas as pd
df = pd.DataFrame({"Column1":
["A", "A", "A", "A", "B", "B", "B", "C", "C", "D"],
"Column2": [24., 25, 21., 33, 26, 24., 25, 21., 33, 26],
"Column3": [0.2, 0.3, 0.4, 0.5, 0.6, 0.2, 0.3, 0.4, 0.5, 0.6]})
Column1 Column2 Column3
0 A 24.0 0.2
1 A 25.0 0.3
2 A 21.0 0.4
3 A 33.0 0.5
4 B 26.0 0.6
5 B 24.0 0.2
6 B 25.0 0.3
7 C 21.0 0.4
8 C 33.0 0.5
9 D 26.0 0.6
Here I have 4 rows corresponding to A, 3 rows corresponding B, 2 for C and 1 for D.
How could I add rows for B, C and D to have the same number as A to have 4 rows using their mean values?
For instance mean of B for column2 is (26+25+24)/3 = 25 and for column3 is (0.6+0.2+0.3)/3 = 0.37
so I add one row of B 25 0.37
For C, there are only 2 rows and means for column 2 and 3 are 27 and 0.45
so I add two row of C 27 0.45
For D there is only one row, so we add three rows with the same values
the target is:
Column1 Column2 Column3
0 A 24.0 0.2
1 A 25.0 0.3
2 A 21.0 0.4
3 A 33.0 0.5
4 B 26.0 0.6
5 B 24.0 0.2
6 B 25.0 0.3
7 B 25.0 0.37
8 C 21.0 0.4
9 C 33.0 0.5
10 C 27.0 0.45
11 C 27.0 0.45
12 D 26.0 0.6
13 D 26.0 0.6
14 D 26.0 0.6
15 D 26.0 0.6