Reputation: 13
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?
Upvotes: 0
Views: 4836
Reputation: 18416
>>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
Upvotes: 2
Reputation: 13349
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
Upvotes: 0