Skip to content Skip to sidebar Skip to footer

Converting String Into Condition For Pandas Dataframe

I have a condition for a pandas dataframe written in a string. Something like this: '(data['Variable1'] == 1) & (data['Variable2'] == 2)' Is there a way to apply this conditio

Solution 1:

You can use pandas query to filter required rows

your_query_string = "Variable1 == 1 & Variable2 == 2"
data = data.query(your_query_string)

Post a Comment for "Converting String Into Condition For Pandas Dataframe"