如何应用具有多个条件的Pandas Groupby进行拆分并应用多个计算?

时间:2014-09-13 22:23:17

标签: python pandas split dataframe subset

我有一个数据框,我需要根据2个条件进行拆分,并将此条件应用于3个不同的列,然后使用这些新拆分以成对方式对其他列执行某些计算。

所以我有一个df A

A = pd.DataFrame([[1, 5, 2, 3, 4, 1], [2, 4, 4, 5, 5, 1], [3, 3, 1, 2, 5, 4], [4, 2, 2, 3, 5, 5], [5, 1, 4, 2, 3, 1]],
                 columns=['A', 'B', 'C', 'D', 'E', 'F'], index=[1, 2, 3, 4, 5])

我需要根据2个条件拆分数据框并应用于前三列,例如"

condition1_colA = A[A['A'] > 1]
condition2_colA = A[A['A'] < 1]
condition1_colB = A[A['B'] > 1]
condition2_colB = A[A['B'] < 1]
condition1_colC = A[A['C'] > 1]
condition2_colC = A[A['C'] < 1]

现在,我需要对6个新切片数据帧中的每一个进行3次计算,但是这个计算是按以下方式配对的:

  • condition1_colA和condition2_colA与列[&#39; D&#39;]
  • condition1_colB和condition2_colB与列[&#39; E&#39;]
  • condition1_colC和condition2_colC与列[&#39; F&#39;]

这是第一对的例子:

x = condition1_colA['D'].mean()
y = condition1_colA['D'].std()
z = condition1_colA['D'][condition1_colA['D'] > 0].count()
x1 = condition2_colA['D'].mean()
y1 = condition2_colA['D'].std()
z1 = condition2_colA['D'][condition1_colA['D'] < 0].count()

然后我们会对其他两对做同样的事情。并在数据帧上组织18个计算。我知道如何手动执行此操作,但我希望通过groupby或者数据透视表来提供帮助。

由于

1 个答案:

答案 0 :(得分:4)

import pandas as pd

A = pd.DataFrame(
    [[1, 5, 2, 3, 4, 1], [2, 4, 4, 5, 5, 1], [3, 3, 1, 2, 5, 4],
     [4, 2, 2, 3, 5, 5], [5, 1, 4, 2, 3, 1]],
    columns=['A', 'B', 'C', 'D', 'E', 'F'], index=[1, 2, 3, 4, 5])

result = []
pairs = []
for col1, col2 in zip(list('ABC'), list('DEF')):
    pairs.append(col1+col2)
    mask = A[col1] > 1
    mask2 = mask & (A[col2] > 0)
    ser = A.loc[mask, col2]
    result.append((ser.mean(), ser.std(), mask2.sum()))

    mask = A[col1] < 1
    mask2 = mask & (A[col2] < 0)
    ser = A.loc[mask, col2]
    result.append((ser.mean(), ser.std(), mask2.sum()))

index = pd.MultiIndex.from_product([pairs, [1, 2]], names=['pair', 'cond'])
df = pd.DataFrame(result, index=index, columns=['mean', 'sum', 'count'])
print(df)

产量

           mean       sum  count
pair cond                       
AD   1     3.00  1.414214      4
     2      NaN       NaN      0
BE   1     4.75  0.500000      4
     2      NaN       NaN      0
CF   1     2.00  2.000000      4
     2      NaN       NaN      0