Home > Net >  comparing 2 columns in pandas and outputting row
comparing 2 columns in pandas and outputting row

Time:06-13

I have the following dataframe in pandas and want to write a if statement to compare name with new-name and print identifier , person-name, new-identifier , new-person

identifier person-name type name new-identifier new-person new-type new-name
(hockey, player) sidney crosby athlete sidney (pittsburg, player) crosby-sidney player SC
(hockey, player) sidney crosby athlete sidney (pittsburg, player) crosby-sidney player MS
(hockey, player) wayne gretzky athlete wayne (oilers, player) gretzky-wayne player WG
(hockey, player) wayne gretzky athlete wayne (oilers, player) gretzky-wayne player TP

I need help writing the if statement in pandas; if sidney = SC, output output would be:

identifier person-name new-identifier new-person
(hockey, player) sidney crosby (pittsburg, player) crosby-sidney
 df[person-name].equals(df[new-person]) 

wouldn't work since I'm comparing contents in the column rather than the entire column. How can I compare the contents of those 2 columns and print the 4 columns

CodePudding user response:

IIUC, you can use

out = df[df['name'].eq('sidney') & df['new-name'].eq('SC')]

To output specific column, you can use

out = df[df['name'].eq('sidney') & df['new-name'].eq('SC')].filter(regex='identifier|person')
  • Related