【发布时间】:2020-12-26 05:08:31
【问题描述】:
我正在尝试在 pandas 的列上使用简单的 .apply 函数,这会产生此错误。很快就需要这方面的帮助。这是我的第一个问题,请忽略我的编辑和结构问题。
data = {'A':[1, 2, 3],
'B':[4, 5, 6],
'C':[7, 8, 9] }
df = pd.DataFrame(data)
df['add'] = df.apply(lambda row : row['A'] + row['B'] + row['C'], axis = 1)
我在 Pandas 中的任何简单的 .apply() 函数中都收到此错误。列上的任何 apply() 都会导致此问题。
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<command-2941072546244745> in <module>
3 'C':[7, 8, 9] }
4 df = pd.DataFrame(data)
----> 5 df['add'] = df.apply(lambda row : row['A'] + row['B'] + row['C'], axis = 1)
/databricks/python/lib/python3.7/site-packages/pandas/core/frame.py in apply(self, func, axis, raw, result_type, args, **kwds)
6876 format.
6877 DataFrame.pivot_table : Create a spreadsheet-style pivot table
-> 6878 as a DataFrame.
6879
6880 Notes
/databricks/python/lib/python3.7/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):
/databricks/python/lib/python3.7/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
/databricks/python/lib/python3.7/site-packages/pandas/core/apply.py in apply_series_generator(self)
301 else:
302 with option_context("mode.chained_assignment", None):
--> 303 for i, v in enumerate(series_gen):
304 # ignore SettingWithCopy here in case the user mutates
305 results[i] = self.f(v)
/databricks/python/lib/python3.7/site-packages/pandas/core/apply.py in series_generator(self)
410 # of it. Kids: don't do this at home.
411 ser = self.obj._ixs(0, axis=0)
--> 412 mgr = ser._mgr
413 blk = mgr.blocks[0]
414
/databricks/python/lib/python3.7/site-packages/pandas/core/generic.py in __getattr__(self, name)
5272 """
5273 Return a Numpy representation of the DataFrame.
-> 5274
5275 .. warning::
5276
AttributeError: 'Series' object has no attribute '_mgr'
【问题讨论】:
-
请包含导致错误的代码,并将错误作为文本而不是图像。
-
另外,请将完整的错误复制为文本并将其包含在内。谢谢。
-
add是什么?