如何在Python中从dataframe列中的字符串中删除非字母数字字符?

如何在Python中从dataframe列中的字符串中删除非字母数字字符?,python,regex,pandas,dataframe,Python,Regex,Pandas,Dataframe,我有一个DF列,其中有许多字符串。我需要从该列中删除所有非字母数字字符:即: df['strings'] = ["a#bc1!","a(b$c"] 运行代码: Print(df['strings']): ['abc','abc'] 我试过: df['strings'].replace([',','.','/','"',':',';','!','@','#','$','%',"'","*","(",")","&",],"") 但这不起作用,我觉得应该有一种更有效的方法使用regex来

我有一个DF列,其中有许多字符串。我需要从该列中删除所有非字母数字字符:即:

df['strings'] = ["a#bc1!","a(b$c"]
运行代码:

Print(df['strings']): ['abc','abc']
我试过:

df['strings'].replace([',','.','/','"',':',';','!','@','#','$','%',"'","*","(",")","&",],"")

但这不起作用,我觉得应该有一种更有效的方法使用regex来实现这一点。任何帮助都将不胜感激。

使用
str.replace

df
  strings
0  a#bc1!
1   a(b$c

df.strings.str.replace('[^a-zA-Z]', '')
0    abc
1    abc
Name: strings, dtype: object

要保留字母数字字符(而不仅仅是预期输出中的字母),您需要:

df.strings.str.replace('\W', '')
0    abc1
1     abc
Name: strings, dtype: object 
您也可以使用regex

import re

regex = re.compile('[^a-zA-Z]')

l = ["a#bc1!","a(b$c"]

print [regex.sub('', i) for i in l]

['abc', 'abc']

由于您编写了字母数字,因此需要在正则表达式中添加0-9。 但也许你只想要字母

import pandas as pd

ded = pd.DataFrame({'strings': ['a#bc1!', 'a(b$c']})

ded.strings.str.replace('[^a-zA-Z0-9]', '')

但是基本上是COLDSPEED写的

是正确的,我必须加上0-9和空格,因为我想这样,但是COLDSPEED的答案是第一个,而且是正确的方法。