熊猫使用正则表达式替换

时间:2020-01-16 06:37:48

标签: python regex pandas

我有一列包含以字符串形式写的空值/缺失值的列,例如“没有分类”,“未知:没有准确的分类”以及其他变体。我想用None替换所有这些值。

我已经尝试过了,但是没有用:

df['Fourth level classification'] = df['Fourth level classification'].replace(
    to_replace=r'.*[Tt]here is no .*', value=None, regex=True
)

此外,如何使整个to_replace字符串大小写不敏感,使其也与“这里没有分类”等匹配?

1 个答案:

答案 0 :(得分:0)

您可以尝试以下方法:

df['Fourth level classification'] = (df['Fourth level classification']
                                    .str
                                    .lower()
                                    .replace(r'(.*(there is no).*)', pd.isna, regex=True))