Skip to content
Advertisement

Percentile Python, problems Setting values

Im calculating percentiles and doing some operations along too many columns.

perc = np.percentile([100,100,100,100,100,100,100,200,300,123,124,90,98,999,567,345],[20,40,60,80])
print(perc)
array([100., 100., 123., 300.])

I need to get the unique values then, setting my vector i got this:

set(perc)
{100.0, 123.0, 300.0}

It works in almost all of the columns, but for any reason i got columns with this issue:

perc_1 = np.percentile([100,100,np.inf,np.inf,np.inf,np.inf,-np.inf,-np.inf,-np.inf],[20,40,60,80])

then calculating percentiles:

print(perc_1)
array([ nan, 100.,  nan,  nan])

then, setting this vector i got this:

set(perc_1)
{nan, nan, 100.0, nan}

Setting doesnt work.

How can i solve this?

Advertisement

Answer

Since nan values, which are float, are not equal even to themselves, you can use code below:

perc_1 = np.percentile([100,100,np.inf,np.inf,np.inf,np.inf,-np.inf,-np.inf,-np.inf],[20,40,60,80])
list(filter(lambda x: x==x, list(set(perc_1))))

Output

[100.0]
Advertisement