python - 通过与连续组进行比较来过滤 pandas groupby

标签 python pandas

我有一个像这样的 pandas 数据框:

In [5]: df
Out[5]:
       date1      date2
0 2015-01-01 2014-12-11
1 2015-01-01 2014-12-30
2 2015-01-01 2015-01-01
3 2015-01-02 2015-12-30
4 2015-01-02 2015-01-01
5 2015-01-02 2015-01-02
6 2015-01-03 2015-01-01
7 2015-01-03 2015-01-02
8 2015-01-03 2015-01-03

我想在 date1 上对此数据帧进行分组,然后按 date2 >= 上一个组的 date1 的记录过滤每个组(并且date1 最小的记录不会被过滤掉)。我的最终目标是计算应用过滤器后每组中剩余的项目数。

过滤将留下以下行:

       date1    date2
0 2015-01-01  2014-12-11
1 2015-01-01  2014-12-30
2 2015-01-02  2015-01-01
4 2015-01-02  2015-01-01
5 2015-01-02  2015-01-02
7 2015-01-03  2015-01-02
8 2015-01-03  2015-01-03

然后计数将是:

    date1    count
0 2015-01-01 3
1 2015-01-02 2
2 2015-01-03 2

我可以按如下方式获取组:

groups = df.sort('timestamp', ascending=False).groupby('timestamp')

但我想不出一种方法来进行过滤和计数,以便比较连续的组。

最佳答案

一行使用 pd.merge_asof

pd.merge_asof(
    df, df[['date1']].assign(d_=df.date1),
    allow_exact_matches=False
).fillna(0).query('date2 >= d_').groupby('date1').size()

date1
2015-01-01    3
2015-01-02    2
2015-01-03    2
dtype: int64

说明

from the docs

For each row in the left DataFrame, we select the last row in the right DataFrame whose ‘on’ key is less than or equal to the left’s key. Both DataFrames must be sorted by the key.

因此,我使 dfdate1 上与自身合并,参数 allow_exact_matchesFalse。这使我可以轻松访问“上一个组”。

从那里,它是一个查询来过滤,groupby + size来获取计数。

关于python - 通过与连续组进行比较来过滤 pandas groupby,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41510099/

相关文章:

python - 使用 python 根据 pandas 中的时间获取不同的值

python - Dataframe 过滤列(如果它是区间数据类型)

python - 是否有纯 python 表类?

pandas - Pandas 中的 "roundtripping"是什么?

python - 如何在字符串中找到未转义的单花括号的索引位置?

python - pandas dataframe上的for-if循环语句操作问题

python - 颜色并不一致地应用于子图中的类别

python - 获取每列中的第一个出现值

python - 如何使用 Poetry 查找特定包的可用版本列表?

python - 如何在 webapp2 中处理上传的文件