如何根据条件为每个ID选择行

时间:2020-09-11 11:25:57

标签: python pandas dataframe pandas-groupby

我有以下数据框:

Hotel_id    Month_Year      Chef_Id  Chef_is_masterchef  Transition
2400188     February-2018   4597566     1                    0
2400188     March-2018      4597566     1                    0
2400188     April-2018      4597566     1                    0
2400188     May-2018        4597566     1                    0
2400188     June-2018       4597566     1                    0
2400188     July-2018       4597566     1                    0
2400188     August-2018     4597566     1                    0
2400188     September-2018  4597566     0                    1
2400188     October-2018    4597566     0                    0
2400188     November-2018   4597566     0                    0
2400188     December-2018   4597566     0                    0
2400188     January-2019    4597566     0                    0
2400188     February-2019   4597566     0                    0
2400188     March-2019      4597566     0                    0
2400188     April-2019      4597566     0                    0
2400188     May-2019        4597566     0                    0


2400614     May-2015        2297544     0                    0
2400614     June-2015       2297544     0                    0
2400614     July-2015       2297544     0                    0
2400614     August-2015     2297544     0                    0
2400614     September-2015  2297544     0                    0
2400614     October-2015    2297544     0                    0
2400614     November-2015   2297544     0                    0
2400614     December-2015   2297544     0                    0
2400614     January-2016    2297544     1                    1
2400614     February-2016   2297544     1                    0
2400614     March-2016      2297544     1                    0

3400624     May-2016        2597531     0                    0
3400624     June-2016       2597531     0                    0
3400624     July-2016       2597531     0                    0
3400624     August-2016     2597531     1                    1

2400133     February-2016   4597531     0                    0
2400133     March-2016      4597531     0                    0
2400133     April-2016      4597531     0                    0
2400133     May-2016        4597531     0                    0
2400133     June-2016       4597531     0                    0
2400133     July-2016       4597531     0                    0
2400133     August-2016     4597531     1                    1
2400133     September-2016  4597531     1                    0
2400133     October-2016    4597531     1                    0
2400133     November-2016   4597531     1                    0
2400133     December-2016   4597531     1                    0
2400133     January-2017    4597531     1                    0
2400133     February-2017   4597531     1                    0
2400133     March-2017      4597531     1                    0
2400133     April-2017      4597531     1                    0
2400133     May-2017        4597531     1                    0

当在 Chef_is_Masterchef 列中从 0到1 1到0 进行转换时,此转换会在过渡列为 1

实际上,我考虑过创建另一列(名称为“ Var ”),该列中的值将按照以下针对原始数据帧的说明进行填充,

期望的数据框:

Hotel_id    Month_Year      Chef_Id  Chef_is_masterchef  Transition  Var
2400188     February-2018   4597566     1                    0       -7
2400188     March-2018      4597566     1                    0       -6
2400188     April-2018      4597566     1                    0       -5
2400188     May-2018        4597566     1                    0       -4
2400188     June-2018       4597566     1                    0       -3
2400188     July-2018       4597566     1                    0       -2
2400188     August-2018     4597566     1                    0       -1
2400188     September-2018  4597566     0                    1        0
2400188     October-2018    4597566     0                    0        1
2400188     November-2018   4597566     0                    0        2
2400188     December-2018   4597566     0                    0        3
2400188     January-2019    4597566     0                    0        4
2400188     February-2019   4597566     0                    0        5
2400188     March-2019      4597566     0                    0        6
2400188     April-2019      4597566     0                    0        7
2400188     May-2019        4597566     0                    0        8

2400614     May-2015        2297544     0                    0       -8
2400614     June-2015       2297544     0                    0       -7
2400614     July-2015       2297544     0                    0       -6
2400614     August-2015     2297544     0                    0       -5
2400614     September-2015  2297544     0                    0       -4
2400614     October-2015    2297544     0                    0       -3
2400614     November-2015   2297544     0                    0       -2
2400614     December-2015   2297544     0                    0       -1
2400614     January-2016    2297544     1                    1        0
2400614     February-2016   2297544     1                    0        1
2400614     March-2016      2297544     1                    0        2

3400624     May-2016        2597531     0                    0       -3
3400624     June-2016       2597531     0                    0       -2
3400624     July-2016       2597531     0                    0       -1
3400624     August-2016     2597531     1                    1        0

2400133     February-2016   4597531     0                    0       -6
2400133     March-2016      4597531     0                    0       -5
2400133     April-2016      4597531     0                    0       -4
2400133     May-2016        4597531     0                    0       -3
2400133     June-2016       4597531     0                    0       -2
2400133     July-2016       4597531     0                    0       -1
2400133     August-2016     4597531     1                    1        0
2400133     September-2016  4597531     1                    0        1
2400133     October-2016    4597531     1                    0        2
2400133     November-2016   4597531     1                    0        3
2400133     December-2016   4597531     1                    0        4
2400133     January-2017    4597531     1                    0        5
2400133     February-2017   4597531     1                    0        6
2400133     March-2017      4597531     1                    0        7
2400133     April-2017      4597531     1                    0        8
2400133     May-2017        4597531     1                    0        9

如果观察到的话,在 Var 列中的过渡点处,我将该值设为零,并在保持相应整数值之前和之后的各行中给出该值。

但是使用下面的代码后,我在Var列中遇到了问题,

s = df['Chef_is_masterchef'].eq(0).groupby(df['Chef_Id']).transform('sum')
df['var'] = df.groupby('Chef_Id').cumcount().sub(s)

