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
Try using apply()
data_df[data_df['column1'].apply(str.lower).str.contains('test')]
You can drop the apply:
data_df[data_df['column1'].str.lower().str.contains('test')]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.