【发布时间】:2019-12-12 04:04:59
【问题描述】:
我有一个数据框,我想对列中的行子集执行指数计算。我尝试了三个版本的代码,其中两个有效。但我不明白为什么一个版本给了我错误。
import numpy as np
版本 1(工作)
np.exp(test * 1.0)
版本 2(工作)
np.exp(test.to_list())
版本 3(错误)
np.exp(test)
它显示以下错误:
AttributeError Traceback (most recent call last)
AttributeError: 'int' object has no attribute 'exp'
The above exception was the direct cause of the following exception:
TypeError Traceback (most recent call last)
<ipython-input-161-9d5afc93942c> in <module>()
----> 1 np.exp(pd_feature.loc[(pd_feature[col] > 0) & (pd_feature[col] < 700), col])
TypeError: loop of ufunc does not support argument 0 of type int which has no callable exp method
测试数据由以下生成:
test = pd.loc[(pd['a'] > 0) & (pd['a'] < 650), 'a']
测试中的数据只是:
0 600
2 600
42 600
43 600
47 600
60 600
67 600
Name: a, dtype: Int64
其数据类型为:
<class 'pandas.core.series.Series'>
但是,如果我尝试生成一个虚拟数据集,它会起作用:
data = {'a':[600, 600, 600, 600, 600, 600, 600], 'b': ['a', 'a', 'a', 'a', 'a', 'a', 'a']}
df = pd.DataFrame(data)
np.exp(df.loc[:,'a'])
知道为什么我会看到此错误吗?非常感谢。
【问题讨论】:
-
test是一个objectdtype 数组,试试test.values.astype(float) -
见this answer,但忽略
apply的使用,将log10替换为exp。
标签: python numpy exponential