Search code examples
pythondrop

Removing values from a column in a dataframe


I am trying to remove these values from this column ProductType - type:int64 from a dataframe (df2):

df3 = df2.drop(df2.index[df2['ProductType'] == '100', '109', '110', '118', '123', '124', '143', '153', '163', 
                                        '173', '179', '190', '191', '196', '197', '200', '205', '206', 
                                        '208', '209', '211', '214', '215', '216', '219', '221', '222', 
                                        '223', '225', '226', '401', '999'], inplace = True) 

AttributeError: only integers, slices (:), ellipsis (...), numpy.newaxis (None) and integer or boolean arrays are valid indices

How can I fixed this? Thank you


Solution

  • You could try this:

    removed_lst = ['100', '109', '110', '118', '123', '124', '143', '153', '163', 
                   '173', '179', '190', '191', '196', '197', '200', '205', '206', 
                   '208', '209', '211', '214', '215', '216', '219', '221', '222', 
                   '223', '225', '226', '401', '999']
    
    df3 = df2[~df2["ProductType"].isin(removed_lst)]