从数据框中的字符串中删除字符

1 python regex string character python-3.x

python初学者在这里。我想在某些条件下更改数据框中列中的一些字符。

\n\n

数据框如下所示:

\n\n
import pandas as pd\nimport numpy as np\nraw_data = {\'name\': [\'Willard Morris\', \'Al Jennings\', \'Omar Mullins\', \'Spencer McDaniel\'],\n                      \'age\': [20, 19, 22, 21],\n                      \'favorite_color\': [\'blue (VS)\', \'red\', \'yellow (AG)\', "green"],\n                      \'grade\': [88, 92, 95, 70]}\ndf = pd.DataFrame(raw_data, index = [\'0\', \'1\', \'2\', \'3\'])\ndf\n
Run Code Online (Sandbox Code Playgroud)\n\n

我的目标是替换姓氏列中的空格,后跟括号和两个字母。

\n\n

蓝色而不是蓝色(VS)。

\n\n

我必须删除 26 个字母变体,但只有一种格式:last_name 后跟空格,后跟括号,后跟两个字母,后跟括号。\n根据我的理解,应该是在正则表达式中:

\n\n
( \\(..\\)\n
Run Code Online (Sandbox Code Playgroud)\n\n

我尝试使用 str.replace 但它仅适用于精确匹配,并且会替换整个值。\n我也尝试过:

\n\n
df.loc[df[\'favorite_color\'].str.contains(\xe2\x80\x98VS\xe2\x80\x99), \'favorite_color\'] = \xe2\x80\x98random\xe2\x80\x99\n
Run Code Online (Sandbox Code Playgroud)\n\n

它还取代了整个值。

\n\n

我看到我只能重写该值,但我也看到使用这个:

\n\n
df[0].str.slice(0, -5)\n
Run Code Online (Sandbox Code Playgroud)\n\n

我可以删除包含我的搜索的字符串的最后 5 个字符。

\n\n

在我看来,我应该列出要删除的 26 个出现的位置,并通过该列进行解析以删除这些出现的位置,同时保留之前的文本。我搜索了与我的问题类似的帖子,但找不到解决方案。你对方向有什么想法吗?

\n

Rak*_*esh 5

您可以str.replace与模式一起使用"(\(.*?\))"

前任:

import pandas as pd

raw_data = {'name': ['Willard Morris', 'Al Jennings', 'Omar Mullins', 'Spencer McDaniel'],
                      'age': [20, 19, 22, 21],
                      'favorite_color': ['blue (VS)', 'red', 'yellow (AG)', "green"],
                      'grade': [88, 92, 95, 70]}
df = pd.DataFrame(raw_data, index = ['0', '1', '2', '3'])
df["newCol"] = df["favorite_color"].str.replace("(\(.*?\))", "").str.strip()
print( df )
Run Code Online (Sandbox Code Playgroud)

输出:

   age favorite_color  grade              name  newCol
0   20      blue (VS)     88    Willard Morris    blue
1   19            red     92       Al Jennings     red
2   22    yellow (AG)     95      Omar Mullins  yellow
3   21          green     70  Spencer McDaniel   green
Run Code Online (Sandbox Code Playgroud)