熊猫-分组和过滤连续值

法比奥·拉曼纳(Fabio Lamanna)

我有这个数据框df

U,Datetime
01,2015-01-01 20:00:00
01,2015-02-01 20:05:00
01,2015-04-01 21:00:00
01,2015-05-01 22:00:00
01,2015-07-01 22:05:00
02,2015-08-01 20:00:00
02,2015-09-01 21:00:00
02,2014-01-01 23:00:00
02,2014-02-01 22:05:00
02,2015-01-01 20:00:00
02,2014-03-01 21:00:00
03,2015-10-01 20:00:00
03,2015-11-01 21:00:00
03,2015-12-01 23:00:00
03,2015-01-01 22:05:00
03,2015-02-01 20:00:00
03,2015-05-01 21:00:00
03,2014-01-01 20:00:00
03,2014-02-01 21:00:00

U制成Datetime我想做的是过滤U以月/年为单位的至少连续三次出现的值。到目前为止,我已按Uyear分组month

m = df.groupby(['U',df.index.year,df.index.month]).size()

获得:

U          
1  2015  1     1
         2     1
         4     1
         5     1
         7     1
2  2014  1     1
         2     1
         3     1
   2015  1     1
         8     1
         9     1
3  2014  1     1
         2     1
   2015  1     1
         2     1
         5     1
         10    1
         11    1
         12    1

第三列与不同月份/年份中的发生次数相关。在这种情况下,月/年中U0203至少包含三个连续值。现在,我不知道如何选择这些用户并将其从列表中删除,或者仅将其保留在原始数据框中df并丢弃其他用户。我也尝试过:

g = m.groupby(level=[0,1]).diff()

但是我无法获得任何有用的信息。

纳德·希瑟姆(Nader Hisham)

最后,我可以提出解决方案:)。

为了让您了解自定义函数的工作原理,只需从前一个值中减去月份的值,结果one当然应该是,并且应该发生两次,例如,如果您有一个数字列表[5 , 6 , 7],则7 - 6 = 16 - 5 = 11这里出现了两次,所以条件已经满足

In [80]:
df.reset_index(inplace=True)

In [281]:
df['month'] = df.Datetime.dt.month
df['year'] = df.Datetime.dt.year
df
Out[281]:
            Datetime    U   month   year
0   2015-01-01 20:00:00 1   1       2015
1   2015-02-01 20:05:00 1   2       2015
2   2015-04-01 21:00:00 1   4       2015
3   2015-05-01 22:00:00 1   5       2015
4   2015-07-01 22:05:00 1   7       2015
5   2015-08-01 20:00:00 2   8       2015
6   2015-09-01 21:00:00 2   9       2015
7   2014-01-01 23:00:00 2   1       2014
8   2014-02-01 22:05:00 2   2       2014
9   2015-01-01 20:00:00 2   1       2015
10  2014-03-01 21:00:00 2   3       2014
11  2015-10-01 20:00:00 3   10      2015
12  2015-11-01 21:00:00 3   11      2015
13  2015-12-01 23:00:00 3   12      2015
14  2015-01-01 22:05:00 3   1       2015
15  2015-02-01 20:00:00 3   2       2015
16  2015-05-01 21:00:00 3   5       2015
17  2014-01-01 20:00:00 3   1       2014
18  2014-02-01 21:00:00 3   2       2014

In [284]:
g = df.groupby([df['U'] , df.year])

In [86]:
res = g.filter(lambda x : is_at_least_three_consec(x['month'].diff().values.tolist()))
res
Out[86]:
      Datetime          U   month   year
7   2014-01-01 23:00:00 2   1       2014
8   2014-02-01 22:05:00 2   2       2014
10  2014-03-01 21:00:00 2   3       2014
11  2015-10-01 20:00:00 3   10      2015
12  2015-11-01 21:00:00 3   11      2015
13  2015-12-01 23:00:00 3   12      2015
14  2015-01-01 22:05:00 3   1       2015
15  2015-02-01 20:00:00 3   2       2015
16  2015-05-01 21:00:00 3   5       2015

如果要查看自定义函数的结果

In [84]:
res = g['month'].agg(lambda x : is_at_least_three_consec(x.diff().values.tolist()))
res
Out[84]:
U  year
1  2015    False
2  2014     True
   2015    False
3  2014    False
   2015     True
Name: month, dtype: bool

这是实现自定义功能的方式

In [53]:    
def is_at_least_three_consec(month_diff):
    consec_count = 0
    #print(month_diff)
    for index , val in enumerate(month_diff):
        if index != 0 and val == 1:
                consec_count += 1
                if consec_count == 2:
                    return True
        else:
            consec_count = 0
​
    return False

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章