熊猫用搜索字符串更新列

时间:2020-01-03 18:41:50

标签: python pandas

您好,我正在与熊猫一起练习,我试图弄清楚只要有“ Fee”上的匹配项,如何将类别字段设置为Fees。我尝试了一种无效的遮罩技术。谢谢

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

2 个答案:

答案 0 :(得分:3)

尝试:

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

答案 1 :(得分:0)

您可以考虑使用np.where

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