gpt4 book ai didi

python - Pandas 数据框替换列的多个子字符串

转载 作者:行者123 更新时间:2023-12-05 01:03:55 26 4
gpt4 key购买 nike

下面是代码

import pandas as pd

df = pd.DataFrame({'A': ['$5,756', '3434', '$45', '1,344']})

pattern = ','.join(['$', ','])

df['A'] = df['A'].str.replace('$|,', '', regex=True)
print(df['A'])

我试图删除每次出现的“$”或“,”...所以我试图用空白替换..

但它只是替换,

我得到的输出

0    $5756
1 3434
2 $45
3 1344$

应该是

0    5756
1 3434
2 45
3 1344

我做错了什么

感谢任何帮助

谢谢

最佳答案

使用:

import pandas as pd

df = pd.DataFrame({'A': ['$5,756', '3434', '$45', '1,344']})
df['A'] = df['A'].str.replace('[$,]', '', regex=True)
print(df)

输出

      A
0 5756
1 3434
2 45
3 1344

问题是字符 $ 在正则表达式中有特殊含义。来自documentation (强调我的):

$
Matches the end of the string or just before the newline at the endof the string, and in MULTILINE mode also matches before a newline.foo matches both ‘foo’ and ‘foobar’, while the regular expression foo$matches only ‘foo’. More interestingly, searching for foo.$ in'foo1\nfoo2\n' matches ‘foo2’ normally, but ‘foo1’ in MULTILINE mode;searching for a single $ in 'foo\n' will find two (empty) matches: onejust before the newline, and one at the end of the string.mode;searching for a single $ in 'foo\n' will find two (empty) matches: onejust before the newline, and one at the end of the string.

因此您需要转义字符或将其放入字符类中。

作为替代用途:

df['A'].str.replace('\$|,', '', regex=True)  # note the escaping \

关于python - Pandas 数据框替换列的多个子字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73137976/

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