Python 如何删除整数列中出现的垃圾字符串

Python 如何删除整数列中出现的垃圾字符串,python,pandas,Python,Pandas,我有一列整数(示例行:123456789),其中一些值散布着垃圾字母。例如:1234y5678。我想删除这些单元格中出现的字母表并保留数字。我如何使用熊猫呢 假设我的数据帧是df,列名是mobile 我是否应该使用np.where以及df[df['mobile'].str.contains('a-z')]等条件并使用字符串替换?使用pd.Series.str.replace: import pandas as pd s = pd.Series(['125109a181', '1361q1j1'

我有一列整数(示例行:
123456789
),其中一些值散布着垃圾字母。例如:
1234y5678
。我想删除这些单元格中出现的字母表并保留数字。我如何使用熊猫呢

假设我的数据帧是
df
,列名是
mobile


我是否应该使用
np.where
以及
df[df['mobile'].str.contains('a-z')]
等条件并使用字符串替换?

使用
pd.Series.str.replace

import pandas as pd

s = pd.Series(['125109a181', '1361q1j1', '85198m4'])
s.str.replace('[a-zA-Z]', '').astype(int)
输出:

0    125109181
1       136111
2       851984

使用
pd.Series.str.replace

import pandas as pd

s = pd.Series(['125109a181', '1361q1j1', '85198m4'])
s.str.replace('[a-zA-Z]', '').astype(int)
输出:

0    125109181
1       136111
2       851984

如果垃圾字符不限于字母,则应使用以下选项:

yourSeries.str.replace('[^0-9]', '')

如果垃圾字符不限于字母,则应使用以下选项:

yourSeries.str.replace('[^0-9]', '')

使用正则表达式字符类
\D
(不是数字):


使用正则表达式字符类
\D
(不是数字):