python - How can I filter a pandas dataset based on multiple columns? -


i trying ifthen logic working in dataframe , couldn't find examples.

what trying filter dataset include values col1=col3 , col2=col4

col1       col2     col3       col4 wagner     john     wagner     john jane       mary     klein      peter  schneider  megan    wicker     sam schneider  megan    schneider  megan 

result

col1       col2     col3        col4 wagner     john     wagner      john schneider  megan    schneider   megan 

my code here doesn't work

 df1.apply(lambda x : x['col1'] if x['col1'] == x['col1'] , x['col2'] == x['col2'] else "", axis=1 

i'd use dataframe.query() method:

in [205]: df.query("col1==col3 , col2==col4") out[205]:         col1   col2       col3   col4 0     wagner   john     wagner   john 3  schneider  megan  schneider  megan 

or "classical" approach:

in [206]: df.loc[(df.col1==df.col3) & (df.col2==df.col4)] out[206]:         col1   col2       col3   col4 0     wagner   john     wagner   john 3  schneider  megan  schneider  megan 

Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -