本文介绍了 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 中所有先前行的有条件运行计数
发布评论