如何摆脱“ AttributeError:'float'对象没有属性'log2'”

时间:2019-05-13 09:04:35

标签: pandas lambda seaborn

说我有一个数据框,其中列min value = 36884326.0和max value = 6619162563.0,我需要将它们绘制为箱形图,因此我尝试对值进行对数变换,如下所示:

diff["values"]     = diff['value'].apply(lambda x: (x+1))
diff["log_values"] = diff['values'].apply(lambda x: x.log2(x))

但是,以上几行引发了如下错误,

AttributeError                            Traceback (most recent call last)
<ipython-input-28-fe4e1d2286b0> in <module>
      1 diff['value'].max()
      2 diff["values"]     = diff['value'].apply(lambda x: (x+1))
----> 3 diff["log_values"] = diff['values'].apply(lambda x: x.log2(x))

~/software/anaconda/lib/python3.7/site-packages/pandas/core/series.py in apply(self, func, convert_dtype, args, **kwds)
   3192             else:
   3193                 values = self.astype(object).values
-> 3194                 mapped = lib.map_infer(values, f, convert=convert_dtype)
   3195 
   3196         if len(mapped) and isinstance(mapped[0], Series):

pandas/_libs/src/inference.pyx in pandas._libs.lib.map_infer()

<ipython-input-28-fe4e1d2286b0> in <lambda>(x)
      1 diff['value'].max()
      2 diff["values"]     = diff['value'].apply(lambda x: (x+1))
----> 3 diff["log_values"] = diff['values'].apply(lambda x: x.log2(x))

AttributeError: 'float' object has no attribute 'log2'

任何建议都会很棒。谢谢

1 个答案:

答案 0 :(得分:1)

您需要numpy.log2功能才能正常使用,请检查sintaxis here