Kvothe Kvothe - 1 year ago 113
Python Question

Create a dataframe of permutations in pandas from list

I have the following lists:

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

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

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

Appreciate any help!

Answer Source

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'])
      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

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download