Search code examples
pythonvalueerror

the truth value of a Series is ambiguous: value error python


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.


Solution

  • Try using loc instead:

    df.loc[df['Distance'] <= 250, 'margin'] = 65