如何将列表作为标题并将列表的列表附加到标题下方?

问题描述 投票:0回答:1

我有一个包含一些元素的列表。 列表1 = ['a','b','c','d'] 列表 2 = [['21']' ['23','56','78','67'], ['12','13','14'], ['21','56' ,'78','68']]

我需要将此列表作为数据帧,其中列表 1 作为标题,列表 2 附加到相应的列表 1 索引中。

我想得到这样的输出 df 。

a    b   c   d

21  23  12  21

    56  13  56

    78  14  78

    67      68
python pandas excel dataframe append
1个回答
0
投票

使用

itertools.zip_longest

from itertools import zip_longest

List1 = ['a', 'b', 'c', 'd']
List2 = [['21'], ['23','56','78','67'], ['12','13','14'], ['21','56','78','68']]

out = pd.DataFrame(zip_longest(*List2), columns=List1)

输出:

      a   b     c   d
0    21  23    12  21
1  None  56    13  56
2  None  78    14  78
3  None  67  None  68
© www.soinside.com 2019 - 2024. All rights reserved.