Drop rows on multiple conditions in pandas dataframe

后端 未结 5 808
猫巷女王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:31

    drop is a method, you are calling it using [], that is why it gives you:

    'method' object is not subscriptable
    

    change to () (a normal method call) an it should work:

    import pandas as pd
    
    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),
                       "col_3": ("Mon", "Tue", "Thu", "Fri", "Mon", "Tue", "Thu")})
    
    df_new = df.drop(df[(df['col_1'] == 1.0) & (df['col_2'] == 0.0)].index)
    print(df_new)
    

    Output

       col_1  col_2 col_3
    0    0.0   0.00   Mon
    1    0.0   0.24   Tue
    2    1.0   1.00   Thu
    4    0.0   0.22   Mon
    5    1.0   3.11   Tue
    

提交回复
热议问题