从字符串中消除数字并保留几个字符

时间:2019-05-12 18:27:25

标签: python pandas

尝试从这样的字符串中消除数字:

'THEO-GREY3JOY' should become 'THEO-GREYJOY'
'JON SNOW4TARGARYEN' should become 'JON SNOW TARGARYEN'

或者如果它们已经是没有数字的字符串,那就放他们走吧。

我一直尝试到现在,但是我剪了空格和“-”。不好。

Date_Neprel = pd.read_excel('1st_Incercare.xlsx')
Nume_ColumnPosition = Date_Neprel.columns.get_loc('Nume')
Prenume_ColumnPosition = Date_Neprel.columns.get_loc('Prenume')
for index,row in Date_Neprel.iterrows():
    Date_Neprel.iloc[index,Nume_ColumnPosition] = re.sub(r'[^a-zA-Z ]+', '', row['Nume'])
    Date_Neprel.iloc[index,Prenume_ColumnPosition] = re.sub(r'[^a-zA-Z ]+', '', row['Prenume']) 

3 个答案:

答案 0 :(得分:2)

尝试一下:

import re
text = 'THEO-GREY3JOY JON SNOW4TARGARYEN'

result = re.sub(r'\d+', '', text)

print(result)

输出:

THEO-GREYJOY JON SNOWTARGARYEN

答案 1 :(得分:2)

尝试以下方法:

Date_Neprel['Nume'] = Date_Neprel['Nume'].str.replace("[0-9]", "")
Date_Neprel['Prenume'] = Date_Neprel['Prenume'].str.replace("[0-9]", "")

答案 2 :(得分:0)

我认为可能有多种方法来处理这种情况。

希望对您有帮助。

s1 = "THEO-GREY3JOY"
s2 = "JON SNOW4TARGARYEN"

new_string = ''.join([i for i in s1 if not i.isdigit()])
print (new_string)

new_string = ''.join([i for i in s2 if not i.isdigit()])
print (new_string)