Pandas 枢轴产生“ValueError:索引包含重复条目,无法重塑”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/46528599/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 04:33:59 来源:igfitidea点击:
Pandas pivot produces "ValueError: Index contains duplicate entries, cannot reshape"
提问by A_Matar
I have a pandas table formatted as following:
我有一个格式如下的Pandas表:
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
Here is my code:
这是我的代码:
df2= results.pivot(index='anger_metric', columns='metric_name', values='angle_value')
# results is the pnadas table/list
I get the following error:
我收到以下错误:
ValueError: Index contains duplicate entries, cannot reshape
How to handle this?
如何处理?
回答by cs95
Try pivot_table
:
尝试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
回答by YOBEN_S
By using unstack
通过使用 unstack
df.groupby(['anger_metric','metric_name'])['angle_value'].sum().unstack(-1)# you can using `mean` instead of `sum`
Out[433]:
metric_name pitch roll yaw
anger_metric
71.0991 22.5075 14.6832 0.7009
90.1341 -10.1930 4.8566 6.4458