Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas drop row with multiple columns

Tags:

python

pandas

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.

like image 657
Jake Wagner Avatar asked Oct 23 '25 15:10

Jake Wagner


1 Answers

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'))]
like image 50
Ted Petrou Avatar answered Oct 25 '25 06:10

Ted Petrou



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!