简体   繁体   English

python dataframe 小写切片

[英]python dataframe lowercase slicing

I have a dataframe.我有一个 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.但我首先想先将我的 column1 设置为全部小写。 So being the n00b that I am, I tried:因此,作为我的 n00b,我尝试了:

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

Of course the Python gods gave me no mercy and gave me an AttributeError.当然,Python 大神毫不留情,给了我一个AttributeError。 Any tips on how I can slice a dataframe based on a substring but first make everything into lowercase first?关于如何根据 substring 对 dataframe 进行切片但首先将所有内容变为小写的任何提示?

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我觉得下面的帖子非常接近我的答案,但我无法让它完全按照我在上面描述的方式工作: Python pandas dataframe with if condition

Thanks Python pros感谢 Python 专业人士

Try using apply()尝试使用 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')]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM