ピボットテーブルをデータフレームに変換したうえで差分をとってはいかがでしょうか?
参考:pandas pivot table to data frame [duplicate]
Pyton
1import pandas as pd
2
3DF = pd.DataFrame({'Campaign ID':[1,2,3,4], 'Campaign name':['a','b','c','d'], 'Time period':['2020-04-01','2020-04-01','2020-04-08','2020-04-08'],'Spend':[10,20,30,40]})
4
5DF=DF[(DF['Time period']=='2020-04-01')| (DF['Time period']=='2020-04-08')].groupby(['Campaign ID','Campaign name','Time period']).sum()
6Pivot=DF.pivot_table(index=['Campaign ID','Campaign name'],columns=['Time period'], values=['Spend'],aggfunc='sum').round(2).fillna(0)
7print(Pivot)
8"""
9 Spend
10Time period 2020-04-01 2020-04-08
11Campaign ID Campaign name
121 a 10.0 0.0
132 b 20.0 0.0
143 c 0.0 30.0
154 d 0.0 40.0
16"""
17
18df = pd.DataFrame(Pivot.to_records())
19
20# 列名を日付部分のみに
21new_cols = {}
22for c in df.columns[2:]: # 'Campaign ID','Campaign name'を除いた列
23 new_c = c.strip('()')
24 new_c = new_c.split(',')
25 new_cols[c] = new_c[1].strip(' \'')
26df = df.rename(columns=new_cols)
27
28df['diff'] = df.iloc[:,3] - df.iloc[:,2]
29print(df)
30"""
31 Campaign ID Campaign name 2020-04-01 2020-04-08 diff
320 1 a 10.0 0.0 -10.0
331 2 b 20.0 0.0 -20.0
342 3 c 0.0 30.0 30.0
353 4 d 0.0 40.0 40.0
36"""
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/07/18 02:51
2020/07/18 02:57