带有搜索字符串的熊猫更新列



您好,我正在和熊猫一起练习,我正在尝试弄清楚如何在"费用"匹配时将类别字段设置为费用。 我尝试了一种遮罩技术,但没有奏效。 谢谢

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

尝试:

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

您可以考虑使用np.where

import numpy as np
df["Description"] = np.where(df["Description"].str.contains("Fee"),
"Fees",
df["Description"])

最新更新