gpt4 book ai didi

python - 使用 .loc 和 OR 运算符返回 ValueError

转载 作者:行者123 更新时间:2023-12-01 07:15:07 25 4
gpt4 key购买 nike

我正在尝试在两列中的任意一列中搜索特定值,当找到目标值时,将第三列中的数字从正数更改为负数或从负数更改为正数。

te1 = df.loc[df['Transaction Event'] == 'Exercise']
te2 = df.loc[df['Transaction Event'] == 'Assignment']
te3 = df.loc[df['Transaction Event'] == 'Expiration']
an1 = df.loc[df['Action'] == 'Delete']
nq = df['Net Quantity']
var1 = df[(df['Transaction Event'] == 'Exercise') | (df['Transaction Event'] == 'Assignment') | (df['Transaction Event'] == 'Expiration') | (df['Action'] == 'Delete')]

df.loc[df[var1], nq] = df.loc[df[var1], nq] * -1

运行此代码会返回以下错误:

---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-282-01dbb8066276> in <module>()
6 var1 = df[(df['Transaction Event'] == 'Exercise') | (df['Transaction Event'] == 'Assignment') | (df['Transaction Event'] == 'Expiration') | (df['Action'] == 'Delete')]
7
----> 8 df.loc[df[var1], nq] = df.loc[df[var1], nq] * -1
9 print(df)

C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\frame.py in __getitem__(self, key)
1958 return self._getitem_array(key)
1959 elif isinstance(key, DataFrame):
-> 1960 return self._getitem_frame(key)
1961 elif is_mi_columns:
1962 return self._getitem_multilevel(key)

C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\frame.py in _getitem_frame(self, key)
2034 if key.values.size and not is_bool_dtype(key.values):
2035 raise ValueError('Must pass DataFrame with boolean values only')
-> 2036 return self.where(key)
2037
2038 def query(self, expr, inplace=False, **kwargs):

C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\generic.py in where(self, cond, other, inplace, axis, level, try_cast, raise_on_error)
5338 other = com._apply_if_callable(other, self)
5339 return self._where(cond, other, inplace, axis, level, try_cast,
-> 5340 raise_on_error)
5341
5342 @Appender(_shared_docs['where'] % dict(_shared_doc_kwargs, cond="False",

C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\generic.py in _where(self, cond, other, inplace, axis, level, try_cast, raise_on_error)
5096 for dt in cond.dtypes:
5097 if not is_bool_dtype(dt):
-> 5098 raise ValueError(msg.format(dtype=dt))
5099
5100 cond = cond.astype(bool, copy=False)

ValueError: Boolean array expected for the condition, not float64

有谁知道这个错误是什么原因造成的吗?

最佳答案

执行此操作时,您不是在创建 mask ,而是在选择 df 的子集:

var1 = df[(df['Transaction Event'] == 'Exercise') | (df['Transaction Event'] == 'Assignment') | (df['Transaction Event'] == 'Expiration') | (df['Action'] == 'Delete')]

相反,你只需要这个:

var1 = (df['Transaction Event'] == 'Exercise') | (df['Transaction Event'] == 'Assignment') | (df['Transaction Event'] == 'Expiration') | (df['Action'] == 'Delete')

在当前代码中,您创建了所需的 bool 数组,而且还使用该数组对原始 df 进行索引。您可以确认是否查看当前代码的 var1 中实际包含的内容。

关于python - 使用 .loc 和 OR 运算符返回 ValueError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58015148/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com