我已经将一个csv文件导入到带有销售管道数据的Pandas DataFrame中。每一条线都代表着潜在的名称、产品信息、管道阶段、可能性、预期的交易规模、预期的结束日期、持续时间等。
现在,我想把它转换成一个销售预测,根据这个预测,我想通过将交易规模除以概率的持续时间来计算每个期间的平均收入。然后根据预期的关闭日期和持续时间为所有可能的期间创建一行。
我创造了一个简单的例子来支持我的问题:
import pandas as pd
pipeline_data = [{'Client': 'A', 'Stage': 'suspect', 'Probability': '0.25', 'Dealsize': '1200', 'Duration': 6, 'Start_period': '2020-08'}, {'Client': 'B', 'Stage': 'prospect', 'Probability': '0.60', 'Dealsize': '1000', 'Duration': 4, 'Start_period': '2020-10'}]
df = pd.DataFrame(pipeline_data)
df输出:
Client Stage Probability Dealsize Duration Start_period
0 A suspect 0.25 1200 6 2020-08
1 B prospect 0.60 1000 4 2020-10因此,客户每月的平均收入是1200 /6* 0.25 = 50。收入将在2020年至08-2021-01年间下降(因此从2020年8月至2021年1月)。
最好的产出是:
Client Stage Probability Dealsize Duration Start_period Weighted_revenue Period
0 A suspect 0.25 1200 6 2020-08 50 2020-08
1 A suspect 0.25 1200 6 2020-08 50 2020-09
2 A suspect 0.25 1200 6 2020-08 50 2020-10
3 A suspect 0.25 1200 6 2020-08 50 2020-11
4 A suspect 0.25 1200 6 2020-08 50 2020-12
5 A suspect 0.25 1200 6 2020-08 50 2021-01
6 B prospect 0.60 1000 4 2020-10 150 2020-10
7 B prospect 0.60 1000 4 2020-10 150 2020-11
8 B prospect 0.60 1000 4 2020-10 150 2020-12
9 B prospect 0.60 1000 4 2020-10 150 2021-01我已经将Start_period转换为句点类型,因此可以使用它来计算/迭代。
我对编码很陌生。我试着在这个网站和其他网站上找到答案,但直到现在还没有成功。我可以想象使用某种嵌套循环和附加函数来解决这个问题,但是我不知道如何在Pandas中使用它.
任何帮助都将不胜感激!
发布于 2020-08-12 16:56:42
您可以尝试使用列表理解,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然后,我们使用列表理解和zip一起创建基于'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-01https://stackoverflow.com/questions/63381051
复制相似问题