Efficient way to unnest (explode) multiple list columns in a pandas DataFrame

前端 未结 4 567
天涯浪人
天涯浪人 2020-11-27 14:43

I am reading multiple JSON objects into one DataFrame. The problem is that some of the columns are lists. Also, the data is very big and because of that I cannot use the ava

4条回答
  •  清歌不尽
    2020-11-27 15:02

    Use set_index on A and on remaining columns apply and stack the values. All of this condensed into a single liner.

    In [1253]: (df.set_index('A')
                  .apply(lambda x: x.apply(pd.Series).stack())
                  .reset_index()
                  .drop('level_1', 1))
    Out[1253]:
        A   B   C   D   E
    0  x1  v1  c1  d1  e1
    1  x1  v2  c2  d2  e2
    2  x2  v3  c3  d3  e3
    3  x2  v4  c4  d4  e4
    4  x3  v5  c5  d5  e5
    5  x3  v6  c6  d6  e6
    6  x4  v7  c7  d7  e7
    7  x4  v8  c8  d8  e8
    

提交回复
热议问题