问题描述
我正在尝试评估促销活动对客户的影响。目的是从提供促销的时间点评估收入。但是,促销是针对不同地点的不同客户提供的。如何将数据重新排列到Month 0
,Month 1
,Month 2
,Month 3
。 Month 0
是客户首次获得促销的月份。
解决方法
使用以下自我解释代码,您可以获得所需的输出:
# Create DataFrame
import pandas as pd
df = pd.DataFrame({"Account":[1,2,3,4,5,6],\
"May-18":[181,166,221,158,210,159],\
"Jun-18":[178,222,230,189,219,200],\
"Jul-18":[184,207,175,167,201,204],\
"Aug-18":[161,174,178,233,223,\
"Sep-18":[218,209,165,204,225],\
"Oct-18":[199,206,205,196,212,205],\
"Nov-18":[231,218,234,235],\
"Dec-18":[173,\
"Promotion Month":["Sep-18","Aug-18","Jul-18","May-18","Jun-18"]})
df = df.set_index("Account")
cols = ["May-18","Jun-18","Sep-18","Oct-18","Nov-18","Dec-18","Promotion Month"]
df = df[cols]
# Define function to select the four months after promotion
def selectMonths(row):
cols = df.columns.to_list()
colMonth0 = cols.index(row["Promotion Month"])
colsOut = cols[colMonth0:colMonth0+4]
out = pd.Series(row[colsOut].to_list())
return out
# Apply the function and set the index and columns of output DataFrame
out = df.apply(selectMonths,axis=1)
out.index = df.index
out.columns=["Month 0","Month 1","Month 2","Month 3"]
那么您得到的输出是:
>>> out
Month 0 Month 1 Month 2 Month 3
Account
1 218 199 231 173
2 174 209 206 196
3 175 178 165 205
4 158 189 167 233
5 223 204 212 234
6 200 204 204 225