上述代码的输出

Hotel_id    Month_Year      Chef_Id  Chef_is_masterchef  Transition  Var
2400188     February-2018   4597566     1                    0       -9
2400188     March-2018      4597566     1                    0       -8
2400188     April-2018      4597566     1                    0       -7
2400188     May-2018        4597566     1                    0       -6
2400188     June-2018       4597566     1                    0       -5
2400188     July-2018       4597566     1                    0       -4
2400188     August-2018     4597566     1                    0       -3
2400188     September-2018  4597566     0                    1       -2
2400188     October-2018    4597566     0                    0       -1
2400188     November-2018   4597566     0                    0        0
2400188     December-2018   4597566     0                    0        1
2400188     January-2019    4597566     0                    0        2
2400188     February-2019   4597566     0                    0        3
2400188     March-2019      4597566     0                    0        4
2400188     April-2019      4597566     0                    0        5
2400188     May-2019        4597566     0                    0        6

2400614     May-2015        2297544     0                    0       -8
2400614     June-2015       2297544     0                    0       -7
2400614     July-2015       2297544     0                    0       -6
2400614     August-2015     2297544     0                    0       -5
2400614     September-2015  2297544     0                    0       -4
2400614     October-2015    2297544     0                    0       -3
2400614     November-2015   2297544     0                    0       -2
2400614     December-2015   2297544     0                    0       -1
2400614     January-2016    2297544     1                    1        0
2400614     February-2016   2297544     1                    0        1
2400614     March-2016      2297544     1                    0        2

3400624     May-2016        2597531     0                    0       -3
3400624     June-2016       2597531     0                    0       -2
3400624     July-2016       2597531     0                    0       -1
3400624     August-2016     2597531     1                    1        0

2400133     February-2016   4597531     0                    0       -6
2400133     March-2016      4597531     0                    0       -5
2400133     April-2016      4597531     0                    0       -4
2400133     May-2016        4597531     0                    0       -3
2400133     June-2016       4597531     0                    0       -2
2400133     July-2016       4597531     0                    0       -1
2400133     August-2016     4597531     1                    1        0
2400133     September-2016  4597531     1                    0        1
2400133     October-2016    4597531     1                    0        2
2400133     November-2016   4597531     1                    0        3
2400133     December-2016   4597531     1                    0        4
2400133     January-2017    4597531     1                    0        5
2400133     February-2017   4597531     1                    0        6
2400133     March-2017      4597531     1                    0        7
2400133     April-2017      4597531     1                    0        8
2400133     May-2017        4597531     1                    0        9

如果观察到的话,对于Chef_Id = 4597566,您可以看到在过渡点的值在Var列中是不同的,而不是零。

这造成了一个问题,因为在过渡时,我必须为每个id选择包括最多3个月之前和2个月之后的行。同样在过渡时,我必须使用以下代码为每个ID选择包括最长6个月之前和5个月之后的行:

df1 = df[df['var'].between(-3, 2)]
print (df1)

df2 = df[df['var'].between(-6, 5)]
print (df2)

所以请让我知道解决方法。

谢谢!

1 个答案:

答案 0 :(得分:1)

GroupBy.cumcount用于每组的计数器,然后通过与0GroupBy.transform进行比较来减去0值的数量:

s = df['Chef_is_masterchef'].eq(0).groupby(df['Chef_Id']).transform('sum')
df['var'] = df.groupby('Chef_Id').cumcount().sub(s)

print (df)
    Hotel_id      Month_Year  Chef_Id  Chef_is_masterchef  Transition  var
0    2400614        May-2015  2297544                   0           0   -8
1    2400614       June-2015  2297544                   0           0   -7
2    2400614       July-2015  2297544                   0           0   -6
3    2400614     August-2015  2297544                   0           0   -5
4    2400614  September-2015  2297544                   0           0   -4
5    2400614    October-2015  2297544                   0           0   -3
6    2400614   November-2015  2297544                   0           0   -2
7    2400614   December-2015  2297544                   0           0   -1
8    2400614    January-2016  2297544                   1           1    0
9    2400614   February-2016  2297544                   1           0    1
10   2400614      March-2016  2297544                   1           0    2
11   3400624        May-2016  2597531                   0           0   -3
12   3400624       June-2016  2597531                   0           0   -2
13   3400624       July-2016  2597531                   0           0   -1
14   3400624     August-2016  2597531                   1           1    0
15   2400133   February-2016  4597531                   0           0   -6
16   2400133      March-2016  4597531                   0           0   -5
17   2400133      April-2016  4597531                   0           0   -4
18   2400133        May-2016  4597531                   0           0   -3
19   2400133       June-2016  4597531                   0           0   -2
20   2400133       July-2016  4597531                   0           0   -1
21   2400133     August-2016  4597531                   1           1    0
22   2400133  September-2016  4597531                   1           0    1
23   2400133    October-2016  4597531                   1           0    2
24   2400133   November-2016  4597531                   1           0    3
25   2400133   December-2016  4597531                   1           0    4
26   2400133    January-2017  4597531                   1           0    5
27   2400133   February-2017  4597531                   1           0    6
28   2400133      March-2017  4597531                   1           0    7
29   2400133      April-2017  4597531                   1           0    8
30   2400133        May-2017  4597531                   1           0    9

最后Series.between个过滤器:

df1 = df[df['var'].between(-3, 2)]
print (df1)

df2 = df[df['var'].between(-6, 5)]
print (df2)