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
現在回家。明天會跟進。
謝謝
哦,是的。第二個技巧的作品。謝謝! – xpt
np,回家,贏了! :-) – piRSquared
新連接在一起的multiindex沒有列標題。我怎樣才能給新加入的multiindex一個列標題(名稱)? Thx – xpt