pandas 中所有先前行的有条件运行计数

编程入门 行业动态 更新时间:2024-10-27 04:38:13
本文介绍了 pandas 中所有先前行的有条件运行计数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

假设我具有以下DataFrame:

Suppose I have the following DataFrame:

df = pd.DataFrame({'Event': ['A', 'B', 'A', 'A', 'B', 'C', 'B', 'B', 'A', 'C'], 'Date': ['2019-01-01', '2019-02-01', '2019-03-01', '2019-03-01', '2019-02-15', '2019-03-15', '2019-04-05', '2019-04-05', '2019-04-15', '2019-06-10'], 'Sale':[100,200,150,200,150,100,300,250,500,400]}) df['Date'] = pd.to_datetime(df['Date']) df Event Date A 2019-01-01 B 2019-02-01 A 2019-03-01 A 2019-03-01 B 2019-02-15 C 2019-03-15 B 2019-04-05 B 2019-04-05 A 2019-04-15 C 2019-06-10

我想获得以下结果:

Event Date Previous_Event_Count A 2019-01-01 0 B 2019-02-01 0 A 2019-03-01 1 A 2019-03-01 1 B 2019-02-15 1 C 2019-03-15 0 B 2019-04-05 2 B 2019-04-05 2 A 2019-04-15 3 C 2019-06-10 1

其中,df['Previous_Event_Count']是事件(df['Event'])在其相邻日期(df['Date'])之前发生的事件(行)的编号.例如,

where df['Previous_Event_Count'] is the number of an event (rows) when the event (df['Event']) takes place before its adjacent date (df['Date']). For instance,

  • 2019年1月1日之前发生的事件A的数量为0,
  • 2019年1月1日之前发生的事件A的数量为1,并且
  • 事件A发生在2019-04-15之前的数目是3.

我可以使用此行获得所需的结果:

I am able to obtain the desired result using this line:

df['Previous_Event_Count'] = [df.loc[(df.loc[i, 'Event'] == df['Event']) & (df.loc[i, 'Date'] > df['Date']), 'Date'].count() for i in range(len(df))]

虽然速度很慢,但是效果很好.我相信有更好的方法可以做到这一点.我已经尝试过这一行:

Although, it is slow but it works fine. I believe there is a better way to do that. I have tried this line:

df['Previous_Event_Count'] = df.query('Date < Date').groupby(['Event', 'Date']).cumcount()

但是会产生NaNs.

推荐答案

groupby + rank

日期可以视为数字.使用'min'获取计数逻辑.

groupby + rank

Dates can be treated as numeric. Use'min' to get your counting logic.

df['PEC'] = (df.groupby('Event').Date.rank(method='min')-1).astype(int) Event Date PEC 0 A 2019-01-01 0 1 B 2019-02-01 0 2 A 2019-03-01 1 3 A 2019-03-01 1 4 B 2019-02-15 1 5 C 2019-03-15 0 6 B 2019-04-05 2 7 B 2019-04-05 2 8 A 2019-04-15 3 9 C 2019-06-10 1

更多推荐

pandas 中所有先前行的有条件运行计数

本文发布于:2023-11-22 00:47:08,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1615315.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:有条件   pandas

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!