我想通过pandas的agg()函数传递numpy percentile()函数,就像下面我对其他各种numpy统计函数所做的那样。
现在我有一个数据帧,看起来像这样:
AGGREGATE MY_COLUMN
A 10
A 12
B 5
B 9
A 84
B 22
我的代码看起来像这样:
grouped = dataframe.groupby('AGGREGATE')
column = grouped['MY_COLUMN']
column
在一个环境中,我的pandas版本为0.17.0,numpy版本为1.10.1。在另一个环境中,我的pandas版本为0.18.1,numpy版本为1.10.4。
我运行这段代码
from pandas import Series
import numpy as np
Series([1,2,3,4,5,np.NaN]).describe()
对于pandas版本0.17.0,我得到以下输出:
count 5.000000
mean 3.000000
std 1.581139
min 1.000000
25% 2.000000
50% 3
我有一个dataframe列,它是一组按降序排列的数字,我需要将最低的%10分配给一个新的dataframe。但我找不到办法提取最低的%10。谢谢。
我尝试过的第一个函数是numpy的percentile函数。
import numpy as np
import pandas as pd
df['Column']` #which has 2400 number
array1 = np.array(df['Column'])
np.percentile(array1,10)` #gave me the variable which is the %10 (
我想将R包Hmisc::wtd.quantile()转换为python。
以下是R中的示例:
我以为参考,逻辑似乎与R不同:
# First function
def weighted_quantile(values, quantiles, sample_weight = None,
values_sorted = False, old_style = False):
""" Very close to numpy.percentile, but supports weights.
NOT