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
2

2 Answers 2

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
Sign up to request clarification or add additional context in comments.

Comments

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

3 Comments

I think A is already of string (object) dtype, so we can omit .astype(str)...
@MaxU dtype==object doesn't imply that all elements are strings. You can have an actual float or int in the array. If you do, str.isnumeric will fail.
this is a good point! I just have tested it - .isnumeric() will retyren NaN in this case

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.