pandas 如果满足df配置,则将x值添加到变量

cnh2zyt3  于 2022-12-16  发布在  其他
关注(0)|答案(1)|浏览(148)

我想尝试的是,如果满足df配置,则将x值添加到变量中
范例

local_bid = 0
df.loc[["Entity"] == "Keyword"]

那么

local_bid = df["Bid"]

我试过了

df.loc[["Entity"] == "Keyword", local_bid] = df["Bid"]

但没有成功

Traceback (most recent call last):
  File "/home/shaumne/Desktop/zorba/Sp_limpr.py", line 17, in <module>
    s =limpr.loc[["Entity"] == "Keyword", local_bid] = limpr["Bid"]
  File "/home/shaumne/.local/lib/python3.10/site-packages/pandas/core/indexing.py", line 818, in __setitem__
    iloc._setitem_with_indexer(indexer, value, self.name)
  File "/home/shaumne/.local/lib/python3.10/site-packages/pandas/core/indexing.py", line 1703, in _setitem_with_indexer
    key, _ = convert_missing_indexer(idx)
  File "/home/shaumne/.local/lib/python3.10/site-packages/pandas/core/indexing.py", line 2585, in convert_missing_indexer
    raise KeyError("cannot use a single bool to index into setitem")
KeyError: 'cannot use a single bool to index into setitem'
qlfbtfca

qlfbtfca1#

您需要比较列df["Entity"],而不是列出["Entity"]

local_bid = 0

df.loc[df["Entity"] == "Keyword", local_bid] = df["Bid"]

相关问题