Naor
Naor

Reputation: 119

How can I create a new column in df with min value between selected columns?

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().

Upvotes: 1

Views: 586

Answers (1)

mozway
mozway

Reputation: 260975

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

Upvotes: 1

Related Questions