5
我有兩個dataframes(實際上是系列)由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
當我嘗試和merge
他們使用
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")
它的工作原理。它使我的代碼在多次連接迭代中可讀性差得多,但是我想知道我在做什麼錯誤,以及如何讓代碼merging on indexes
的第一版工作。
由於
這很有趣,它看起來很好。你使用的是什麼版本的熊貓?此外,你可以嘗試dataframe.join()... bw.join(pw) – 2014-12-03 22:34:04
嗨@BobHaffner它的熊貓在Ubuntu的存儲庫,我認爲是0.14.1。 bw.join(pw)給出一個錯誤'AttributeError:'Series'對象沒有屬性'join''這就是爲什麼我要走合併的道路...... – 2014-12-04 00:27:59
好吧,拍攝。我忘記了這個連接是唯一的東西 – 2014-12-04 01:06:37