我试图计算包含浮动列表的dataframe列的平均值和标准偏差。我不认为我需要提取每个列表来计算它,所以我尝试在dataframe中进行操作。 令人惊讶的是,我找不到关于这个特定主题的任何东西
下面是一个玩具示例来说明我的问题:
l = pd.DataFrame({'D' : [[4,5,6,6,6],[6,8,8,3]], 'R' : [[3,5,6,4,6],[6,9,9,3]]})
l1 = l.apply(pd.to_numeric).mean()
l2 = l.apply(pd.to_numeric).std()
我得到以下错误:
Traceback (most recent call last):
File "pandas/_libs/lib.pyx", line 1892, in pandas._libs.lib.maybe_convert_numeric
TypeError: Invalid object type
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/Users/pierre/Desktop/Project_inv/pr.py", line 8, in <module>
l1 = l.apply(pd.to_numeric).mean()
File "/Users/pierre/PycharmProjects/untitled22/venv/lib/python3.7/site-packages/pandas/core/frame.py", line 6487, in apply
return op.get_result()
File "/Users/pierre/PycharmProjects/untitled22/venv/lib/python3.7/site-packages/pandas/core/apply.py", line 151, in get_result
return self.apply_standard()
File "/Users/pierre/PycharmProjects/untitled22/venv/lib/python3.7/site-packages/pandas/core/apply.py", line 257, in apply_standard
self.apply_series_generator()
File "/Users/pierre/PycharmProjects/untitled22/venv/lib/python3.7/site-packages/pandas/core/apply.py", line 286, in apply_series_generator
results[i] = self.f(v)
File "/Users/pierre/PycharmProjects/untitled22/venv/lib/python3.7/site-packages/pandas/core/tools/numeric.py", line 135, in to_numeric
coerce_numeric=coerce_numeric)
File "pandas/_libs/lib.pyx", line 1925, in pandas._libs.lib.maybe_convert_numeric
TypeError: ('Invalid object type at position 0', 'occurred at index D')
我不确定是什么问题,有没有人能告诉我如何着手解决这个问题
首先,我认为在熊猫中与
list
合作不是good idea但是确实需要它,通过^{} 处理元素是否可能:
因此,我建议首先将列表展平,例如,对pandas 0.25+使用^{} ,然后处理:
相关问题 更多 >
编程相关推荐