1

I have a dataframe with one column like this:

Value
xyz123
123
abc
def

I want to remove any rows that contain numbers so I end up with a dataframe like this:

Value
abc
def

I have tried

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

but I got this error:

"TypeError: 'method' object is not subscriptable"

4
  • 1
    Have you tried with df.filter method? Commented Feb 26, 2018 at 20:48
  • 1
    @UdayrajDeshmukh that's not what df.filter does... Commented Feb 26, 2018 at 20:50
  • 1
    have you somewhere bound df to something that isn't a dataframe? Otherwise - what you've shown should work... what does type(df) show you? Commented Feb 26, 2018 at 20:52
  • I didn't bind it anywhere except where it loaded the data from a .csv Commented Feb 26, 2018 at 21:05

2 Answers 2

6

Independent from what looks like an issue with variable naming, you could be more explicit about removing only rows with numbers:

df[~df.Value.str.contains(r'\d')]

  Value
2   abc
3   def

\d:

Matches any Unicode decimal digit (that is, any character in Unicode character category [Nd]). This includes [0-9], and also many other digit characters. If the ASCII flag is used only [0-9] is matched (but the flag affects the entire regular expression, so in such cases using an explicit [0-9] may be a better choice).

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

2 Comments

The OPs code should work - the error suggests that they've assigned df to something that isn't a dataframe... So until they sort that out - this code will produce the same error... it is an alternative way of doing it though when that part of it is sorted.
Completely agree - "I want to remove values containing numbers" - using this approach makes more sense to me and is more accurate than the criteria being "not a-z"...(which isn't the same thing)
3

IIUC isalpha

df[df.Value.str.isalpha()]
Out[2057]: 
  Value
2   abc
3   def

Comments

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.