Reputation: 181
I have to create Data frame with two columns in which df['col1']
rows should repeat as many times as other column df['col2']
. This should repeat many times depends on list data.
Df:
col1 col2
Fruit Apple
Fruit Mango
Fruit banana
Place US
Place UK
Place France
Place India
Where I have data in following format
List1 =["fruit", "place"]
List2 =[["Apple", "Mango", "Banana"], ["US", "UK", "France", "India"]]
Note: not for loop approach bcs I have 2 million rows
Upvotes: 1
Views: 121
Reputation: 38415
Here is a solution using numpy.repeat to repeat elements in list1 and itertools.chain.from_iterable to flatten the nested list,
list1 =['fruit', 'place']
list2 =[['Apple', 'Mango', 'Banana'], ['US', 'UK', 'France', 'India']]
import itertools
import numpy as np
l1 = np.repeat(np.array(list1), [len(i) for i in list2])
l2 = np.array(list(itertools.chain.from_iterable(list2)))
pd.DataFrame({'col1':l1, 'col2':l2})
col1 col2
0 fruit Apple
1 fruit Mango
2 fruit Banana
3 place US
4 place UK
5 place France
6 place India
Upvotes: 1