gpt4 book ai didi

python - 在 Numpy 中定义计算 IQR 的函数

转载 作者:太空宇宙 更新时间:2023-11-03 20:21:22 26 4
gpt4 key购买 nike

我正在尝试定义一个函数来计算 numpy 中的 IQR。代码非常简单,但我不断收到一条错误消息,我无法弄清楚

def calculate_icq(variable):
icq = 0
icq += (np.percentile(df.variable, 75) - np.percentile(df.variable, 25))

return icq

当我使用列变量之一运行该函数时(例如calculate_icq('Price')),它说“DataFrame”对象没有属性“variable”

术语“价格”不应该映射到变量并替换它

最佳答案

你不能这样调用列。您需要使用df[variable]

def calculate_icq(variable):
icq = 0
icq += (np.percentile(df[variable], 75) - np.percentile(df[variable], 25))

return icq

关于python - 在 Numpy 中定义计算 IQR 的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58142399/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com