根据来自另一个数据框的多个列条件创建列

时间:2018-10-14 17:25:11

标签: python pandas numpy pandas-groupby multiple-conditions

假设我有两个数据框-条件和数据。

import pandas as pd

conditions = pd.DataFrame({'class': [1,2,3,4,4,5,5,4,4,5,5,5],
                           'primary_lower': [0,0,0,160,160,160,160,160,160,160,160,800],
                           'primary_upper':[9999,9999,9999,480,480,480,480,480,480,480,480,4000],
                           'secondary_lower':[0,0,0,3500,6100,3500,6100,0,4800,0,4800,10],
                           'secondary_upper':[9999,9999,9999,4700,9999,4700,9999,4699,6000,4699,6000,3000],
                           'group':['A','A','A','B','B','B','B','C','C','C','C','C']})

data = pd.DataFrame({'class':[1,1,4,4,5,5,2],
                     'primary':[2000,9100,1100,170,300,210,1000],
                     'secondary':[1232,3400,2400,380,3600,4800,8600]})

我想在“数据”表中生成一个新列(组),给定“条件”表中提供的条件,该组将为每行分配一个组。

条件表的结构使得每个组中的行用“ OR”连接,而列则用“ AND”连接。例如,要分配组“ B”:

(类别= 4 AND 160 <=主<= 480 AND 3500 <=次要<= 4700)

OR

(类别= 4 AND 160 <=主要<= 480 AND 6100 <=次要<= 9999)

OR

(类别= 5 AND 160 <=主要<= 480 AND 3500 <=次要<= 4700)

OR

(类别= 5 AND 160 <=主要<= 480 AND 6100 <=次要<= 9999)

任何不符合任何条件的行都将被分配组“其他”。因此,最终的数据帧将如下所示:

+-------+---------+-----------+-------+
| class | primary | secondary | group |
+-------+---------+-----------+-------+
|     1 |    2000 |      1232 | A     |
|     1 |    9100 |      3400 | A     |
|     4 |    1100 |      2400 | Other |
|     4 |     170 |       380 | C     |
|     5 |     300 |      3600 | B     |
|     5 |     210 |      4800 | C     |
|     2 |    1000 |      8600 | A     |
+-------+---------+-----------+-------+

1 个答案:

答案 0 :(得分:1)

您可以迭代GroupBy对象并在每个组中合并掩码:

for key, grp in conditions.groupby('group'):

    cols = ['class', 'primary_lower', 'primary_upper',
            'secondary_lower', 'secondary_upper']

    masks = (data['class'].eq(cls) & \
             data['primary'].between(prim_lower, prim_upper) & \
             data['secondary'].between(sec_lower, sec_upper) \
             for cls, prim_lower, prim_upper, sec_lower, sec_upper in \
             grp[cols].itertuples(index=False))

    data.loc[pd.concat(masks, axis=1).any(1), 'group'] = key

data['group'] = data['group'].fillna('Other')

结果:

print(data)

   class  primary  secondary  group
0      1     2000       1232      A
1      1     9100       3400      A
2      4     1100       2400  Other
3      4      170        380      C
4      5      300       3600      C
5      5      210       4800      C
6      2     1000       8600      A

注意index=4的结果与所需的输出不同,因为存在多个满足数据的条件。