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)
I think this may be simpler for certain cases:
groupby = dfm.groupby('variable')
for ix, row in reversed(tuple(groupby)):
...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With