use groupby to upsample multiple dataframe columns?

Viewed 47

Is there a way to groupby first by group, and then to upsample education AND income(so that rows are copied in a way that the new dataframe is has equal occurrences / is balanced across education and income)?



df = pd.DataFrame({'group': [1,2,2,1,2,2],
                   'X1':[10,11,30,10,30,8],
                   'X2':[0.2,0.3,0.2,0.2,0.8,0.1], 
                   'education':[2,0,2,2,0,2], 
                   'income':[3,2,6,1,2,1]})

the output would have equal counts of classes (balanced):

   group  X1   X2  education  income  
       1  10  0.2          0       3   
       1  10  0.2          0       3   
       1  10  0.2          0       1   
       1  10  0.2          0       1   
       2  11  0.3          2       2   
       2   8  0.1          2       2   
       2  30  0.2          2       6   
       2  30  0.8          2       6   
0 Answers
Related