I would like to exclude rows from my dataframe if they meet a list (eligibility_criteria) condition, unless the tariff column starts with '***'. This is what I have:
import pandas as pd
df = df[~df['eligibility'].str.contains(eligibility_criteria, na=False)] #This works
How do I add this 'except when' qualifier...
#df['tariff'].str.startswith("***")
Let E be the eligibility criterion and S be true if the tariff starts with "***". You want to exclude the rows that are E unless S which is the same as E & ~S. Conversely, you want to keep the rows that do not have this property: ~(E & ~S), which is the same as ~E | S:
df = df[~df['eligibility'].str.contains(eligibility_criteria, na=False) |
df['tariff'].str.startswith("***")]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With