Home > Net >  Percentile Python, problems Setting values
Percentile Python, problems Setting values

Time:02-22

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?

CodePudding user response:

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]

CodePudding user response:

You can to replace nan and inf to preserve those values.

set(None if np.isnan(x) or np.isinf(x) else x for x in perc_1)

which is {100.0, None}

  • Related