[英]How can I properly use pivot on this pandas dataframe?
我有以下 df:
Item Service Damage Type Price
A Fast 3.5 1 15.48403728
A Slow 3.5 1 17.41954194
B Fast 5 1 19.3550466
B Slow 5 1 21.29055126
C Fast 5.5 1 23.22605592
and so on
我想把它變成這種格式:
Item Damage Type Price_Fast Price_slow
所以第一行是:
Item Damage Type Price_Fast Price_slow
A 3.5 1 15.4840.. 17.41954...
我試過:
df.pivot(index=['Item', 'Damage', 'Type'],columns='Service', values='Price')
但它拋出了這個錯誤:
ValueError: Length of passed values is 2340, index implies 3
准確獲得您想要使用的數據框布局
dfData = dfRaw.pivot_table(index=['Item', 'Damage', 'Type'],columns='Service', values='Price')
就像@CJR 建議的,然后是
dfData.reset_index(inplace=True)
展平數據框和
dfData.rename(columns={'Fast': 'Price_fast'}, inplace=True) dfData.rename(columns={'Slow': 'Price_slow'}, inplace=True)
以獲得您想要的列名。
然后使用
dfNew.columns = dfNew.columns.values
擺脫自定義索引標簽,你就完成了(感謝@Akaisteph7 指出我沒有完全完成我以前的解決方案。)
您可以使用以下代碼執行此操作:
# You should use pivot_table as it handles multiple column pivoting and duplicates aggregation
df2 = df.pivot_table(index=['Item', 'Damage', 'Type'], columns='Service', values='Price')
# Make the pivot indexes back into columns
df2.reset_index(inplace=True)
# Change the columns' names
df2.rename(columns=lambda x: "Price_"+x if x in ["Fast", "Slow"] else x, inplace=True)
# Remove the unneeded column Index name
df2.columns = df2.columns.values
print(df2)
輸出:
Item Damage Type Price_Fast Price_Slow
0 A 3.5 1 15.484037 17.419542
1 B 5.0 1 19.355047 21.290551
2 C 5.5 1 23.226056 NaN
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.