1

I have the following df:

data = {
        'A':[0,0,1,1,1,1,1,1,1],
        'B':[0,0,1,1,0,1,4,1,2],
        'C':[0,10,130,1,0,1,4,1,12]
       }

X=5

df=pd.DataFrame(data)
df["D"]=min(df["A"],X)
print(df)

I've tried to create a new column - D, with the minimum value from A and X for each row but I'm getting the following error:

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

1 Answers1

1

Just use the pandas method Series.clip:

df['D'] = df['A'].clip(upper=X)

python min does not work as it expects a single argument, it cannot compare two Series of data points

Ch3steR
  • 20,090
  • 4
  • 28
  • 58
mozway
  • 194,879
  • 13
  • 39
  • 75
  • I've edited the answer since OP changed his question which invalidated the then correct answer. Please feel free to roll back/ edit the changes if not ok. – Ch3steR Aug 29 '21 at 15:24