Home > OS >  new column which is min of two other columns if condition is True
new column which is min of two other columns if condition is True

Time:12-23

if df['freq']<10 i want to transform all values in "freq_new" to the minimun between df["freq_cell"] and 10

I try this and it didnt work

df.loc[df['freq']<10,['freq_new']]=min((df["freq_cell"]/10),10)

CodePudding user response:

Use:

import numpy as np
df['freq_new'] = np.where(df['freq']<10, np.minimum((df["freq_cell"]/10),10), df['freq'])

CodePudding user response:

here you go

 import pandas as pd
 import numpy as np 
`df.loc[df['freq']>10,'freq_new']=np.minimum((df['freq_cell'].min()/10,10)),df['freq'])`

CodePudding user response:

The syntax of your code is incorrect. IIUC, you can use loc to locate where column 'freq' is less than 10, and create a new column called 'freq_new' which will be the minimum between 'freq_cell' and the value 10 using clip:

df.loc[df.freq < 10, 'frec_new'] = df['freq_cell'].clip(upper=10)

The code in the RHS says: return value from freq_cell but ceil it at 10 if it's bigger. So the maximum will be 10.

  • Related