2014-12-03 2 views
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 

내가 시도

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 

pwmerge을 를 사용하여

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.

감사

+1

흥미로운 점은 나에게 잘 들립니다. 팬더의 어떤 버전을 사용하고 있습니까? 또한, 당신은 시도 할 수 있습니다 dataframe.join() ... bw.join (pw) –

+0

안녕하세요 @ BobHaffner 자사의 우분투 저장소에서 팬더 내가 생각하는 0.14.1입니다. bw.join (pw)은 오류 'AttributeError :'Series '객체에'join '속성이 없기 때문에 병합 경로를 따라 가고 있습니다. –

+1

좋습니다. 나는 그 조인이 유일한 일이라는 것을 잊었다. –

답변

8

변경 DataFrame로 시리즈 그럼 가능

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() 시리즈는 데이터 프레임 (인덱스 열)으로 바뀝니다. 그래서 그 후에 병합 할 수 있습니다.