Pythoner Pythoner - 1 year ago 218
Python Question

Pandas drop row with multiple columns

I request your kind assistance in dropping a row from a csv using Pandas using two filters.

import pandas as pd

moving = pd.read_csv('C:/Users/Salesdata.csv')
df = pd.DataFrame(moving)

df = df[df['Last Name, First Name'] != 'Reid, Mark and Connie' & df['Actual Sale Date'] == 3/8/2015]

df.to_csv('improvedcsv.csv', index=False)


My data:

Last Name, First Name Actual Sale Date
Bugs, Rabbit and Bunny 12/11/2015
Reid, Mark and Connie 3/8/2015
Cortese, Robert and Laura 10/15/2014
Reid, Mark and Connie 2/28/2015


I need to delete the Reid, Mark and Connie with the 3/8/2015. When I run the above drop column snippet the new csv returns NO data, only the column headings. How to fix this, please help Pythoners.

Answer Source

You need to put quotation marks around 3/8/2015 and change the logic a bit to filter out all those that are not equal to the condition your are filtering. You also need parentheses around each condition.

df[~((df['Last Name, First Name'] == 'Reid, Mark and Connie') & 
    (df['Actual Sale Date'] == '3/8/2015'))]
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download