Drop rows on multiple conditions in pandas dataframe

后端 未结 5 839
猫巷女王i
猫巷女王i 2020-12-05 11:37

My df has 3 columns

df = pd.DataFrame({\"col_1\": (0.0, 0.0, 1.0, 1.0, 0.0, 1.0, 1.0), 
                   \"col_2\": (0.0, 0.24, 1.0, 0.0, 0.22, 3.11, 0.0)         


        
5条回答
  •  渐次进展
    2020-12-05 12:13

    mask = df['Product_Code'].isin(['filter1', 'filter2', 'filter3'])
    df = df[~mask]
    df.head()
    

    .isin() allows you to filter the entire dataframe based on multiple values in a series. This is the least amount of code to write, compared to other solutions that I know of.

    Adding the ~ inside the column wise filter reverses the logic of isin().

提交回复
热议问题