Normalize DataFrame by group

后端 未结 4 621
梦谈多话
梦谈多话 2021-02-07 02:04

Let\'s say that I have some data generated as follows:

N = 20
m = 3
data = np.random.normal(size=(N,m)) + np.random.normal(size=(N,m))**3

and t

4条回答
  •  南方客
    南方客 (楼主)
    2021-02-07 02:39

    Although this is not the prettiest solution, you could do something like this:

    indx = df['indx'].copy()
    for indices in df.groupby('indx').groups.values():
        df.loc[indices] -= df.loc[indices].mean()
    df['indx'] = indx
    

提交回复
热议问题