“仅对具有唯一值的索引对象有效的重新索引"; [英] "Reindexing only valid with uniquely valued Index objects"
本文介绍了“仅对具有唯一值的索引对象有效的重新索引";的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当我运行这样的代码时:
When I run code like this:
import pandas as pd
A = pd.DataFrame([('a', -1.374201, 35),
('b', 1.415697, 29),
('a', 0.233841, 18),
('b', 1.550599, 30),
('a', -0.178370, 63),
('b', -1.235956, 42),
('a', 0.088046, 2),
('b', 0.074238, 84)], columns='key value other'.split())
B = A.groupby('key')['value'].mean()
C = pd.DataFrame([('a', 0.469924, 44),
('b', 1.231064, 68),
('a', -0.979462, 73),
('b', 0.322454, 97)], columns='key value other'.split())
D = C.set_index('key')
D['value'] -= B
...最后一行失败并显示错误:
...the last line fails with the error:
Exception: Reindexing only valid with uniquely valued Index objects
我在做什么错了?
推荐答案
如果我正确地遵循了您的示例(感谢您添加示例,顺便说一句),我相信您所需要的很简单:
If I follow your example correctly (thanks for adding it, BTW), I believe what you need is as simple as:
D.sub(B, axis='index')
哪个给我:
In [29]: D.sub(B, axis='index')
Out[29]:
value other
key
a 0.777595 44.307671
a -0.671791 73.307671
b 0.779919 67.548856
b -0.128690 96.548856
如您所见,这弄乱了other
列.如果这是一个问题,那么不幸的是,您又回到了相同的重复索引情况.
As you can see, this messes up the other
column. If that's a problem, you're back in the same duplicate index situation, unfortunately.
这篇关于“仅对具有唯一值的索引对象有效的重新索引";的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文