pandasdataframedrop

Trying to drop rows based on values


I am trying to drop rows based on the value 'WITHDRAWN' in my pandas dataframe. It is under a column called 'Judge'. What I was intending was to drop rows with the value 'WITHDRAWN' in the 'Judge' column.

This is the code I have most recently used

adversary_df = adversary_df.drop(adversary_df[adversary_df['Judge'] == 'WITHDRAWN'], axis = 0, inplace = True)

This is the error I am receiving (those are the 3 columns of my data frame) KeyError: "['Client No.', 'Defendant/Municipality', 'Judge'] not found in axis"

This was another I tried from my understanding of pd.drop() documentation.

adversary_df = adversary_df.loc([:,'WITHDRAWN'], axis = 0)

This one just produces a weird, non-descript error

KeyError: 'WITHDRAWN'

I'm a relative novice in art of programming, so if anyone could lend some insight into the main issue, it would be greatly appreciated. Additionally, if someone knows what the non-descript error means then that would also be helpful!

adversary_df = adversary_df.drop(adversary_df[adversary_df['Judge'] == 'WITHDRAWN'], axis = 0, inplace = True)

Expected to drop two rows that have the value 'WITHDRAWN' under the 'Judge' column.


Solution

  • A simpler approach that would work, using boolean indexing:

    adversary_df = adversary_df[adversary_df['Judge'] != 'WITHDRAWN']
    

    Alternatively, if you still want to use drop, then you should be passing the indexes (instead of the df values as you do in your initial code). This way:

    adversary_df = adversary_df.drop(adversary_df[adversary_df['Judge'] == 'WITHDRAWN'].index) 
    

    Note that you should also remove the inplace = True, since you are returning the result df to adversary_df anyway (or set explicitly inplace = False)