简体   繁体   中英

Creating a data frame from a list of lists with empty lists

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM