1

Hoping that this is not been asked before. I am trying to groupby two columns and add the mean as a new column.

I get this message, without me being able to check how can I correct it, or if the code is actually correct

main:1: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame. Try using .loc[row_indexer,col_indexer] = value instead

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy

If there is a way to bypass this warning, or to correct it. Please not that the DF is really big (half a million rows). Any help would be nice.

average_stand['mean'] = average_stand.groupby(['branch_id','bill']).transform('mean',numeric_only = False)


    customer_id  branch_id   bill       mean
144    1            1       10           13
145    1            1       16           13
146    1            2       16           16
147    2            2       14           16.5
148    2            2       19           16.5
Quang Hoang
  • 146,074
  • 10
  • 56
  • 74
Protonios
  • 9
  • 1
  • 2
    Your df is subset of others ? try look at https://stackoverflow.com/questions/20625582/how-to-deal-with-settingwithcopywarning-in-pandas – BENY May 09 '19 at 18:27
  • 1
    Agree with @WeNYoBen . wherever you are creating a slice to create this dataframe, try to assign it by calling `.copy()` – anky May 09 '19 at 18:36
  • Indeed, .copy() worked. Thank you both. – Protonios May 10 '19 at 12:57

0 Answers0