1

I am trying to fill a column in my dataset using the following condition.

def shorthaul(df):
    for i in df:
        if df['Distance'] <= 250:
            df['margin'] = 65

When I try to run this the following error pops up:

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

I would appreciate any help to solve this issue.

Nir Alfasi
  • 53,191
  • 11
  • 86
  • 129
Sonia N.
  • 13
  • 2
  • Let's say `df['Distance'] <= 250` is `[True, False, True]`, what do you want to happen next? – timgeb Jul 21 '21 at 09:47
  • 1
    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) – MrJuicyBacon Jul 21 '21 at 09:48

1 Answers1

0

Try using loc instead:

df.loc[df['Distance'] <= 250, 'margin'] = 65
U13-Forward
  • 69,221
  • 14
  • 89
  • 114