在 pandas 中的字符串中更改单词中的字符

Changing a character in a word within a string in pandas

我有以下数据框:

description
#voice-artist,#creative-director
#designer,#asistant

我想将#替换为“”,将“,”替换为“,”

我做了以下操作,但它不起作用,即我得到了相同的字符串

df["description"] = df["description"].str.replace("#", "")
df["description"] = df["description"].str.replace(",", ", ")

我怎样才能得到我想要的?

您可以尝试使用 regex?

样本 DF:

>>> df
                        description
0  #voice-artist,#creative-director
1               #designer,#asistant

您的解决方案,只是 regex 暗示 ..

>>> df["description"] = df["description"].str.replace("#", "", regex=True)
>>> df["description"] = df["description"].str.replace(",", ", ", regex=True)
>>> df
                       description
0  voice-artist, creative-director
1               designer, asistant

或:

请尝试使用 Series.str.replace。如果您需要替换子字符串,它会很有用。

>>> df["description"] = df["description"].str.replace("#", "")
>>> df["description"] = df["description"].str.replace(",", ", ")
>>> df
                       description
0  voice-artist, creative-director
1               designer, asistant