Asim
Asim

Reputation: 1480

Python: get combinations of unique values from columns of datafame

I have a dataframe like this:

id  a   b   c   d   e
0   a10 a11 a12 a13 a14
1   a10 a21 a12 a23 a24
2   a30 a21 a12 a33 a14
3   a30 a21 a12 a43 a44
4   a10 a51 a12 a53 a14

and I want all unique lists of combinations of length 'x' from the dataframe. If length is 3 then some of the combinations will be:

[[a10,a11,a12],[a10,a21,a12],[a10,a51,a12],[a30,a11,a12],[a30,a21,a12],[a30,a51,a12],
[a11,a12,a13],[a11,a12,a23],[a11,a12,a33],[a11,a12,a43],[a11,a12,a53],[a21,a12,a13]....]

There are only 2 constraints:

1. Length of combination lists should be equal to the 'x'
2. In one combination, there can be at max only 1 unique value from a column of dataframe.

The minimal piece of code is given below that is constructing the dataframe. Any help will be much appreciated. Thanks!

data_dict={'a':['a10','a10','a30','a30','a10'],
          'b':['a11','a21','a21','a21','a51'],
          'c':['a12','a12','a12','a12','a12'],
          'd':['a13','a23','a33','a43','a53'],
          'e':['a14','a24','a14','a44','a14']}
df1=pd.DataFrame(data_dict)

Upvotes: 3

Views: 71

Answers (2)

Erfan
Erfan

Reputation: 42916

To get a unique value per column:

aa = [list(product(np.unique(df1[col1]), 
                   np.unique(df1[col2]), 
                   np.unique(df1[col3]))) 
      for col1, col2, col3 in list(combinations(df1.columns, 3))]

Old answer

First we flatten your matrix to a 1d array with np.flatten, and get the unique values with np.unique, then we use itertools.combinations:

from itertools import combinations

a = np.unique(df1.to_numpy().flatten())
aa = set(combinations(a, 3))

{('a10', 'a11', 'a12'),
 ('a10', 'a11', 'a13'),
 ('a10', 'a11', 'a14'),
 ('a10', 'a11', 'a21'),
 ('a10', 'a11', 'a23'),
 ('a10', 'a11', 'a24'),
 ('a10', 'a11', 'a30'),
 ('a10', 'a11', 'a33'),
 ('a10', 'a11', 'a43'),
 ('a10', 'a11', 'a44'),
 ('a10', 'a11', 'a51'),
 ('a10', 'a11', 'a53'),
 ('a10', 'a12', 'a13'),
 ('a10', 'a12', 'a14'),
 ...

Or to actually get the lists (which is less efficient):

from itertools import combinations

a = np.unique(df1.to_numpy().flatten())
aa = [list(x) for x in set(combinations(a, 3))]

[['a12', 'a33', 'a51'],
 ['a11', 'a12', 'a13'],
 ['a10', 'a11', 'a21'],
 ['a10', 'a23', 'a24'],
 ['a12', 'a14', 'a24'],
 ['a14', 'a43', 'a53'],
 ['a11', 'a21', 'a53'],
 ['a10', 'a12', 'a24'],
 ['a12', 'a21', 'a44'],
 ['a12', 'a30', 'a51'],
 ['a14', 'a23', 'a30'],
 ...

Upvotes: 3

jezrael
jezrael

Reputation: 862851

Use combinations with filtering by sets created by each column of DateFrame for second condition:

from  itertools import combinations

L = [set(df[x]) for x in df]
a = [x for x in combinations(np.unique(df.values.ravel()), 3) 
     if all(len(set(x).intersection(y)) < 2 for y in L)]

Upvotes: 2

Related Questions