0

still learning python and have a previous SAS background. Want to compare two date columns and have a string column based on the result.

example is below

enter image description here

essentially, if fee_paid_date is before close date, then say"fee paid before closed" else "fee paid during open"

so here is what i did

main_df["Payment_validity"] = main_df['fee_paid_date'].apply(lambda x: 'Fee paid while open' if x <=main_df['PACKAGE_D_CLOSE'] else 'Fee paid after closed ')

and i get the following error:

ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

Any help is much appreciated

1 Answer 1

1

Use axis=1 to tell do the apply row wise

main_df["Payment_validity"] = main_df[['PACKAGE_D_CLOSE','fee_paid_date']].apply(lambda x: 'Fee paid while open' if x[1] <= x[0] else 'Fee paid after closed ', axis=1)

NOTE: assuming PACKAGE_D_CLOSE and fee_paid_date are datetime object not string.

You can cast them to datetime by simply

main_df['fee_paid_date'] = pd.to_datetime(main_df['fee_paid_date'])
main_df['PACKAGE_D_CLOSE'] = pd.to_datetime(main_df['PACKAGE_D_CLOSE'])
Sign up to request clarification or add additional context in comments.

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.