gpt4 book ai didi

python - 用 Pandas 替换字符串

转载 作者:太空宇宙 更新时间:2023-11-04 00:44:33 25 4
gpt4 key购买 nike

我有一个 pandas 列,其中包含一些字符串值,例如:

White bear
Brown Bear
Brown Bear 100 Kg
White bear 200 cm

如何检查所有包含序列“White bear”的字符串并将整个值(不仅是序列)替换为“White_bear”之类的字符串?

df['Species'] = df['Species'].str.replace('White bear', 'White_bear')   

不适合我,因为它只替换了序列。

最佳答案

您可以使用 bool 索引:

In [173]: df.loc[df.Species.str.contains(r'\bWhite\s+bear\b'), 'Species'] = 'White_bear'

In [174]: df
Out[174]:
Species
0 White_bear
1 Brown Bear
2 Brown Bear 100 Kg
3 White_bear

或更通用的解决方案:

In [204]: df
Out[204]:
Species
0 White bear
1 Brown Bear
2 Brown Bear 100 Kg
3 White bear 200 cm

In [205]: from_re = [r'.*?\bwhite\b\s+\bbear\b.*',r'.*?\bbrown\b\s+\bbear\b.*']

In [206]: to_re = ['White_bear','Brown_bear']

In [207]: df.Species = df.Species.str.lower().replace(from_re, to_re, regex=True)

In [208]: df
Out[208]:
Species
0 White_bear
1 Brown_bear
2 Brown_bear
3 White_bear

RegEx explanation

关于python - 用 Pandas 替换字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40319746/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com