1

I want to check if each value in a column exist in another dataframe (df2) and if its date is at least 3 days close to the date in the second dataframe (df2) or if they meet other conditions.

The code I've written works, but I want to know if there's a better solution to this problem or a code that's more efficient

Exemple:

def check_answer(df):
    if df.ticket_count == 1:
        return 'Yes'
    elif (df.ticket_count > 0) and (df.occurrences == 1):
        return 'Yes'
    elif any(
            df2[df2.partnumber == df.partnumber]['ticket_date'] >= df['date']
    ) and any(
        df2[df2.partnumber == df.partnumber]['ticket_date'] <= df['date'] + pd.DateOffset(days=3)
    ):
        return 'Yes'
    else:
        return 'No'

df['result'] = df.apply(check_answer, axis=1)
Breno
  • 11
  • 1

1 Answers1

0

You could try using list comprehension.

Here's an example: list comprehension in pandas

And if you need to create a copy of your data frame with news columns containing the result of your conditions, you can check this exemple: Pandas DataFrame Comprehensions

I hope I could help

Best regards.