【发布时间】:2021-12-20 14:01:19
【问题描述】:
假设我有整数值 0 t0 100 的数据框。 我想将这些值分为 3 个部分,low、mid 和 high,其中 low 小于 33,high 大于 66 和中值在 33 到 66 之间。 所以我用
df['low'] = df['int'] <= 33
df['mid'] = 33 < df['int'] < 66
df['high'] = df['int'] >= 66
我得到错误
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_12080/1299746928.py in <module>
1 df['low'] = df['int'] <= 33
----> 2 df['mid'] = 33 < df['int'] < 66
3 df['high'] = df['int'] >= 66
c:\program files\python37\lib\site-packages\pandas\core\generic.py in __nonzero__(self)
1536 def __nonzero__(self):
1537 raise ValueError(
-> 1538 f"The truth value of a {type(self).__name__} is ambiguous. "
1539 "Use a.empty, a.bool(), a.item(), a.any() or a.all()."
1540 )
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
我已经尝试过 if else 语句以及and 和其他运算符。
low 和 high 都可以,但是 mid 不行。
请问有什么办法吗?
【问题讨论】: