1 回答
TA贡献1848条经验 获得超2个赞
您可以尝试使用列表理解,pd.date_range并且explode
df['Weighted_revenue']=(df['Dealsize'].astype(float)/df['Duration'].astype(float))*df['Probability'].astype(float)
df['Period']=[pd.date_range(x, periods=y, freq="M").strftime('%Y-%m') for x,y in zip(df["Start_period"], df["Duration"])]
df=df.explode('Period')
输出:
df
Client Stage Probability Dealsize Duration Start_period Weighted_revenue Period
0 A suspect 0.25 1200 6 2020-08 50.0 2020-08
0 A suspect 0.25 1200 6 2020-08 50.0 2020-09
0 A suspect 0.25 1200 6 2020-08 50.0 2020-10
0 A suspect 0.25 1200 6 2020-08 50.0 2020-11
0 A suspect 0.25 1200 6 2020-08 50.0 2020-12
0 A suspect 0.25 1200 6 2020-08 50.0 2021-01
1 B prospect 0.60 1000 4 2020-10 150.0 2020-10
1 B prospect 0.60 1000 4 2020-10 150.0 2020-11
1 B prospect 0.60 1000 4 2020-10 150.0 2020-12
1 B prospect 0.60 1000 4 2020-10 150.0 2021-01
细节:
首先,我们'Weighted_revenue'使用您描述的公式创建列:
df['Weighted_revenue']=(df['Dealsize'].astype(float)/df['Duration'].astype(float))*df['Probability'].astype(float)
df
Client Stage Probability Dealsize Duration Start_period Weighted_revenue
0 A suspect 0.25 1200 6 2020-08 50.0
1 B prospect 0.60 1000 4 2020-10 150.0
然后,我们使用列表推导 withzip来创建基于'Start_period'和'Duration'列的日期范围
df['Period']=[pd.date_range(x, periods=y, freq="M").strftime('%Y-%m') for x,y in zip(df["Start_period"], df["Duration"])]
df
Client Stage Probability Dealsize Duration Start_period Weighted_revenue Period
0 A suspect 0.25 1200 6 2020-08 50.0 [2020-08, 2020-09, 2020-10, 2020-11, 2020-12, 2021-01]
1 B prospect 0.60 1000 4 2020-10 150.0 [2020-10, 2020-11, 2020-12, 2021-01]
最后我们使用explode扩展列表:
df=df.explode('Period')
df
Client Stage Probability Dealsize Duration Start_period Weighted_revenue Period
0 A suspect 0.25 1200 6 2020-08 50.0 2020-08
0 A suspect 0.25 1200 6 2020-08 50.0 2020-09
0 A suspect 0.25 1200 6 2020-08 50.0 2020-10
0 A suspect 0.25 1200 6 2020-08 50.0 2020-11
0 A suspect 0.25 1200 6 2020-08 50.0 2020-12
0 A suspect 0.25 1200 6 2020-08 50.0 2021-01
1 B prospect 0.60 1000 4 2020-10 150.0 2020-10
1 B prospect 0.60 1000 4 2020-10 150.0 2020-11
1 B prospect 0.60 1000 4 2020-10 150.0 2020-12
1 B prospect 0.60 1000 4 2020-10 150.0 2021-01
添加回答
举报