Python基于先前的时间段创建变量

2024-10-01 17:24:58 发布

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

嗨,我有如下数据集:

import pandas as pd
d1={'Participant_ID':['P1','P1','P1','P1','P1','P1','P2','P2','P2','P2','P2','P2'],
'Brand_Type':['B1','B1','B2','B1','B2','B2','B2','B2','B1','B2','B1','B1'],
'Date1':['5/6/2010','1/27/2010','4/3/2010','7/11/2010','3/1/2010','6/8/2010',
       '5/6/2010','1/27/2010','4/3/2010','7/11/2010','3/1/2010','6/8/2010']}
d11=pd.DataFrame(d1)
d11['Date2'] = pd.to_datetime(d11['Date1'])
#converting to date var explicitly to avoid any issues and sorting the data by Participant ID and Date
d111=d11.sort_values(by=['Participant_ID','Date2'])

数据集表示客户在某个时间点消费的品牌

我想从这个数据集中创建3个变量:

a)表示品牌类型是B1还是B2的虚拟变量。我可以这样做:

d111['Brand_Type_new']=d111['Brand_Type'] 
#creating a separate column to preserve the Brand_type variable
d1111= pd.get_dummies(d111, prefix='Category_', columns=['Brand_Type'])

b)一个虚拟变量,表示参与者是否在两个时期前消费了品牌B1

enter image description here

注意:对于参与者1,前两行是0,因为没有观察结果。第三行(对于2010年4月3日)是1,因为客户在2010年1月27日(两个时期前)消费了B1

c)一个变量,表示参与者之前(不包括当前期间)消费品牌的次数

enter image description here

对于第一行,值是0。第二行是1,因为参与者在上一个期间已经消耗了B1,依此类推

提前多谢了

这是一个测试数据,两个参与者的原始数据日期不同(参与者的观察次数不同)。我需要帮助如何做到这一点时,日期等数目是不同的参与者


Tags: to数据idtype参与者b2b1pd
1条回答
网友
1楼 · 发布于 2024-10-01 17:24:58

基于以下a部分的解决方案:

d111['Brand_Type_new']=d111['Brand_Type'] 
#creating a separate column to preserve the Brand_type variable
d1111= pd.get_dummies(d111, prefix='Category_', columns=['Brand_Type'])
print(d1111)

   Participant_ID      Date1      Date2 Brand_Type_new  Category__B1  \
1              P1  1/27/2010 2010-01-27             B1             1   
4              P1   3/1/2010 2010-03-01             B2             0   
2              P1   4/3/2010 2010-04-03             B2             0   
0              P1   5/6/2010 2010-05-06             B1             1   
5              P1   6/8/2010 2010-06-08             B2             0   
3              P1  7/11/2010 2010-07-11             B1             1   
7              P2  1/27/2010 2010-01-27             B2             0   
10             P2   3/1/2010 2010-03-01             B1             1   
8              P2   4/3/2010 2010-04-03             B1             1   
6              P2   5/6/2010 2010-05-06             B2             0   
11             P2   6/8/2010 2010-06-08             B1             1   
9              P2  7/11/2010 2010-07-11             B2             0   

    Category__B2  
1              0  
4              1  
2              1  
0              0  
5              1  
3              0  
7              1  
10             0  
8              0  
6              1  
11             0  
9              1  

您可以使用^{}+^{}^{}

new_d=d1111.copy()
B1_groups=new_d.groupby('Participant_ID')['Category__B1']
new_d['B1_dummy_2periodAgo']=B1_groups.shift(2,fill_value=0)
new_d['B1_Cumulative']=B1_groups.apply(lambda x: x.cumsum().shift(fill_value=0))
print(new_d)


   Participant_ID      Date1      Date2 Brand_Type_new  Category__B1  \
1              P1  1/27/2010 2010-01-27             B1             1   
4              P1   3/1/2010 2010-03-01             B2             0   
2              P1   4/3/2010 2010-04-03             B2             0   
0              P1   5/6/2010 2010-05-06             B1             1   
5              P1   6/8/2010 2010-06-08             B2             0   
3              P1  7/11/2010 2010-07-11             B1             1   
7              P2  1/27/2010 2010-01-27             B2             0   
10             P2   3/1/2010 2010-03-01             B1             1   
8              P2   4/3/2010 2010-04-03             B1             1   
6              P2   5/6/2010 2010-05-06             B2             0   
11             P2   6/8/2010 2010-06-08             B1             1   
9              P2  7/11/2010 2010-07-11             B2             0   

    Category__B2  B1_dummy_2periodAgo  B1_Cumulative  
1              0                    0              0  
4              1                    0              1  
2              1                    1              1  
0              0                    0              1  
5              1                    0              2  
3              0                    1              2  
7              1                    0              0  
10             0                    0              0  
8              0                    0              1  
6              1                    1              2  
11             0                    1              2  
9              1                    0              3 

相关问题 更多 >

    热门问题