I have a simple quantile function that I was going to use to select some data. I am trying to convert that function to a lambda function, but it does not appear to be working the way I expected. I want the lambda function to be able to set values above the 90th percentile as 'yes' and those below as 'no'. Need help figuring out what I am doing wrong.
Below is the code with a small set of data for illustrative purposes.
import pandas as pd
import numpy as np
# the simple function
def selector(x):
return np.quantile(x, 0.90)
# the lambda function (not working properly.)
df.apply(lambda x: 'yes' if (x >= np.quantile(x, 0.90)) else 'no'))
df = pd.DataFrame({'tm': [263, 257, 263, 268, 268,259, 265,
263, 256, 263, 264, 268, 268, 263,
262, 260, 262, 235, 263, 264, 264]})
CodePudding user response:
Use numpy.where
if performance is important:
df['new'] = np.where(df['tm'] >= np.quantile(df['tm'], 0.90), 'yes', 'no')
print (df)
tm new
0 263 no
1 257 no
2 263 no
3 268 yes
4 268 yes
5 259 no
6 265 no
7 263 no
8 256 no
9 263 no
10 264 no
11 268 yes
12 268 yes
13 263 no
14 262 no
15 260 no
16 262 no
17 235 no
18 263 no
19 264 no
20 264 no
Your solution with lambda function is necessary change by test quantile of original column tm
:
df['new'] = df['tm'].apply(lambda x: 'yes' if (x >= np.quantile(df['tm'], 0.90)) else 'no')
CodePudding user response:
can you try this lambda code :
df2 = df.applymap(lambda x : "yes" if x >= selector(x) else "no")