0

I have the following DataFrame:

df = pd.DataFrame()
df['I'] = [-1.922410e-11, -6.415227e-12,  1.347632e-11,  1.728460e-11,3.787953e-11]
df['V'] = [0,0,0,1,1]
off = df.groupby('V')['I'].mean() 

I need to subtract the off values to the respective df['I'] values. In code I want something like this:

for i in df['V'].unique():
    df['I'][df['V']==i] -= off.loc[i]

I want to know if there is another approach of doing this without using loops.

Ivanovitch
  • 358
  • 1
  • 2
  • 10
  • 1
    use transform here : `df['I'].sub(df.groupby('V')['I'].transform('mean'))` and assign it back – anky Mar 25 '20 at 17:54
  • related : https://stackoverflow.com/questions/30244952/how-do-i-create-a-new-column-from-the-output-of-pandas-groupby-sum – anky Mar 25 '20 at 17:59

0 Answers0