简体   繁体   中英

Create a Pandas Dataframe from two lists: column 1 is first list, column 2 is second list which is a nested list

I am attempting to use two different lists to create a Pandas dataframe. The first list is to be the first column and the second list is the other column. The second list is nested and I want the first column to repeat values for as many values are in the nested item in the second column.

For example, the two lists:

list1 = ['first', 'second', 'third']

list2 = [['a', 'b', 'c', 'd'], ['e', 'f'], ['g']]

Would return the output

    column1 column2
0   first   b
1   first   c
2   first   d
3   second  e
4   second  f
5   third   g

I have tried using dict(zip(list1, list2) and then using pd.from_dict() but it is not yielding the results I need.

Anyone have any suggestions?

try zip followed by explode :

df = pd.DataFrame(list(zip(list1,list2)), columns=[1,2]).explode(2).reset_index(drop=True).add_prefix('column')

df:

    column1 column2
0   first   a
1   first   b
2   first   c
3   first   d
4   second  e
5   second  f
6   third   g
>>list1
['first', 'second', 'third']
>>list2
[['a', 'b', 'c', 'd'], ['e', 'f'], ['g']]

You can try something like this: First create the dataframe with those lists, then Transpose the dataframe and then finally use explode method.

>>df = pd.DataFrame([list1, list2], index=['column1', 'column2']).T.explode('column2')
>>df
  column1 column2
0   first       a
1   first       b
2   first       c
3   first       d
4  second       e
5  second       f
6   third       g

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