Pandas在索引上合并不起作用

12

我有两个由 groupby 操作生成的数据帧(实际上是序列):

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

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

当我尝试使用 pd.merge(bw,pw,left_index=True,right_index=True) 合并它们时,我会收到一个错误提示。

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

但是当我这样做的时候

bw = bw.reset_index()
pw = pw.reset_index()
mrg = pd.merge(pw,bw,on="l1")

它可以用。但是经过多次连接后,我的代码变得难以阅读,所以我想知道我做错了什么,以及如何使第一个版本的代码在索引上合并有效。

谢谢。


1
很有趣,我觉得它看起来不错。你用的是哪个 pandas 版本?另外,你可以尝试 dataframe.join()... bw.join(pw) - Bob Haffner
嗨@BobHaffner,我认为Ubuntu存储库中的pandas版本是0.14.1。bw.join(pw)会出现错误AttributeError: 'Series' object has no attribute 'join',这就是为什么我要走合并路径的原因... - Tahnoon Pasha
1
好的,开始吧。我忘记了join只能在数据框中使用。 - Bob Haffner
1个回答

18

将该系列转换为数据帧,然后才能进行合并。

merged = pd.merge(pd.DataFrame(bw),pd.DataFrame(pw),left_index=True,right_index=True)
print(merged)

结果:

                                 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

或者如果要以并行方式执行合并(bw和pw具有相同的索引,相同数量的项)。

c = zip(bw.tolist(),pw.tolist())
merged = pd.DataFrame(c, index=bw.index)
当你使用reset_index()函数对一个序列进行操作时,它会变成一个DataFrame(即将索引变为列),这就是为什么在此之后可以进行合并操作的原因,两者应该具有相同的结果。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接