这是我的数据集
Id B C
1 0.784 -1.6745
2 2.123 -2.8934
以下是我所尝试的
import numpy as np
df.apply(lambda x: np.exp(x)/(1+np.exp(x)))
错误信息
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
AttributeError: 'float' object has no attribute 'exp'
The above exception was the direct cause of the following exception:
TypeError Traceback (most recent call last)
<ipython-input-43-288751cc2e1d> in <module>
----> 1 A.apply(lambda x: np.exp(x)/(1+np.exp(x)))
~/.local/lib/python3.6/site-packages/pandas/core/frame.py in apply(self, func, axis, raw, result_type, args, **kwds)
7550 kwds=kwds,
7551 )
-> 7552 return op.get_result()
7553
7554 def applymap(self, func) -> "DataFrame":
~/.local/lib/python3.6/site-packages/pandas/core/apply.py in get_result(self)
183 return self.apply_raw()
184
--> 185 return self.apply_standard()
186
187 def apply_empty_result(self):
~/.local/lib/python3.6/site-packages/pandas/core/apply.py in apply_standard(self)
274
275 def apply_standard(self):
--> 276 results, res_index = self.apply_series_generator()
277
278 # wrap results
~/.local/lib/python3.6/site-packages/pandas/core/apply.py in apply_series_generator(self)
303 for i, v in enumerate(series_gen):
304 # ignore SettingWithCopy here in case the user mutates
--> 305 results[i] = self.f(v)
306 if isinstance(results[i], ABCSeries):
307 # If we have a view on v, we need to make a copy because
<ipython-input-43-288751cc2e1d> in <lambda>(x)
----> 1 A.apply(lambda x: np.exp(x)/(1+np.exp(x)))
~/.local/lib/python3.6/site-packages/pandas/core/series.py in __array_ufunc__(self, ufunc, method, *inputs, **kwargs)
724
725 inputs = tuple(extract_array(x, extract_numpy=True) for x in inputs)
--> 726 result = getattr(ufunc, method)(*inputs, **kwargs)
727
728 name = names[0] if len(set(names)) == 1 else None
TypeError: loop of ufunc does not support argument 0 of type float which has no callable exp method
发布于 2022-07-04 04:20:57
看起来是数据类型问题。
df = pd.DataFrame({'B':[0.784,2.123], 'C':[-1.6745,-2.8934]})
print(df.info())
信息:
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 2 entries, 0 to 1
Data columns (total 2 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 B 2 non-null float64
1 C 2 non-null float64
dtypes: float64(2)
memory usage: 160.0 bytes
适用范围:
df.apply(lambda x: np.exp(x)/(1+np.exp(x)))
print(df)
结果:
B C
0 0.784 -1.6745
1 2.123 -2.8934
复制错误:
df = pd.DataFrame({'B':[0.784,2.123], 'C':[-1.6745,-2.8934]}, dtype=object)
df.apply(lambda x: np.exp(x)/(1+np.exp(x)))
https://stackoverflow.com/questions/72851483
复制相似问题