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
Use
itertools.zip_longestand theDataFrameconstructor:Output:
For a custom object for empty cells, specify a
fillvalue:Output:
Alternatively, as suggested by @LukasHestermeyer you could also set up the DataFrame with List1 as index and
transpose:The difference is that transposing after the fact can mess up the dtypes.
Assuming for instance:
Then:
You could however chain
convert_dtypesas a workaround: