创建具有多个记录的数据框

时间:2019-02-11 11:53:31

标签: python python-3.x pandas dataframe

我有以下三个列表,并希望创建一个数据框:

list1 = [1,2,3,4,5]
list2 = [1,2,3,4,5,6,7,8,9]
list3 = [1,2,3,4,5,6,7]

是否有可能创建一个大小为list1 * list2 * list3(315行)的值的数据框:

list1   list2   list3
1       1       1
1       1       2
1       1       3
1       1       4
1       1       5
1       1       6
1       1       7
1       2       1
1       2       2
and so on.

我在这里坚持逻辑。如果有人可以帮助的话,那将真的很有帮助。

谢谢

1 个答案:

答案 0 :(得分:3)

使用itertools.product()返回笛卡尔积:

import pandas as pd
from itertools import product

x = list(product(list1,list2,list3))

df = pd.DataFrame(x)

print(df)