Comparing (less than) two values different columns not working

Question:

I’m trying to compare two values on my sheet and the code is not working, although there are values that attend the condition that I am comparing.

The goal is: IF the END_DATE is smaller than the EFF_MTH, then the EFF_MTH should be the END_DATE value.

Can someone please assist? where am I missing the issue?

I added .all() as I was receiving the error:

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

Please let me know if there is a better way to handle this. Thanks in advance.

df = pd.read_excel (f"C:/Users/Me/Desktop/Cleaned_file - 12-01-22.xlsx")

## REMOVING TIME FROM DATES
df['DATE_START'] = df['DATE_START'].dt.strftime('%d%m%Y')

## Slices only the month
df['END_DATE'] = df['END_DATE'].dt.strftime('%d%m%Y'[2:4])

## Defines eff month based on period column

df.loc[df['PERIOD'] == '3Q', 'EFF_MTH'] = "09"
df.loc[df['PERIOD'] == '1H', 'EFF_MTH'] = "06"
df.loc[df['PERIOD'] == '2Q', 'EFF_MTH'] = "06"
df.loc[df['PERIOD'] == '1Q', 'EFF_MTH'] = "03"
df.loc[df['PERIOD'] == '2H', 'EFF_MTH'] = "12"
df.loc[df['PERIOD'] == '4Q', 'EFF_MTH'] = "12"

## comparing end month with eff month

if df['END_DATE'].all() < df['EFF_MTH'].all():
    df['EFF_MTH']== df['END_DATE']

Answers:

Did you mean if (df['END_DATE'] < df['EFF_MTH']).all()?

But what you want to do is:

df['EFF_MTH'] = np.where(df['END_DATE'] > df['EFF_MTH'], df['END_DATE'], df['EFF_MTH'])
Answered By: Yevhen Kuzmovych
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.