Suppose we have some lists lst1
and lst2
and we want to create a data frame from them. So:
JavaScript
x
3
1
lst1 = ['Apple', 'Orange']
2
lst2 = []
3
When I try to create a data frame from these lists, it is empty:
JavaScript
1
4
1
import pandas as pd
2
df_output = pd.DataFrame(list(zip(lst1, lst2)),
3
columns = ["lst1", "lst2" ])
4
Is there any easy way to add the lst2
column even though it is empty?
Advertisement
Answer
There may be a more appropriate way to do this, but this’ll work:
JavaScript
1
5
1
>>> pd.DataFrame(map(pd.Series, (lst1, lst2)), index=["lst1", "lst2"]).T
2
lst1 lst2
3
0 Apple NaN
4
1 Orange NaN
5