Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

comparing 2 columns in pandas and outputting row

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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')
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading