我正在嘗試使用熊貓為資料透視表撰寫代碼,
這是我的示例 excel 檔案。
Name Owner Role Can Edit Can Read Can Delete Can download
John Julan Role1 Yes No No No
Ricard Julan Role2 No Yes No No
Sam Hannah Role2 No No Yes No
Julia Hannah Role1 No No No Yes
Katie Julan Role2 No Yes No Yes
和輸出應該是這樣的:

這是我的代碼
import pandas as pd
import numpy as np
df = pd.read_excel('pivot.xlsx')
table = pd.pivot_table(df, values=['Can Edit','Can Read','Can Delete','Can download'], index=['Owner'],columns=['Role'], aggfunc=np.sum)
但我沒有得到想要的結果
uj5u.com熱心網友回復:
您幾乎擁有它,但您在索引中忘記了“名稱”:
pd.pivot_table(df, index=['Name', 'Owner'], columns=['Role'],
values=['Can Edit','Can Read','Can Delete','Can download'],
aggfunc=np.sum)
請注意,如果使用了所有其他列,則無需指定值:
pd.pivot_table(df, index=['Name', 'Owner'], columns=['Role'], aggfunc=np.sum)
輸出:
Can Delete Can Edit Can Read Can download
Role Role1 Role2 Role1 Role2 Role1 Role2 Role1 Role2
Name Owner
John Julan No NaN Yes NaN No NaN No NaN
Julia Hannah No NaN No NaN No NaN Yes NaN
Katie Julan NaN No NaN No NaN Yes NaN Yes
Ricard Julan NaN No NaN No NaN Yes NaN No
Sam Hannah NaN Yes NaN No NaN No NaN No
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/454547.html
上一篇:pandas中的高級資料框重塑
下一篇:1個月前的值滯后
