Skip to content
Advertisement

Organizing pandas dataframe and switching column order by each row

I have two pandas columns that are supposed to represent the interactions of 2 types of chemicals. I want the ‘user’ type of ID to be in one column and the id2 column to only have the ‘id/’ ID types. This basically means just switch the first two columns for any rows that are not in this order. But the thing is, for each row where I switch the order, I also need to flip the True/False value for the third column and I have about 7 other columns that I want to stay the same. I have a small example below.

JavaScript

desired

JavaScript

Advertisement

Answer

Use boolean indexing:

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