【问题标题】:concat MultiIndex pandas DataFrame columnsconcat MultiIndex pandas DataFrame 列
【发布时间】:2016-11-06 16:12:03
【问题描述】:

我有一个分组的 MultiIndex pandas 数据框,如下所示:

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

如何将第一列的值连接到第二列?这比“How to concat Pandas dataframe columns”更难,因为涉及到多级数据/分层索引/MultiIndex。

更新:

我的实际数据实际上来自数据库,具有正确的名称。这个技巧对我来说仍然不起作用:

  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

现在要回家了。明天会跟进。

谢谢

【问题讨论】:

    标签: python pandas concat


    【解决方案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
    

    【讨论】:

    • np,带着胜利回家吧! :-)
    • new-joined-together-multiindex 没有列标题。如何为新加入的多索引提供列标题(名称)?谢谢
    • 找到了,s.index.name = 'NewName'
    猜你喜欢
    • 2019-02-08
    • 2019-05-20
    • 2017-08-25
    • 1970-01-01
    • 2020-06-15
    • 2019-08-02
    • 2017-03-28
    • 2021-05-18
    • 2023-03-25
    相关资源
    最近更新 更多