2

I have a column in pandas which has string and numbers mixed I want to strip numbers from the string.

A
11286011
11268163
C7DDA72897
C8ABC557
Abul
C80DAS577
C80DSS665

Want an output as

A
C7DDA72897
C8ABC557
Abul
C80DAS577
C80DSS665
Abul
  • 207
  • 1
  • 4
  • 14
  • 2
    Possible duplicate of [remove non-numeric rows in one column with pandas](http://stackoverflow.com/questions/33961028/remove-non-numeric-rows-in-one-column-with-pandas) – Andy Apr 13 '17 at 13:26
  • Possible duplicate of [Pandas select only numeric or integer field from dataframe](http://stackoverflow.com/questions/24883503/pandas-select-only-numeric-or-integer-field-from-dataframe) – dot.Py Apr 13 '17 at 13:27

2 Answers2

4
In [52]: df
Out[52]:
            A
0    11286011
1    11268163
2  C7DDA72897
3    C8ABC557
4   C80DAS577
5   C80DSS665

In [53]: df = pd.to_numeric(df.A, errors='coerce').dropna()

In [54]: df
Out[54]:
0    11286011.0
1    11268163.0
Name: A, dtype: float64

or using RegEx:

In [59]: df.loc[~df.A.str.contains(r'\D+')]
Out[59]:
          A
0  11286011
1  11268163
MaxU - stop genocide of UA
  • 191,778
  • 30
  • 340
  • 375
4

You can use .str.isnumeric to use in boolean slicing.

df[df.A.astype(str).str.isnumeric()]

          A
0  11286011
1  11268163

As pointed out by @MaxU, assuming every element is already a string, you can limit this to

df[df.A.str.isnumeric()]

          A
0  11286011
1  11268163
piRSquared
  • 265,629
  • 48
  • 427
  • 571