How to systematically drop a Pandas row given a particular condition in a column?

I have a simple dataframe consisting of 3 columns: Year, Period (Month) and Inflation I want to remove even months (e.g., February, April, etc.) and the code I came up with is the following:

i = df[((df["Periodo"] == "Febrero") | (df["Periodo"] == "Abril") | (df["Periodo"] =="Junio") | (df["Periodo"] =="Agosto") | (df["Periodo"] =="Octubre") | (df["Periodo"] =="Diciembre"))].index
df.drop(i, inplace = True)

Is there a quicker way rather than typing those tedious OR conditions to make the index? Like a for loop or something. Thanks

1 answer

  • answered 2021-05-15 17:41 Andrej Kesely

    You can use .isin() (note the ~ used as negation):

    to_drop = ["Febrero", "Abril", "Junio", "Agosto", "Octubre", "Diciembre"]