Search code examples
pythonpandasfilterdataframekeyerror

Filtering Dataframe by Row


I have a dataframe that looks like this

CompanyName User Issue  Equipment Issue No  TBD Total
Customer1   0   0   35  0   35
Customer2   0   0   28  0   28
Customer3   2   3   12  0   17
Customer4   5   1   8   0   14

I want to remove Customer1 and Customer2 from the table. I am trying to use

df= df[[c for c in df.columns if c not in ['Customer1','Customer2']]]

and

df=df[(~df.isin('Customer2','Customer2'))]

and

df=df[~df['CompanyName'].str.contains('Customer1')]

I'm not getting any errors, but Customer1 and Customer2 are still showing up in the plot!

df.columns.tolist

Out[85]: <bound method Index.tolist of Index([u'CompanyName', u'User Issue', u'Equipment Issue', u'No', u'TBD'], dtype='object')>

sortedtotal.columns.tolist()
Out[93]: 
['CompanyName',
 'User Issue',
 'Equipment Issue',
 'No',
 'TBD']

Solution

  • Try resetting the index:

    df.reset_index()
    

    You are then getting an autoincrementing index, but your customers index will become a column. More here: http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.reset_index.html

    After that your commands to filter the data should work.

    EDIT: Use this filtering approach: df = df[~df['Company Name'].isin(['customer1', 'customer2'])]