当我运行以下命令时
import pandas as pd
def my_func(E, I):
return E * I
d = {'E': [1, 1], 'I': [2, 2]}
df = pd.DataFrame(data=d)
df['S'] = df.apply(lambda x: my_func(x['E'], x['I']),axis=1).map(lambda x: x[0])
我收到以下错误
Traceback (most recent call last):
File "U:\GSY\scipt.py", line 14, in <module>
df['S'] = df.apply(lambda x: my_func(x['E'], x['I']),axis=1).map(lambda x: x[0])
File "C:\Users\gsy.LSH\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\pandas\core\series.py", line 3630, in map
new_values = super()._map_values(arg, na_action=na_action)
File "C:\Users\gsy.LSH\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\pandas\core\base.py", line 1145, in _map_values
new_values = map_f(values, mapper)
File "pandas\_libs\lib.pyx", line 2329, in pandas._libs.lib.map_infer
File "U:\GSY\script.py", line 14, in <lambda>
df['S'] = df.apply(lambda x: my_func(x['E'], x['I']),axis=1).map(lambda x: x[0])
TypeError: 'int' object is not subscriptable
我 100% 确信这在过去是有效的,因为我已经广泛使用了它。有谁知道为什么会发生这种情况?
慕虎7371278
相关分类