Skip to main content
 首页 » 编程设计

Python pandas 如何根据特殊时间范围条件统计行数

2024年10月01日8del

我有一个这样的数据框:

person  action_type        time 
A           4           2014-11-10 
A           4           2014-11-15 
A           3           2014-11-16 
A           1           2014-11-18 
A           4           2014-11-19 
B           4           2014-11-13 
B           2           2014-11-15 
B           4           2014-11-19 

所以我想添加一个名为“action_4”的新列,它表示过去 7 天的人的 action_type 计数为 4(不包括它自己)。 结果是这样的:

person  action_type        time      action_4 
A           4           2014-11-10      0 
A           4           2014-11-15      1 
A           3           2014-11-16      2 
A           1           2014-11-18      1 
A           4           2014-11-19      1 
B           4           2014-11-13      0 
B           2           2014-11-15      1 
B           4           2014-11-19      1 

因为我的数据框的形状是 21649900*3,所以请避免使用 for...in...。

请您参考如下方法:

这是我的方法。

我认为根据时间(例如 7 天)检查间隔总是非常昂贵,因此最好依靠观察次数。 (实际上,在最新的 pandas 版本中,他们引入了“时间感知”滚动,但我对此没有经验......)

所以我的方法是,对于每个人,强制执行每日频率,然后简单地计算过去 7 天(不包括今天)发生的 action_4 的次数。我已经在代码中添加了注释,应该可以清楚地说明这一点,但请随时要求提供更多解释。

import pandas as pd 
from io import StringIO 
 
inp_str = u""" 
person action_type time 
A 4 2014-11-10 
A 4 2014-11-15 
A 3 2014-11-16 
A 1 2014-11-18 
A 4 2014-11-19 
B 4 2014-11-13 
B 2 2014-11-15 
B 4 2014-11-19 
""" 
 
or_df = pd.read_csv(StringIO(inp_str), sep = " ").set_index('time') 
or_df.index = pd.to_datetime(or_df.index) 
 
# Find first and last date for each person 
min_dates = or_df.groupby('person').apply(lambda x: x.index[0]) 
max_dates = or_df.groupby('person').apply(lambda x: x.index[-1]) 
 
# Resample each person to daily frequency so that 1 obs = 1 day 
transf_df = pd.concat([el.reindex(pd.date_range(min_dates[pp], max_dates[pp], freq = 'D')) for pp, el in  or_df.groupby('person')]) 
# Forward fill person 
transf_df.loc[:, 'person'] = transf_df['person'].ffill() 
# Set a null value for action_type (possibly integer so you preserve the column type) 
transf_df = transf_df.fillna({'action_type' : -1}) 
 
# For each person count the number of action 4, exluding today 
result = transf_df.groupby('person').transform(lambda x: x.rolling(7, 1).apply(lambda y: len(y[y==4])).shift(1).fillna(0)) 
result.columns = ['action_4'] 
 
# Bring back to original index 
pd.concat([transf_df, result], axis = 1).set_index('person', append = True).loc[or_df.set_index('person', append = True).index, :] 

这给出了预期的输出:

                   action_type  action_4 
time       person                        
2014-11-10 A               4.0       0.0 
2014-11-15 A               4.0       1.0 
2014-11-16 A               3.0       2.0 
2014-11-18 A               1.0       1.0 
2014-11-19 A               4.0       1.0 
2014-11-13 B               4.0       0.0 
2014-11-15 B               2.0       1.0 
2014-11-19 B               4.0       1.0