【发布时间】:2021-10-27 02:46:40
【问题描述】:
我在 Jupyter notebook 中工作,我的理解是在最后一行调用
df_speed_full['cam_id'] == rand_cam_id 我将索引标记为True 和False,然后将它们传递给外部df_speed_full 以仅选择具有True 的那些。然而不知何故,我得到了这个。我希望能解释一下我在这里缺少什么。我尝试阅读其他答案,但没有点击
rand_row = df_speed_full.sample(random_state=42)
rand_cam_id = list(rand_row['cam_id'])
rand_cam_df = df_speed_full[df_speed_full['cam_id'] == rand_cam_id]
这就是我得到的:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
C:\Users\HENRYK~1\AppData\Local\Temp/ipykernel_18136/4206593172.py in <module>
----> 1 rand_cam_df = df_speed_full[df_speed_full['cam_id'] == rand_cam_id]
~\anaconda3\lib\site-packages\pandas\core\ops\common.py in new_method(self, other)
67 other = item_from_zerodim(other)
68
---> 69 return method(self, other)
70
71 return new_method
~\anaconda3\lib\site-packages\pandas\core\arraylike.py in __eq__(self, other)
30 @unpack_zerodim_and_defer("__eq__")
31 def __eq__(self, other):
---> 32 return self._cmp_method(other, operator.eq)
33
34 @unpack_zerodim_and_defer("__ne__")
~\anaconda3\lib\site-packages\pandas\core\series.py in _cmp_method(self, other, op)
5500
5501 with np.errstate(all="ignore"):
-> 5502 res_values = ops.comparison_op(lvalues, rvalues, op)
5503
5504 return self._construct_result(res_values, name=res_name)
~\anaconda3\lib\site-packages\pandas\core\ops\array_ops.py in comparison_op(left, right, op)
259 # The ambiguous case is object-dtype. See GH#27803
260 if len(lvalues) != len(rvalues):
--> 261 raise ValueError(
262 "Lengths must match to compare", lvalues.shape, rvalues.shape
263 )
ValueError: ('Lengths must match to compare', (229025,), (1,))
【问题讨论】: