2

I am looking to remove all rows from the df that have ONLY numbers in the string

Here is an extract of the dataframe

                         qid    question_stemmed                                   target   question_length total_words
149952  1d53c9c017999b4f77e2    8430397824532987451912384179815150754023741609...   0              241              3

Is there a way i can do that?

I tried the below, but it will remove all rows that have numbers in the string (along with any other datatype). However, i am looking to see if i can remove all 'numeric ONLY' rows.

df['question_stemmed'] = df[df['question_stemmed'].str.contains(r'[^a-z]')]

Appreciate any help here

2
  • df = df[~df['question_stemmed'].str.match(r'\d+$')] or df = df[~df['question_stemmed'].str.match(r'[0-9]+$')] Commented Jul 15, 2020 at 7:07
  • Thanks @WiktorStribiżew i got a better solution for a larger problem from you on the other post :) Commented Jul 15, 2020 at 21:07

1 Answer 1

5

If we're only worrying about ASCII digits 0-9:

df = df[~df['question_stemmed'].str.isdigit()]

If we need to worry about unicode or digits in other languages:

df = df[~df['question_stemmed'].str.isnumeric()]

Pandas methods internally call the corresponding python methods. See What's the difference between str.isdigit, isnumeric and isdecimal in python? for an explanation of how these functions work.

Sign up to request clarification or add additional context in comments.

3 Comments

thanks , but for some strange, reason, it is assigning the value of qid to the question_stemmed column. it must be a very silly error. Trying to figure out what is causing this.. Any idea here?
@Shalin this doesn't do any column assignment, so the issue is likely elsewhere in your code. Not sure as I can't see it...
it was my error. I applied this df['question_stemmed'] = df[~df['question_stemmed'].str.isnumeric()] hence it was reassigning. works fine now. Thanks @cs95

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.