python - Setting values in grouped data frame in pandas -


i have 2 data frames grouped 4 separate keys. assign mean of column of 1 group, row values in column in group. understand it, how should done:

g_test.get_group((1, 5, 13, 8)).monthly_sales = \     g_train.get_group((1, 5, 13, 8)).monthly_sales.mean() 

except nothing. values in monthly_sales of group identified in g_test unchanged. can please explain doing wrong , suggest alternatives?

these first few rows of g_train.get_group((1, 5, 13, 8))

    year month day store item units monthly_sales     1      5    5   13     8    4   466     1      5    6   13     8    12  475     1      5    0   13     8    22  469     1      5    5   13     8    26  469     1      5    6   13     8    39  480 

and these first few rows of g_test.get_group((1, 5, 13, 8))

    year month day store item monthly_sales     1      5    1   13     8    0     1      5    2   13     8    0     1      5    3   13     8    0     1      5    4   13     8    0     1      5    5   13     8    0 

only first few rows shown, mean of g_train((1, 5, 13, 8)).monthly_sales 450, want copied on monthly_sales column in g_test.

edit: understand that, code snippet below work:

`df1.loc[(df1.year == 1)      & (df1.month == 5)      & (df1.store == 13)      & (df1.item == 8), 'monthly_sales'] = \ gb2.get_group((1, 5, 13, 8)).monthly_sales.mean()` 

this operation great copying mean once, whole reason split data frame groups avoid these logic checks , multiple times different store , item numbers. there else can do?

you need assign result dataframe, not groupby object. should work:

df1.loc[(df1.year == 1)          & (df1.month == 5)          & (df1.store == 13)          & (df1.item == 8), 'monthly_sales'] = \     gb2.get_group((1, 5, 13, 8)).monthly_sales.mean()  >>> gb1.get_group((1, 5, 13, 8))    year  month  day  store  item  units  monthly_sales 0     1      5    5     13     8      4          471.8 1     1      5    6     13     8     12          471.8 2     1      5    0     13     8     22          471.8 3     1      5    5     13     8     26          471.8 4     1      5    6     13     8     39          471.8 

Comments

Popular posts from this blog

IF statement in MySQL trigger -

c++ - What does MSC in "// appease MSC" comments mean? -

javascript - Blogger related post gadget image Resize s72-c [ Need Expert Help ] -