0

I have a data frame, but the headings can change dynamically, as such I am using i.loc for Columns 4 onwards.

My working code is:

cv.iloc[:,[4]] = cv['Allocation'].apply(lambda x: x if dt.strptime(dates[0], "%Y-%m-%d") <= dt.strptime('2021-12-31', "%Y-%m-%d") else 0)

This applies the contents of cv['Allocation'] if the first date in my list is <= the fixed date in the formula.

However, what i would like to do is swap the fixed date for a Panda Column I want this:

cv.iloc[:,[4]] = cv['Allocation'].apply(lambda x: x if dt.strptime(dates[0], "%Y-%m-%d") <= cv['Allocation Completion Date'] else 0)

If I do that, I get: 'ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().'

Runawaygeek
  • 115
  • 3
  • 13
  • Does this answer your question? [Truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all()](https://stackoverflow.com/questions/36921951/truth-value-of-a-series-is-ambiguous-use-a-empty-a-bool-a-item-a-any-o) – Trenton McKinney Feb 03 '21 at 01:28
  • Not really, as I am not using AND or OR, I kinda get the concept, but I am not sure what I am missing. – Runawaygeek Feb 03 '21 at 01:29
  • I adjusted from the link you posted: ``` cv.iloc[:,[4]] = cv['Allocation'].apply(lambda x: dt.strptime(dates[0], "%Y-%m-%d") <= cv['Allocation Completion Date']) | 0 ``` now i just get NaNs ... lol – Runawaygeek Feb 03 '21 at 01:38

1 Answers1

0

dt.strptime(dates[0], "%Y-%m-%d") <= cv['Allocation Completion Date'] return a Series of bool.

so, try np.where:

cond = dt.strptime(dates[0], format="%Y-%m-%d")  <= cv['Allocation Completion Date']
cv.iloc[:,[4]] = np.where(cond, cv['Allocation'], 0)
Ferris
  • 5,325
  • 1
  • 14
  • 23
  • np.where, I feel like an idiot now, you know when you get lost in the code, wood for trees style.. thank you so much. – Runawaygeek Feb 03 '21 at 09:23