Skip to content
Advertisement

Reshape Pandas DatafRames by binary columns value

Can’t figure out how to reshape my DataFrame into new one by several binary columns value.

Input:

JavaScript

I want to reshape by binary values, i.e. column a/b/c, if their value == 1, I need every time new column with all data.

Expected output:

JavaScript

Stucked here from the morning, will appreciate help very much !

Advertisement

Answer

Use DataFrame.melt with filtering 1 in boolean indexing, DataFrame.pop is used for removing column after filtration:

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