[英]Pandas pivot or pivot table?
我正在嘗試使用以下格式獲取 excel 數據框:
DOW Location 7/30/2022 8/6/2022 8/13/2022 8/20/2022
Volumes Saturday North 33 32 29 24
Volumes Saturday South 34 17 30 28
Volumes Sunday North 40 57 25 28
Volumes Sunday South 47 38 32 45
ACT Saturday North 750 1060 1066 1082
ACT Saturday South 545 509 1306 1121
ACT Sunday North 801 860 572 795
ACT Sunday South 622 526 711 491
並以這種格式擁有 output:
DOW_Location Location Date Volumes ACT
Volumes Saturday North North 7/30/2022 33 750
Volumes Sunday North North 7/31/2022 40 801
Volumes Saturday South South 7/30/2022 34 545
Volumes Sunday South South 7/31/2022 47 622
我想我可以在通過組合df['DOW'] + df['Location']
創建唯一索引后執行df.pivot()
並從df['DOW']
中刪除工作日以創建df['Category']
列。 我的問題是,當值跨越多個列時,我不知道如何填充 pivot 中的值。
所以我不確定這里的“值”參數是什么:
df = df.pivot(index='DOW_Location', columns='Category', values=?)
如果 pivot 不是這里的理想解決方案,那么另一種方法是什么?
# split the DOW into columns
df[['col','day']]=df['DOW'].str.split(' ',expand=True)
# melt to bring the dates as rows
df2=df.melt(id_vars=['DOW','Location','col','day'], var_name='date')
#pivot and sum
(df2.pivot_table(index=['day','Location','date'],
columns=['col'],aggfunc=sum)
.reset_index())
day Location date value
col ACT Volumes
0 Saturday North 7/30/2022 750 33
1 Saturday North 8/13/2022 1066 29
2 Saturday North 8/20/2022 1082 24
3 Saturday North 8/6/2022 1060 32
4 Saturday South 7/30/2022 545 34
5 Saturday South 8/13/2022 1306 30
6 Saturday South 8/20/2022 1121 28
7 Saturday South 8/6/2022 509 17
8 Sunday North 7/30/2022 801 40
9 Sunday North 8/13/2022 572 25
10 Sunday North 8/20/2022 795 28
11 Sunday North 8/6/2022 860 57
12 Sunday South 7/30/2022 622 47
13 Sunday South 8/13/2022 711 32
14 Sunday South 8/20/2022 491 45
15 Sunday South 8/6/2022 526 38
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.