Home > OS >  Cartesian product of rows in pandas?
Cartesian product of rows in pandas?

Time:04-07

The data I'm working with (data sample

The output I'd like is something like:

cap-diameter | cap-shape
10 | x
10 | f
20 | x
20 | f
5 | p
5 | x
10 | p
10 | x
...

So I don't want the cartesian product of all of the entries in each column, just that of the respective rows. I think pd.explode() might be a good place to start but I'm not sure how to accomplish this. Thanks in advance.

CodePudding user response:

Explode each column consecutively

df = df.explode('cap-diameter')
df = df.explode('cap-shape')
  • Related