本文介紹了Pandas pivot 產生“ValueError:索引包含重復的條目,無法重塑";的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
我有一個格式如下的熊貓表:
I have a pandas table formatted as following:
anger_metric metric_name angle_value
0 71.0991 roll 14.6832
1 71.0991 yaw 0.7009
2 71.0991 pitch 22.5075
3 90.1341 roll 4.8566
4 90.1341 yaw 6.4458
5 90.1341 pitch -10.1930
我需要創建一個這樣的視圖,將其旋轉成這樣:
I need to create a view of this that pivots it to sth like this:
anger_metric roll yaw pitch
0 71.0991 14.6832 0.7009 22.5075
1 90.1341 4.8566 6.4458 -10.1930
這是我的代碼:
df2= results.pivot(index='anger_metric', columns='metric_name', values='angle_value')
# results is the pnadas table/list
我收到以下錯誤:
ValueError: Index contains duplicate entries, cannot reshape
如何處理?
推薦答案
試試pivot_table
:
df
anger_metric metric_name angle_value
0 71.0991 roll 14.6832
1 71.0991 yaw 0.7009
2 71.0991 pitch 22.5075
3 90.1341 roll 4.8566
4 90.1341 yaw 6.4458
5 90.1341 pitch -10.1930
result = df.pivot_table(index='anger_metric',
columns='metric_name',
values='angle_value')
result.columns.name = None
result
pitch roll yaw
anger_metric
71.0991 22.5075 14.6832 0.7009
90.1341 -10.1930 4.8566 6.4458
這篇關于Pandas pivot 產生“ValueError:索引包含重復的條目,無法重塑";的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!