take minimum between column value and constant global value
Asked Answered
D

2

36

I would like create new column for given dataframe where I calculate minimum between the column value and some global value (in this example 7). so my df has the columns session and note and my desired output column is minValue :

session     note     minValue
1       0.726841     0.726841
2       3.163402     3.163402  
3       2.844161     2.844161
4       NaN          NaN

I'm using the built in Python method min :

df['minValue']=min(7, df['note'])

and I have this error:

ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
Dogmatize answered 13/11, 2015 at 9:40 Comment(0)
T
46

Use np.minimum:

In [341]:
df['MinNote'] = np.minimum(1,df['note'])
df

Out[341]:
   session      note  minValue   MinNote
0        1  0.726841  0.726841  0.726841
1        2  3.163402  3.163402  1.000000
2        3  2.844161  2.844161  1.000000
3        4       NaN       NaN       NaN

Also min doesn't understand array-like comparisons hence your error

Tatianatatianas answered 13/11, 2015 at 9:45 Comment(0)
M
43

The preferred way to do this in pandas is to use the Series.clip() method.

In your example:

import pandas

df = 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   minVaue
0  0.726841        1  0.726841
1  3.163402        2  1.000000
2  2.844161        3  1.000000
3       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()
Misconstrue answered 9/5, 2019 at 10:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.