我有一个数据库列表,我想遍历df的列,用单词“data_terms”替换与data_terms中的任何项匹配的所有实例。我尝试过使用np.select:
discrete_distributed_bar["profile_standardized"] = np.select(
[discrete_distributed_bar["profile_standardized"].isin(data_terms)],
["database"],
default=discrete_distributed_bar.profile_standardized,
)并且,使用np.where:
for index, row in discrete_distributed_bar["profile_standardized"].items():
np.where(
discrete_distributed_bar["profile_standardized"].isin(data_terms),
"database",
row,
)但这种替代并没有真正发生。这里我漏掉了什么?
谢谢你的帮助!
发布于 2020-09-08 13:14:31
这里似乎应该简化解决方案:
discrete_distributed_bar["profile_standardized"] = discrete_distributed_bar["profile_standardized"].replace(data_terms, 'database')但我认为数据存在一些问题(例如空格),请通过以下方式进行测试:
print (discrete_distributed_bar["profile_standardized"].isin(data_terms))然后是可能的用途:
discrete_distributed_bar["profile_standardized"] = discrete_distributed_bar["profile_standardized"].str.strip().replace(data_terms, 'database')https://stackoverflow.com/questions/63787427
复制相似问题