0

I need to filter the df based on 2 criteria: where Name != jack and ignore all dates for jack where Date <= 2020-04-01

 # List of Tuples
df =       [ ('jack', 'Apples' , '2020-01-01') ,
             ('Riti', 'Mangos' , '2020-02-01') ,
             ('Aadi', 'Grapes' , '2020-03-01') ,
             ('jack', 'Oranges', '2020-04-01') ,
             ('Lucy', 'Mangos' , '2020-05-01') ,
             ('jack', 'Apples' , '2020-12-01')
              ]
#Create a DataFrame object
df1 = pd.DataFrame(df, columns = ['Name' , 'Product', 'Date']) 
df1

Expected result being:

    Name    Product Date
0   Riti    Mangos  2020-02-01
1   Aadi    Grapes  2020-03-01
2   Lucy    Mangos  2020-05-01
3   jack    Apples  2020-12-01
6
  • Do you mean (Name != jack) or (ignore all dates for jack where date <= 2020-04-01)? Commented Nov 10, 2022 at 4:39
  • both criteria. (Name != jack) AND (ignore all dates for jack where date <= 2020-04-01) Commented Nov 10, 2022 at 4:44
  • Then why is there jack in the 4th row?(index = 3) Commented Nov 10, 2022 at 4:44
  • date far jack is greater than 2020-04-01. this is valid Commented Nov 10, 2022 at 4:47
  • (FALSE AND TRUE) is FALSE.. Commented Nov 10, 2022 at 4:49

2 Answers 2

2

boolean indexing of multi condition

cond1 = df1['Name'] != 'jack'
cond2 = pd.to_datetime(df1['Date']) > pd.Timestamp('2020-04-01')
df1[cond1 | cond2].reset_index(drop=True)

output:

    Name    Product Date
0   Riti    Mangos  2020-02-01
1   Aadi    Grapes  2020-03-01
2   Lucy    Mangos  2020-05-01
3   jack    Apples  2020-12-01
Sign up to request clarification or add additional context in comments.

Comments

0

You could do the below:

import pandas as pd
 # List of Tuples
df = [('jack', 'Apples' , '2020-01-01'), 
('Riti', 'Mangos' , '2020-02-01'),
('Aadi', 'Grapes' , '2020-03-01'),
('jack', 'Oranges', '2020-04-01'),
('Lucy', 'Mangos' , '2020-05-01'),
('jack', 'Apples' , '2020-12-01')
]
#Create a DataFrame object
df1 = pd.DataFrame(df, columns = ['Name', 'Product', 'Date']) 

# Filtering for condition Name = !jack
df1 = df1[df1['Name'] != 'jack']

# Convert the date to datetime64
df1['Date'] = pd.to_datetime(df1['Date'], format='%Y-%m-%d')

# Filter by specified date
filtered_df = df1.loc[(df1['Date'] <= '2020-04-01')]

# print the filtered result
print(filtered_df)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.