我有一个dataframe df
,在just_dates
列中有用户活动的日期,例如:
just_dates Count
0 2015-06-01 89
1 2015-06-02 44
2 2015-06-03 61
3 2015-06-04 47
4 2015-06-05 82
5 2015-06-09 46
12 2015-06-19 42
13 2015-06-21 1
14 2015-06-22 68
15 2015-06-23 82
.. ... ...
529 2017-05-24 86
535 2017-06-02 63
536 2017-06-04 1
537 2017-06-05 28
538 2017-06-06 77
有没有办法让我检查用户是否每周至少在线两次?在这种情况下,用户在第一周至少浪费了两个活跃时间,而不是像倒数第二周和最后一周一样?我还喜欢添加一种容忍度,即他不需要在某个时间一周活动两次。
发布于 2019-06-10 00:08:50
假设dataframe df
包含单个用户的信息,下面的代码应该可以解决这个问题:
import pandas as pd
df = pd.DataFrame([['2015-06-01', 1], ['2015-06-02', 3], ['2016-06-23', 1]], columns=['just_dates', 'Count'])
# Convert just_dates to datetime
df['just_dates'] = pd.to_datetime(df['just_dates'])
weekly_counts = df.groupby(pd.Grouper(key='just_dates', freq='W')).Count.sum()
print(weekly_counts.where(weekly_counts > 1).dropna())
输出应该是:
just_dates
2015-06-07 4.0
Freq: W-SUN, Name: Count, dtype: float64
https://stackoverflow.com/questions/56515951
复制相似问题