Michael Martinez
Michael Martinez

Reputation: 61

Fill in missing rows from columns after groupby in python pandas

I have a dataset that looks something like this but is much larger.

Column A   Column B  Result
1          1         2.4
1          4         2.9
1          1         2.8
2          5         9.3
3          4         1.2

df.groupby(['Column A','Column B'])['result'].mean() 

Column A   Column B  Result
1          1         2.6
           4         2.9
2          5         9.3
3          4         1.2

I want to have a range from 1-10 for Column B with the results for these rows to be the average of Column A and Column B. So this is my desired table:

Column A   Column B  Result
1          1         2.6
           2         2.75
           3         2.75
           4         2.9 
           5         6.025
2          1         5.95
           2         9.3
           3         9.3
...

Hopefully the point is getting across. I know the average thing is pretty confusing so I would settle with just being able to fill in the missing values of my desired range. I appreciate the help!

Upvotes: 3

Views: 2144

Answers (1)

jezrael
jezrael

Reputation: 862591

You need reindex by new index created by MultiIndex.from_product and then groupby by first level Column A with fillna by mean per groups:

df = df.groupby(['Column A','Column B'])['Result'].mean() 
mux = pd.MultiIndex.from_product([df.index.get_level_values(0).unique(),
                                  np.arange(1,10)], names=('Column A','Column B'))
df = df.reindex(mux)
df = df.groupby(level='Column A').apply(lambda x: x.fillna(x.mean()))
print (df)
Column A  Column B
1         1           2.60
          2           2.75
          3           2.75
          4           2.90
          5           2.75
          6           2.75
          7           2.75
          8           2.75
          9           2.75
2         1           9.30
          2           9.30
          3           9.30
          4           9.30
          5           9.30
          6           9.30
          7           9.30
          8           9.30
          9           9.30
3         1           1.20
          2           1.20
          3           1.20
          4           1.20
          5           1.20
          6           1.20
          7           1.20
          8           1.20
          9           1.20
Name: Result, dtype: float64

Upvotes: 3

Related Questions