2016-07-04 2 views
1

내가 가지고 그룹화 MultiIndex 같은 dataframe을 판다 DataFrame 열을 판다 다음CONCAT MultiIndex는

In [10]: arrays = [np.array(['bar', 'bar', 'baz', 'baz', 'foo', 'foo', 'qux', 'qux']), 
    ....:   np.array(['one', 'two', 'one', 'two', 'one', 'two', 'one', 'two'])] 
    ....: 

In [11]: s = pd.Series(np.random.randn(8), index=arrays) 

In [12]: s 
Out[12]: 
bar one -0.861849 
    two -2.104569 
baz one -0.494929 
    two 1.071804 
foo one 0.721555 
    two -0.706771 
qux one -1.039575 
    two 0.271860 

가 어떻게 두 번째 컬럼에 첫 번째 열 값을 CONCAT 수 있습니까? 다중 레벨 데이터/계층 적 인덱싱/MultiIndex가 포함되어 있기 때문에 "How to concat Pandas dataframe columns"보다 어렵습니다.

UPDATE :

내 실제 데이터가 실제로 적절한 이름, 데이터베이스에서 제공됩니다. 트릭은 여전히 ​​내 말에 작동하지 :

p['Details']= p.index.to_series().str.join(' ') + ' ' + p.astype(str) 
    File "D:\Programs\Anaconda3\lib\site-packages\pandas\core\ops.py", line 995, i 
n f 
    return self._combine_series(other, na_op, fill_value, axis, level) 
    File "D:\Programs\Anaconda3\lib\site-packages\pandas\core\frame.py", line 3446 
, in _combine_series 
    return self._combine_series_infer(other, func, level=level, fill_value=fill_ 
value) 
    File "D:\Programs\Anaconda3\lib\site-packages\pandas\core\frame.py", line 3457 
, in _combine_series_infer 
    return self._combine_match_columns(other, func, level=level, fill_value=fill 
_value) 
    File "D:\Programs\Anaconda3\lib\site-packages\pandas\core\frame.py", line 3469 
, in _combine_match_columns 
    left, right = self.align(other, join='outer', axis=1, level=level, copy=Fals 
e) 
    File "D:\Programs\Anaconda3\lib\site-packages\pandas\core\frame.py", line 2679 
, in align 
    fill_axis=fill_axis, broadcast_axis=broadcast_axis) 
    File "D:\Programs\Anaconda3\lib\site-packages\pandas\core\generic.py", line 37 
84, in align 
    fill_axis=fill_axis) 
    File "D:\Programs\Anaconda3\lib\site-packages\pandas\core\generic.py", line 38 
65, in _align_series 
    return_indexers=True) 
    File "D:\Programs\Anaconda3\lib\site-packages\pandas\core\index.py", line 2233 
, in join 
    return self._join_multi(other, how=how, return_indexers=return_indexers) 
    File "D:\Programs\Anaconda3\lib\site-packages\pandas\core\index.py", line 2326 
, in _join_multi 
    raise ValueError("cannot join with no level specified and no overlapping nam 
es") 
ValueError: cannot join with no level specified and no overlapping names 

이제 집에 갈. 내일 추적 할거야.

감사합니다.

답변

1

처음 두 열은 실제로 시리즈 개체의 인덱스입니다.

s.index.to_series().str.join(' ') + ' ' + s.astype(str) 
s.index.to_series().str.join(' ') + ' ' + s.astype(str) 

bar one  bar one -1.29416824528 
    two bar two -0.417249293315 
baz one baz one -0.474058653156 
    two baz two -0.941660942375 
foo one  foo one -0.41741715261 
    two  foo two 0.739981512301 
qux one  qux one -1.03909641549 
    two  qux two -1.00168469914 
dtype: object 

을 아니면 유엔이 변경 플로트 값을 유지하고 단지 multiindex을 축소하려는 :

s.index.to_series().str.join(' ') + ' ' + s.astype(str) 

이 당신을 가져옵니다. 이것은 here이라고 대답했습니다.

s.index = s.index.to_series().str.join(' ') 

bar one -1.294168 
bar two -0.417249 
baz one -0.474059 
baz two -0.941661 
foo one -0.417417 
foo two 0.739982 
qux one -1.039096 
qux two -1.001685 
dtype: float64 
+0

오, 예. 두 번째 트릭이 작동합니다. 감사! – xpt

+0

np, 집에 돌아가 승리! :-) – piRSquared

+0

새로 가입 된 멀티 인텍스에는 열 머리글이 없습니다. 새로 합류 한 멀티 인덱스에 열 머리글 (이름)을 어떻게 부여 할 수 있습니까? Thx – xpt