在python中使用.apply查找四分位数

问题描述 投票:0回答:1

我正在使用.apply将一个名为petrol ['tax']的数据帧的列传递给函数,该函数返回第一个四分位数。我试图使用下面的代码,但它抛出这个错误'浮动'对象没有属性'分位数'。

def Quartile(petrol_attrib):
    return petrol_attrib.quantile(.25)

petrol['tax'].apply(Quartile)

我需要帮助来实现这一点。

python pandas dataframe
1个回答
1
投票
 df = pd.DataFrame(np.array([[1, 1], [2, 10], [3, 100], [4, 100]]),
                  columns=['a', 'b'])

现在你可以在pandas中使用分位数功能了。确保使用0到1之间的数字。下面是一个例子:

 df.a.quantile(0.5)
 3.25

You can apply the function to the whole dataframe 
© www.soinside.com 2019 - 2024. All rights reserved.