Skip to content
Advertisement

pandas: Explode (duplicate) by group

I have a df that looks like this :

JavaScript

It’s an example for one row but there are thousands of rows. I want to explode each value where there are multiple values in these four “TEST” columns ie. I want each one to duplicate the row for each one of the “test” that is the same and if there are many (separed by a “/”) i want to duplicate too. I want an “X” if the duplication contains nothing.

Of course there are a lot of other columns, for which the values doesn’t matter for the duplication: it should stay the same. I’m not sure how to explain it, but here’s an example of the output I want for the input I provided:

JavaScript

Advertisement

Answer

Here is an alternative, using a helper function:

JavaScript

output:

JavaScript

output on @jezrael’s better example:

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