How to make a list as headers and append the lists of list to under the headers?

65 Views Asked by At

I have a list with some elements.

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

I need to make this lists as dataframe with list 1 as header and List 2 to append within corresponding list1 index.

I would like to get output df like this.

    a    b   c   d
0   21  23  12  21
1       56  13  56
2       78  14  78
3       67      68
1

There are 1 best solutions below

2
mozway On

Use itertools.zip_longest and the DataFrame constructor:

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)

Output:

      a   b     c   d
0    21  23    12  21
1  None  56    13  56
2  None  78    14  78
3  None  67  None  68

For a custom object for empty cells, specify a fillvalue:

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

Output:

    a   b   c   d
0  21  23  12  21
1      56  13  56
2      78  14  78
3      67      68

Alternatively, as suggested by @LukasHestermeyer you could also set up the DataFrame with List1 as index and transpose:

out = pd.DataFrame(List2, index=List1).T

The difference is that transposing after the fact can mess up the dtypes.

Assuming for instance:

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

Then:

pd.DataFrame(zip_longest(*List2), columns=List1).dtypes
a    float64
b     object
c    float64
d      int64
dtype: object

pd.DataFrame(List2, index=List1).T.dtypes
a    object
b    object
c    object
d    object
dtype: object

You could however chain convert_dtypes as a workaround:

out = pd.DataFrame(List2, index=List1).T.convert_dtypes()

out.dtypes
a             Int64
b    string[python]
c             Int64
d             Int64
dtype: object