Python:np.nanpercentile,我的数据帧需要哪种数据类型?

时间:2017-10-03 17:38:08

标签: python pandas dataframe percentile

我有一个类型为对象的熊猫数据框。

df.dtypes

Out:
data        object
stimulus    object
trial       object
dtype: object

df.head()

Out:
    data    stimulus    trial
0   2      -2           1
1   2      -2           2
2   2      -2           3
3   2      -2           4
4   2      -2           5

我想得到我的数据集的特定百分位数。当我使用这段代码时,我在输出中得到NaN,可能是因为我的数据集本身有NaN,python解释为无穷大,所以在计算更高的百分位数时会出现问题。

df.groupby('stimulus').data.apply(lambda x: np.percentile(x, q=66))

Out:
stimulus
-2.00     2.0
-1.75     2.9
-1.00     1.0
-0.75     1.0
-0.50     0.0
 0.50     7.8
 1.00     9.9
 1.25    11.9
 1.75    13.9
 2.50     NaN

我已经发现我需要使用np.nanpercentile()代替,但是当我使用np.nanpercentile()时,我得到了这个错误。我在其他地方读到np.nanpercentile()检查输入数组的数据格式并抱怨它是否适合。你知道我需要改变数据的方式和格式吗?

df.groupby('stimulus').data.apply(lambda x: np.nanpercentile(x, q=66))

Out:
TypeError: ufunc 'isnan' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''

1 个答案:

答案 0 :(得分:0)

这最终为我完成了这项工作:

@Component("beanB")
class B implements A{

    @override
    some(){
    }

}

@Component("beanC")
class C implements A{

    @override
    some(){
    }

}

Class D {

    @Autowired
    @Qualifier("beanB")
    List<A> somes;//B will be injected because of @Qualifier

}