1

Suppose we have some lists lst1 and lst2 and we want to create a data frame from them. So:

lst1 = ['Apple', 'Orange']
lst2 = []

When I try to create a data frame from these lists, it is empty:

import pandas as pd
df_output = pd.DataFrame(list(zip(lst1, lst2)),
               columns = ["lst1", "lst2" ])

Is there any easy way to add the lst2 column even though it is empty?

stackguy1723
  • 165
  • 1
  • 2
  • 12
  • Do you want the empty list to be the second column or the second row? – ddejohn Oct 12 '21 at 15:45
  • @ddejohn: second column – stackguy1723 Oct 12 '21 at 15:46
  • Replace zip with zip_longest from itertools. – ayhan Oct 12 '21 at 15:51
  • @ayhan while that's a more general solution, I don't think it's necessary here when `pd.Series` can do the job sufficiently well. – ddejohn Oct 12 '21 at 15:52
  • 1
    @ddejohn I don't really agree that pd.Series does the job really well here because you end up calling transpose. That's one of the most inefficient methods in pandas and brings a lot of problems (your numeric columns will turn into objects etc). I am ok with reopening though. – ayhan Oct 12 '21 at 15:55
  • That's a fair assessment, assuming OP's use-case is actually more complex than this, which would be a fair assumption. – ddejohn Oct 12 '21 at 15:57
  • @ayhan I think you should add your comments as an answer. – ddejohn Oct 12 '21 at 15:59

1 Answers1

1

There may be a more appropriate way to do this, but this'll work:

>>> pd.DataFrame(map(pd.Series, (lst1, lst2)), index=["lst1", "lst2"]).T
     lst1 lst2
0   Apple  NaN
1  Orange  NaN
ddejohn
  • 8,775
  • 3
  • 17
  • 30