简体   繁体   English

DataFrame以组方式减去意味着

[英]DataFrame subtract group-wise means

I have a DataFrame with columns that can be divided into different groups. 我有一个DataFrame,其列可以分为不同的组。 I need to return a df where the entries are the original values minus the group mean. 我需要返回一个df,其中条目是原始值减去组平均值。
I did the following by using groupby which gives me the group means. 我使用groupby执行以下操作,这给了我组的意思。

base = datetime.today().date()
date_list = [base - timedelta(days=x) for x in range(0, 10)]
df = pd.DataFrame(data=np.random.randint(1, 100, (10, 8)), index=date_list, columns=['a1', 'a2', 'b1', 'a3', 'b2', 'c1' , 'c2', 'b3'])

xx = df.loc[[datetime(2016, 5, 18).date()]]
xx.index = ['group']
xx.a1 = 1
xx.a2 = 1
xx.a3 = 1
xx.b3 = 2
xx.b2 = 2
xx.b1 = 2
xx.c1 = 3
xx.c2 = 3
df = df.append(xx)
dft = df.T
dft.groupby(['group']).mean().T

Update 20/05/16: 2016年5月20日更新:

Aided by unutbu's answer, I come up the following solution as well: 在unutbu的回答的帮助下,我也提出了以下解决方案:

df.T.groupby(group, axis=0).apply(lambda x: x - np.mean(x)).T

If you use the transform method, eg, 如果你使用transform方法,例如,

means = df.groupby(group, axis=1).transform('mean')

then transform will a DataFrame of the same shape as df . 然后transform将与df形状相同的DataFrame。 This makes it easier to subtract means from df . 这使得从df减去means更容易。

You can also pass a sequence, such as group=[1,1,1,2,2,3,3] to df.groupby instead of passing a column name. 您还可以将序列(例如group=[1,1,1,2,2,3,3]传递给df.groupby而不是传递列名。 df.groupby(group, axis=1) will group the columns based on the sequence values. df.groupby(group, axis=1)将根据序列值对列进行分组。 So, for example, to group according to the non-numeric part of each column name, you could use: 因此,例如,要根据每个列名称的非数字部分进行分组,您可以使用:

import numpy as np
import datetime as DT
np.random.seed(2016)
base = DT.date.today()
date_list = [base - DT.timedelta(days=x) for x in range(0, 10)]
df = pd.DataFrame(data=np.random.randint(1, 100, (10, 8)), 
                  index=date_list, 
                  columns=['a1', 'a2', 'b1', 'a3', 'b2', 'c1' , 'c2', 'b3'])

group = df.columns.str.extract(r'(\D+)', expand=False)
means = df.groupby(group, axis=1).transform('mean')
result = df - means
print(result)

which yields 产量

            a1  a2  b1  a3  b2  c1  c2  b3
2016-05-18  29  29  53  29  53  23  23  53
2016-05-17  55  55  32  55  32  92  92  32
2016-05-16  59  59  53  59  53  50  50  53
2016-05-15  46  46  30  46  30  55  55  30
2016-05-14  56  56  28  56  28  28  28  28
2016-05-13  34  34  36  34  36  70  70  36
2016-05-12  39  39  64  39  64  48  48  64
2016-05-11  45  45  59  45  59  57  57  59
2016-05-10  55  55  30  55  30  37  37  30
2016-05-09  61  61  59  61  59  59  59  59

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM