Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Drop rows in dataframe that are between two dates Pandas

Hello so I have the table below:

import pandas as pd

raw_data = {
    'vendor_id': [1, 2, 3, 4, 5, 6], 
    'name': ['vendor_schmendor', 'parts_r_us', 'vendor_king', 'vendor_diagram', 'venny', 'vendtriloquist'], 
    'contract_sign_date': ['2018-09-01', '2018-09-03', '2018-10-11', '2018-08-21', '2018-08-13', '2018-10-29'],
    'total_spend' :[34324, 23455, 77654, 23334, 94843, 23444]}

df = pd.DataFrame(raw_data, columns = ['vendor_id', 'name', 'contract_sign_date', 'total_spend'])

I was given a task where I have to drop all the rows where the contract_sign_date is between "2018-09-01" and "2018-10-13", this is my solution (although it doesn’t work):

alter = df.drop((df['contract_sign_date'] == "2018-09-01") & (df['contract_sign_date'] == "2018-10-13"))

the output throws: KeyError: '[False, False, False, False, False, False] not found in axis'

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

So can anyone provide a code so in order that I can construct what I was desired for?

>Solution :

Your condition is to check simultaneous equality with two different values (a == b) and (a==c), which is impossible.

Use between and the boolean NOT operator ~:

alter = df[~df['contract_sign_date'].between("2018-09-01", "2018-10-13")]

output:

   vendor_id            name contract_sign_date  total_spend
3          4  vendor_diagram         2018-08-21        23334
4          5           venny         2018-08-13        94843
5          6  vendtriloquist         2018-10-29        23444

NB. we’re using strings here as the YYYY-MM-DD format enables direct comparison, with a different format you would need to use a datetime type

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading