Skip to content Skip to sidebar Skip to footer

Compare 2 Columns In Pandas Dataframe With .loc

I want to compare two columns with value (1) and list rows that satisfy this condition. Here is my code: import pandas as pd df = pd.DataFrame({'col':[0,1,1,0,1],

Solution 1:

Add parentheses because priority precedence of & operator:

df1 = df.loc[(df['col'] == 1) & (df['col2'] == 1)]

Post a Comment for "Compare 2 Columns In Pandas Dataframe With .loc"