Home > Back-end >  How can I create a dataframe based on two lists?
How can I create a dataframe based on two lists?

Time:10-07

I want to know how could I create a dataframe based on two list. I have the following lists:

List_time = [1,2,3]
List_item = [a,b,c]

For every item in list_item, I want another column that agregates every time in list_time:

df = [1 a
      1 b
      1 c
      2 a
      2 b
      2 c
      3 a
      3 b
      3 c]

Sorry if it's a very basic question, I'm exhausted right now. Thanks

CodePudding user response:

Try this;

List_time = [1,2,3]
List_item = ["a","b","c"]

n = 3 # times need to repeat
import pandas as pd
df = pd.DataFrame({"List_time":[i for i in List_time for _ in range(n)],
                   "List_item":List_item*n})

#output of df;
   List_time List_item
0          1         a
1          1         b
2          1         c
3          2         a
4          2         b
5          2         c
6          3         a
7          3         b
8          3         c

CodePudding user response:

Use itertools.product

from itertools import product

df = pd.DataFrame(product(List_time, List_item))

CodePudding user response:

I like to use itertools product function for just this purpose. It will combine lists as cross products and Pandas will ingest this nicely.

import itertools
import pandas as pd

a = [1, 2, 3]
b = ['a', 'b', 'c']

df = pd.DataFrame(data=itertools.product(a, b))

Output:

   0  1
0  1  a
1  1  b
2  1  c
3  2  a
4  2  b
5  2  c
6  3  a
7  3  b
8  3  c

Edit: I misread the question, my mistake

  • Related