如何将大熊猫DataFrame中的元组行扩展为多行作为多索引?

时间:2020-04-09 07:17:19

标签: python pandas dataframe

示例DataFrame:

>>> idx = pd.MultiIndex.from_arrays([['foo', 'foo', 'bar', 'bar'], ['one', 'two', 'one', 'two']])
>>> df = pd.DataFrame({'Col1': [('a', 'b'), 'c', 'd', 'e'], 'Col2': [('A', 'B'), 'C', 'D', 'E']}, index=index)
>>> print(df)
           Col1    Col2
foo one  (a, b)  (A, B)
    two       c       C
bar one       d       D
    two       e       E

我想通过拆开元组的行来变换DataFrame,同时将所有内容保持在其原始索引下,结果如下:

          Col1 Col2
foo one 0    a    A
        1    b    B
    two 0    c    C
bar one 0    d    D
    two 0    e    E

我可以很好地打开元组的包装,但是我很难确定如何将新行重新插入到DataFrame中。这是我已经尝试过的示例:

>>> unpacked = pd.DataFrame(df.loc['foo', 'one'].tolist(), index=df.columns).T
>>> print(unpacked)
  Col1 Col2
0    a    A
1    b    B
>>> df.loc['foo', 'one'] = unpacked
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Program Files\Python37\lib\site-packages\pandas\core\indexing.py", line 190, in __setitem__
    self._setitem_with_indexer(indexer, value)
  File "C:\Program Files\Python37\lib\site-packages\pandas\core\indexing.py", line 645, in _setitem_with_indexer
    value = self._align_frame(indexer, value)
  File "C:\Program Files\Python37\lib\site-packages\pandas\core\indexing.py", line 860, in _align_frame
    raise ValueError('Incompatible indexer with DataFrame')
ValueError: Incompatible indexer with DataFrame

很明显为什么失败了,但是我不确定从这里去哪里。是否可以在此过程中创建新的MultiIndex级别,以处理任意数量的未打包行?

1 个答案:

答案 0 :(得分:1)

在列表理解中使用Series.explode使用concat,然后通过GroupBy.cumcount添加新级别:

df = pd.concat([df[x].explode() for x in df.columns], axis=1)
df = df.set_index(df.groupby(df.index).cumcount(), append=True)
print (df)
          Col1 Col2
foo one 0    a    A
        1    b    B
    two 0    c    C
bar one 0    d    D
    two 0    e    E