首页 > 解决方案 > Pandas 使用搜索字符串更新列

问题描述

您好,我正在使用 pandas 练习,我正在尝试弄清楚如何在匹配“费用”时将类别字段设置为费用。我尝试了一种无效的掩蔽技术。谢谢

df = g1_2019[g1_2019['Description'].str.contains('Fee')]
df.head()

Index   Date    No. Description Category
495 3/18/2019   0   Withdrawal RW-ISA:Fee: International Service Fee    Not Categorized
496 3/18/2019   0   International Service Fee Assessed = $0.07 on ...   Not Categorized
650 4/17/2019   0   Withdrawal RW-ISA:Fee: International Service Fee    Not Categorized
651 4/17/2019   0   International Service Fee Assessed = $0.07 on ...   Not Categorized
879 5/17/2019   0   Withdrawal RW-ISA:Fee: International Service Fee    Not Categorized

标签: pythonpandas

解决方案


尝试:

mask = g1_2019['Description'].str.contains('Fee')
g1_2019.loc[mask, "Category"] = "Fees"

推荐阅读