代码之家  ›  专栏  ›  技术社区  ›  id101112

使用numpy.percentile获取数据帧中不同ID上的百分比

  •  1
  • id101112  · 技术社区  · 6 年前

    我必须一次获取整个数据的百分位数,但是我有几个ids数据,希望有所有ids的单独结果。这是我的代码,在这里我得到一些错误

    result_frame.groupby('ID').apply(percentile('rolling_mean', [25]))
    

    我得到以下错误

    ---------------------------------------------------------------------------
    TypeError                                 Traceback (most recent call last)
    <ipython-input-60-87a94290cfde> in <module>()
    ----> 1 result_frame.groupby('VoyageID').apply(percentile('rolling_mean', [25]))
    
    ~/anaconda3/lib/python3.6/site-packages/numpy/lib/function_base.py in percentile(a, q, axis, out, overwrite_input, interpolation, keepdims)
       4272     r, k = _ureduce(a, func=_percentile, q=q, axis=axis, out=out,
       4273                     overwrite_input=overwrite_input,
    -> 4274                     interpolation=interpolation)
       4275     if keepdims:
       4276         if q.ndim == 0:
    
    ~/anaconda3/lib/python3.6/site-packages/numpy/lib/function_base.py in _ureduce(a, func, **kwargs)
       4014         keepdim = [1] * a.ndim
       4015 
    -> 4016     r = func(a, **kwargs)
       4017     return r, keepdim
       4018 
    
    ~/anaconda3/lib/python3.6/site-packages/numpy/lib/function_base.py in _percentile(a, q, axis, out, overwrite_input, interpolation, keepdims)
       4389             n = np.isnan(ap[-1:, ...])
       4390 
    -> 4391         x1 = take(ap, indices_below, axis=axis) * weights_below
       4392         x2 = take(ap, indices_above, axis=axis) * weights_above
       4393 
    
    TypeError: ufunc 'multiply' did not contain a loop with signature matching types dtype('<U32') dtype('<U32') dtype('<U32')
    
    1 回复  |  直到 6 年前
        1
  •  2
  •   smj    6 年前

    这个怎么样?

    import numpy as np
    import pandas as pd
    
    data = pd.DataFrame({'group': ['A', 'B'] * 100, 'value': np.random.randn(200)})
    
    data.groupby('group')['value'].quantile([.25, .75])
    

    你对分组是正确的,你只需要确定你想总结的列,然后使用 quantile .