take minimum between column value and constant global value
Use np.minimum
:
In [341]:df['MinNote'] = np.minimum(1,df['note'])dfOut[341]: session note minValue MinNote0 1 0.726841 0.726841 0.7268411 2 3.163402 3.163402 1.0000002 3 2.844161 2.844161 1.0000003 4 NaN NaN NaN
Also min
doesn't understand array-like comparisons hence your error
The preferred way to do this in pandas
is to use the Series.clip()
method.
In your example:
import pandasdf = pandas.DataFrame({'session': [1, 2, 3, 4], 'note': [0.726841, 3.163402, 2.844161, float('NaN')]})df['minVaue'] = df['note'].clip(upper=1.)df
Will return:
note session minVaue0 0.726841 1 0.7268411 3.163402 2 1.0000002 2.844161 3 1.0000003 NaN 4 NaN
numpy.minimum
will also work, but .clip()
has some advantages:
- It is more readable
- You can apply simultaneously lower and upper bounds:
df['note'].clip(lower=0., upper=10.)
- You can pipe it with other methods:
df['note'].abs().clip(upper=1.).round()