
问题内容
我有一个 pandas 数据框,如下所示,其中详细说明了对某个区域的其他调用:
commsdate |
area |
day0 incremental |
day1 incremental |
day2 incremental |
01/01/24 |
sales |
43 |
36 |
29 |
01/01/24 |
service |
85 |
74 |
66 |
02/01/24 |
sales |
56 |
42 |
31 |
02/01/24 |
service |
73 |
62 |
49 |
03/01/24 |
sales |
48 |
32 |
24 |
03/01/24 |
service |
67 |
58 |
46 |
我正在尝试按日期计算收到的电话数量,因此 1 月 1 日收到的销售电话将是该日期的 day0_incremental (43),1 月 2 日将是 1 月 2 日的 day0 加上 1 月 1 日的 day1 (36+) 56) 和 1 月 3 日将是 1 月 3 日的 day0 加上 1 月 2 日的 day1 加上 1 月 1 日的 day2 (48+42+29),产生以下数据框:
CallDate |
Sales |
Service |
01/01/24 |
43 |
85 |
02/01/24 |
92 |
147 |
03/01/24 |
119 |
195 |
04/01/24 |
63 |
107 |
05/01/24 |
24 |
46 |
我已经成功地为第二个表创建了数据框的外壳,在区域列下没有值,但不知道接下来的步骤:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | df[ 'commsdate' ] = pd.to_datetime(df[ 'commsdate' ], format= '%d/%m/%y' )
areaunique = df[ 'area' ].unique().tolist()
from datetime import timedelta
calldate = pd.date_range(start=min(df[ 'commsdate' ]), end =max(df[ 'commsdate' ])+timedelta(days=6), freq= 'd' )
data = {area: [] for area in areaunique}
dfnew = pd.dataframe(data)
dfnew[ 'calldate' ] = calldate
dfnew = dfnew.melt(id_vars=[ 'calldate' ], var_name= 'area' )
dfnew = dfnew.pivot(index= 'calldate' , columns= 'area' , values= 'value' )
dfnew = dfnew.reset_index()
dfnew = dfnew[[ 'calldate' ] + areaunique]
|
登录后复制
我已经开始编写 for 循环,但我只做到了这一点:
1 2 | for i in range(1,len(areaunique)+1):
dfnew.columns(i) =
|
登录后复制
正确答案
您可以拨打pivot
、shift
和add
:
1 2 3 4 5 6 7 8 | df[ 'commsdate' ] = pd.to_datetime(df[ 'commsdate' ], dayfirst=true)
tmp = df.pivot(index= 'commsdate' , columns= 'area' )
out = (tmp[ 'day0 incremental' ]
.add(tmp[ 'day1 incremental' ].shift(freq= '1d' ), fill_value=0)
.add(tmp[ 'day2 incremental' ].shift(freq= '2d' ), fill_value=0)
.reset_index().rename_axis(columns=none)
)
|
登录后复制
或者,使用从 dayx …
字符串中提取的数字以编程方式使用 functools.reduce
:
1 2 3 4 5 6 7 8 9 10 11 12 | from functools import reduce
import re
reg = re.compile(r 'day(\d+)' )
df[ 'commsdate' ] = pd.to_datetime(df[ 'commsdate' ], dayfirst=true)
tmp = df.pivot(index= 'commsdate' , columns= 'area' )
out = reduce(lambda a,b: a.add(b, fill_value=0),
(tmp[d].shift(freq=f '{reg.search(d).group(1)}d' ) for d in
tmp.columns.get_level_values(0).unique())
).reset_index().rename_axis(columns=none)
|
登录后复制
输出:
1 2 3 4 5 6 | CommsDate Sales Service
0 2024-01-01 43.0 85.0
1 2024-01-02 92.0 147.0
2 2024-01-03 119.0 195.0
3 2024-01-04 63.0 107.0
4 2024-01-05 24.0 46.0
|
登录后复制
以上是与pandas有条件合并的详细内容。更多信息请关注PHP中文网其他相关文章!