Ketil Tveiten
Ketil Tveiten

Reputation: 240

Rowwise max/min in pandas

I'd like to do something like this:

df['A'] = max(0, min(df.B, df.C - df.D))

However, I get a ValueError ("the truth value of a Series is ambiguous"), which I guess means that the max and min functions are doing some boolean operations under the hood, and this doesn't distribute over the Series. I understand you can get the min/max of some set of columns by e.g.

df[['col1','col2','col3']].max(axis = 1)

and so I should be able to get my desired output by way of making some temporary columns with intermediate values, but I'd like a clean solution that does it directly. How does one do this without having to make extra columns for throwaway intermediate values?

Upvotes: 1

Views: 2578

Answers (1)

jpp
jpp

Reputation: 164643

max and min are built-in Python functions. They aren't designed for vectorised functionality which comes with Pandas / NumPy.

Instead, you can use np.maximum / np.minimum to perform element-wise calculations:

import numpy as np

df['A'] = np.maximum(0, np.minimum(df['B'], df['C'] - df['D']))

Upvotes: 7

Related Questions