I have data in the table which I wanted to explode
Input:
| Col 1 | Col 2 | Col 3 |
| -------- | ---------|---------|
| [A,B,C,D]| Value 1 | Value 2 |
Expected Output: The 'col 1' is exploded in the combination the [A,B,C,D]. The values 'col 1' can vary in length also the values are dynamic
| Col 1 | Col 2 | Col 3 |
| -------- | -------- |-------|
| [A,B] | Value 1 |Value 2|
| [A,C] | Value 1 |Value 2|
| [A,D] | Value 1 |Value 2|
| [B,C] | Value 1 |Value 2|
| [B,D] | Value 1 |Value 2|
| [C,D] | Value 1 |Value 2|
Let's use itertools.combinations
on Col 1
column then explode
import itertools
df['Col 1'] = df['Col 1'].apply(lambda lst: list(itertools.combinations(lst, 2)))
out = df.explode('Col 1', ignore_index=True)
print(out)
Col 1 Col 2 Col 3
0 (A, B) Value 1 Value 2
1 (A, C) Value 1 Value 2
2 (A, D) Value 1 Value 2
3 (B, C) Value 1 Value 2
4 (B, D) Value 1 Value 2
5 (C, D) Value 1 Value 2