【问题标题】:pandas - groupby and filtering for consecutive valuespandas - groupby 和过滤连续值
【发布时间】:2016-02-20 09:09:41
【问题描述】:

我有这个数据框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

UDatetime 对象制作。我想要做的是过滤 U 在月/年中至少连续出现三个的值。到目前为止,我已按Uyearmonth 分组为:

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

第三列与不同月份/年份的发生有关。在这种情况下,只有0203U 值至少包含三个连续的月/年值。现在我不知道如何选择这些用户并将它们列在列表中,例如,或者只是将它们保留在原始数据框df 中并丢弃其他用户。我也试过了:

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

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

【问题讨论】:

  • 困难....我通过u = m.unstack('U') 开始取得一些进展。这里的诀窍是月份和年份不能包含间隙。然后((u == u.shift(-1)) & (u == u.shift(-2))).any() 会告诉您哪些值连续三个月。

标签: python pandas dataframe time-series


【解决方案1】:

我终于可以想出解决方案了:)。

为了让您了解自定义函数的工作原理,只需从之前的值中减去月份的值,结果当然应该是 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-11-18
    • 1970-01-01
    • 2019-02-06
    • 2019-03-16
    • 1970-01-01
    • 1970-01-01
    • 2017-01-20
    • 2018-07-22
    相关资源
    最近更新 更多