pandas 熊猫合并索引不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27281734/
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
pandas merge on index not working
提问by Tahnoon Pasha
I have two dataframes (Series actually) generated by a groupby operation:
我有两个由 groupby 操作生成的数据帧(实际上是系列):
bw
bw
l1
Consumer Discretionary 0.118718
Consumer Staples 0.089850
Energy 0.109988
Financials 0.159418
Health Care 0.115060
Industrials 0.109078
Information Technology 0.200392
Materials 0.035509
Telecommunications Services 0.030796
Utilities 0.031190
dtype: float64
and pw
和 pw
l1
Consumer Discretionary 0.148655
Consumer Staples 0.067873
Energy 0.063899
Financials 0.095689
Health Care 0.116015
Industrials 0.181346
Information Technology 0.117715
Materials 0.043155
Telecommunications Services 0.009550
Utilities 0.156103
dtype: float64
When I try and mergethem using
当我尝试和merge他们使用
pd.merge(bw,pw,left_index=True,right_index=True)
pd.merge(bw,pw,left_index=True,right_index=True)
I get an error
我收到一个错误
Traceback (most recent call last):
File "/usr/lib/python2.7/dist-packages/IPython/core/interactiveshell.py", line 2883, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-174-739bb362e06d>", line 1, in <module>
pd.merge(pw,attr,left_index=True,right_index=True)
File "/usr/lib/python2.7/dist-packages/pandas/tools/merge.py", line 39, in merge
return op.get_result()
File "/usr/lib/python2.7/dist-packages/pandas/tools/merge.py", line 185, in get_result
join_index, left_indexer, right_indexer = self._get_join_info()
File "/usr/lib/python2.7/dist-packages/pandas/tools/merge.py", line 251, in _get_join_info
left_ax = self.left._data.axes[self.axis]
IndexError: list index out of range
but when I do
但是当我这样做的时候
bw = bw.reset_index()
pw = pw.reset_index()
mrg = pd.merge(pw,bw,on="l1")
It works. It makes my code much less readable over multiple iterations of joins however so I'd like to know what I'm doing wrong and how I can get the first version of the code merging on indexesto work.
有用。但是,它使我的代码在多次连接迭代中的可读性大大降低,因此我想知道我做错了什么以及如何使代码的第一个版本merging on indexes正常工作。
Thanks
谢谢
回答by Robbie Liu
Change the series into DataFrame then it is possible to merge
把系列改成DataFrame就可以合并了
merged = pd.merge(pd.DataFrame(bw),pd.DataFrame(pw),left_index=True,right_index=True)
print(merged)
The result:
结果:
0_x 0_y
l1
Consumer Discretionary 0.118718 0.118718
Consumer Staples 0.089850 0.089850
Energy 0.109988 0.109988
Financials 0.159418 0.159418
Health Care 0.115060 0.115060
Industrials 0.109078 0.109078
Information Technology 0.200392 0.200392
Materials 0.035509 0.222509
Telecommunications Services 0.030796 0.030796
Utilities 0.031190 0.031190
Or if the merge is to be performed in a parallel manner (bw and pw have the same index, same number of items).
或者如果要以并行方式执行合并(bw 和 pw 具有相同的索引,相同的项目数)。
c = zip(bw.tolist(),pw.tolist())
merged = pd.DataFrame(c, index=bw.index)
should have the same result.
应该有相同的结果。
When you reset_index()a series, it turns to a DataFrame (index to column). That is why you can merge after that.
当您创建reset_index()一个系列时,它会变成一个 DataFrame(索引到列)。这就是为什么你可以在那之后合并。

