2016-07-04 250 views
1

我有一個多指標分組數據框大熊貓類似如下:CONCAT多指標大熊貓據幀列

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的第一列的值到第二列?這比「How to concat Pandas dataframe columns」更困難,因爲涉及多級數據/分層索引/ MultiIndex。

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) 
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 

或者,也許你想保留未改變的浮點值,只是摺疊多指標。這被回答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

新連接在一起的multiindex沒有列標題。我怎樣才能給新加入的multiindex一個列標題(名稱)? Thx – xpt