根据给定datafram的列中的条件创建新的数据帧

2024-05-14 00:16:15 发布

您现在位置:Python中文网/ 问答频道 /正文

我有以下数据帧

在测向头()

UID     Timestamp       Weekday  Business_hour
AAD 2017-07-11 09:31:44 TRUE    TRUE
AAD 2017-07-11 23:24:43 TRUE    FALSE
AAD 2017-07-12 13:24:43 TRUE    TRUE
SAP 2017-07-23 14:24:34 FALSE   FALSE
SAP 2017-07-24 16:58:49 TRUE    TRUE
YAS 2017-07-31 21:10:35 TRUE    FALSE

基于以下条件

Active:同一UID是否有更大的事件。那是,同一个UID在同一天出现2次以上。在

多天:同一UID是否在多天(2天以上)处于活动状态。在

工作日:在工作日的工作时间内是否会出现相同的UID。在

目标输出应该看起来像

^{pr2}$

Tags: 数据falsetrueuid事件business条件timestamp
1条回答
网友
1楼 · 发布于 2024-05-14 00:16:15

你可以这样逐个计算:

data.Timestamp = pd.to_datetime(data.Timestamp)
data['date' ] = [x.date() for x in data.Timestamp]

target_df = pd.DataFrame()
target_df['UID'] = data.UID.unique()


a = data.groupby(['UID', 'date']).size()
a = a[a>1]
target_df['Active'] = [True if x in pd.DataFrame(a).reset_index().UID.values else False for x in target_df.UID.values]

a = data.groupby('UID')['Timestamp'].nunique()
a = a[a>1]
target_df['Multiple_days'] = [True if x in pd.DataFrame(a).reset_index().UID.values else False for x in target_df.UID.values]

a = data[(data.Weekday==True)&(data.Business_hour==True)].UID.unique()
target_df['Busi_weekday'] = [True if x in a else False for x in target_df.UID.values]

target_df

enter image description here

相关问题 更多 >