1

I have a dataframe. I want to slice it by checking if the value contains a string. For example, this code works:

data_df[data_df['column1'].str.contains('test')]

But I first want to set my column1 to be all lowercase first. So being the n00b that I am, I tried:

data_df[data_df['column1'].lower().str.contains('test')]

Of course the Python gods gave me no mercy and gave me an AttributeError. Any tips on how I can slice a dataframe based on a substring but first make everything into lowercase first?

I feel like the following post is very close to my answer but I can't get it to work exactly how I described up there: Python pandas dataframe slicing, with if condition

Thanks Python pros!!!

1
  • 1
    You need str accessor before lower() as well: data_df[data_df['column1'].str.lower().str.contains('test')] Commented May 10, 2016 at 4:49

2 Answers 2

1

Try using apply()

data_df[data_df['column1'].apply(str.lower).str.contains('test')]
Sign up to request clarification or add additional context in comments.

Comments

0

You can drop the apply:

data_df[data_df['column1'].str.lower().str.contains('test')]

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.