Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python pandas how to get the reverse of groupby

I have two data frames. They are the same except for one column. I want to change the column of the second dataframe according to mean values from the first dataframe. For the latter I have to use groupby, but then I don't know how to get a reverse. Below is a minimal example, where in this particular example df_two should end up being the same as df_one. My question is how to get from tmp to df2_new - see the code below.

import pandas as pd


def foo(df1, df2):
    # Group by A
    groupsA_one = dict(list(df1.groupby('A', as_index=False)))
    groupsA_two = dict(list(df2.groupby('A', as_index=False)))

    for key_A in groupsA_one:
        # Group by B
        groupsB_one = dict(list(groupsA_one[key_A].groupby('B', as_index=False)))
        groupsB_two = dict(list(groupsA_two[key_A].groupby('B', as_index=False)))

        for key_B in groupsB_one:
            # Group by C
            tmp = groupsB_two[key_B].groupby('C', as_index=False)['D'].mean()   # Returns DataFrame with NaN
            tmp['D'] = groupsB_one[key_B].groupby('C', as_index=False)['D'].mean()['D']
            print tmp

    df2_new = []        # ???
    return df2_new

if __name__ == '__main__':
    A1 = {'A': [1, 1, 1, 1, 2, 2, 2, 2], 'B': [1, 1, 2, 2, 1, 1, 2, 2],
          'C': [1, 2, 1, 2, 1, 2, 1, 2], 'D': [5, 5, 5, 5, 5, 5, 5, 5]}
    A2 = {'A': [1, 1, 1, 1, 2, 2, 2, 2], 'B': [1, 1, 2, 2, 1, 1, 2, 2],
          'C': [1, 2, 1, 2, 1, 2, 1, 2], 'D': [0, 0, 0, 0, 0, 0, 0, 0]}
    df_one = pd.DataFrame(A1)
    df_two = pd.DataFrame(A2)
    foo(df_one, df_two)
like image 680
user3176500 Avatar asked Sep 13 '25 07:09

user3176500


1 Answers

I think this may be simpler for certain cases:

groupby = dfm.groupby('variable')
for ix, row in reversed(tuple(groupby)):
    ...
like image 103
ariddell Avatar answered Sep 17 '25 19:09

ariddell