Skip to content
Advertisement

Pandas, get all possible value combinations of length k grouped by feature

I have a Pandas dataframe something like:

Feature A Feature B Feature C
A1 B1 C1
A2 B2 C2

Given k as input, i want all values combination grouped by feature of length k, for example for k = 2 I want:

JavaScript

How can I achieve that?

Advertisement

Answer

This is probably not that efficient but it works for small scale.

First, determine the unique combinations of k columns.

JavaScript

Then use MultiIndex.from_product to get cartesian product of k columns.

JavaScript
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement