从列表创建 pandas 排列的数据框

问题描述:

我有以下列表:

aa = ['aa1', 'aa2', 'aa3', 'aa4', 'aa5']
bb = ['bb1', 'bb2', 'bb3', 'bb4', 'bb5']
cc = ['cc1', 'cc2', 'cc3', 'cc4', 'cc5']

我想这样创建一个熊猫数据框:

I want to create a pandas dataframe as such:

aa    bb    cc
aa1   bb1   cc1
aa2   bb1   cc1
aa3   bb1   cc1
aa4   bb1   cc1
aa5   bb1   cc1
aa1   bb2   cc1
aa1   bb3   cc1
aa1   bb4   cc1
aa1   bb5   cc1
aa1   bb1   cc2
aa1   bb1   cc3
aa1   bb1   cc4
aa1   bb1   cc5

我对如何执行此操作感到困惑. 我看了一些例子: 如何在Python中生成列表的所有排列

I'm stuck as to how to do this. I've looked at examples: How to generate all permutations of a list in Python

我可以使用以下方法分别进行每个排列:

I can do each permutation individually using:

import itertools
itertools.permutations(['aa1','aa2','aa3','aa4','aa5'])

我有几十个列表,理想情况下,我想自动执行.

I have a few tens of lists and ideally, I'd like to do them automatically.

感谢任何帮助!

我相信您需要itertools.product,而不是permutations.

I believe you need itertools.product, not permutations.

In [287]: lists = [aa, bb, cc]

In [288]: pd.DataFrame(list(itertools.product(*lists)), columns=['aa', 'bb', 'cc'])
Out[288]: 
      aa   bb   cc
0    aa1  bb1  cc1
1    aa1  bb1  cc2
2    aa1  bb1  cc3
3    aa1  bb1  cc4
4    aa1  bb1  cc5
5    aa1  bb2  cc1
6    aa1  bb2  cc2
7    aa1  bb2  cc3
8    aa1  bb2  cc4
...

这将为您提供列表的笛卡尔积.到目前为止,列名已经过硬编码,但是您可以使用df.rename动态重命名它们.

This will give you the Cartesian product of your lists. As of now, the column names are hardcoded, but you can use df.rename to dynamically rename them.