Skip to content
Advertisement

Pandas long format of success table

I have a table with the following structure in pandas:

JavaScript

I would like to put it in a long format. In this case, we have, for each user, a different number of events, and successes. I would like to transform this into an event table (each row corresponds to an event, and there is a column that tells you whether it was a success or not). In this case, the output dataframe should be:

JavaScript

What’s the simplest way of doing this in pandas? I would like to avoid for loops that iterate on each user, create dataframes and then append them.

Advertisement

Answer

You may try with reindex with repeat then assign the value with limit condition created by groupby cumsum

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