我需要根据世界卫生组织的标准计算身高和体重等测量指标的z分数。要做到这一点,我想使用专为之设计的pygrowup包。打字时:
data['z_height'] = Calculator.lhfa(df['length'],df.age, df.female) 我收到一条错误消息:
`AttributeError Traceback (most recent call last)
<ipython-input-9-8c8a5b790d43> in <module>
3
4
----> 5 df['z_height'] = Calculator.lhfa(df['length'],df.age, df.female)
6 df.head()
~\anaconda3\lib\site-packages\pygrowup\pygrowup.py in lhfa(self, measurement, age_in_months, sex, height)
280 def lhfa(self, measurement=None, age_in_months=None, sex=None, height=None):
281 """ Calculate length/height-for-age """
--> 282 return self.zscore_for_measurement('lhfa', measurement=measurement,
283 age_in_months=age_in_months,
284 sex=sex, height=height)
~\anaconda3\lib\site-packages\pandas\core\generic.py in
__getattr__(self, name) 5272 if self._info_axis._can_hold_identifiers_and_holds_name(name): 5273 return self[name]
-> 5274 return object.__getattribute__(self, name) 5275 5276 def __setattr__(self, name: str, value) -> None:
AttributeError: 'Series' object has no attribute 'zscore_for_measurement' 因为这是一个非常具体的包,所以Google没有太多的信息。GitHub存储库似乎不活跃,我的问题也没有在那里得到解决。因此,我将非常感谢您的任何建议,谢谢!
发布于 2020-06-23 23:41:40
注释中的解决方案:
性别变量应该是"M“或"F",而不是1或0。
先前的回答:
当Calculator类需要一个十进制(或类似的)时,您似乎正在将一个Pandas对象(df['length'])传递给它。
您可以将Series实例转换为所需的类型,但如果zscore_for_measurement()方法有多个值(这是从Calculator.lhfa()调用的),则还需要提供一个值:
>>> x = np.array([1, 2, 2.5])
>>> x
array([ 1. , 2. , 2.5])
>>> x.astype(int)
array([1, 2, 2])通过小数点(例如5.5等)可能代替了df['length']参数。
源代码:https://github.com/ewheeler/pygrowup/blob/master/pygrowup/pygrowup.py
关于Pandas系列对象的文档:https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.Series.html
关于铸造系列对象的文档:https://pandas.pydata.org/pandas-docs/version/0.7.0/generated/pandas.Series.astype.html
如果结果不是这样,我会更新我的答案。
编辑
您还可以尝试创建(实例化)一个Calculator对象,然后调用该对象上的lhfa()方法--如果它可能引用源代码中的"self“,就好像它是另一个(Pandas Series)对象一样。例如:
calc = Calculator()
data['z_height'] = calc.lhfa(...)或者,您也可以尝试像这样直接调用zscore_for_measurement()方法,确保手动传递'lhfa‘指示器参数:
data['z_height'] = Calculator.zscore_for_measurement('lhfa', measurement, age, sex...)https://stackoverflow.com/questions/62545258
复制相似问题