Pandas DataFrame筛选包含某个关键词的行/列

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Pandas DataFrame筛选包含某个关键词的行/列相关的知识,希望对你有一定的参考价值。

参考技术A 首先把需要筛选的列转化为pandas.core.strings.StringMethods,然后再用contains函数来得到布尔值的 (因为直接取行/列为Series对象,而不能直接对Series对象使用contains)Series:

筛选可以直接利用[ ]来完成:

同理,可以使用str函数来对DataFrame的的行/列做对于字符串的操作:

s.str.lower()
s.str.upper()
s.str.len()
s.str.strip()
s.str.split(' ')
s.str.replace('@','$')
s.str.count()
s.str.startswith()
s.str.endswith()
s.str.find()
s.str.findall()
s.str.swapcase()
s.str.isupper()
s.str.islower()
s.str.isnumeric()

以上是关于Pandas DataFrame筛选包含某个关键词的行/列的主要内容,如果未能解决你的问题,请参考以下文章

pandas筛选dataframe数据中指定数据列的内容包含在指定列表中的所有数据列

pandas筛选dataframe数据中指定数据列的内容包含在指定列表中的所有数据列

pandas筛选

pandas基于dataframe字符串数据列包含(contains)特定字符串来筛选dataframe中的数据行(rows where values contain substring)

pandas基于dataframe字符串数据列不包含特定字符串来筛选dataframe中的数据行(rows where values do not contain substring)

pandas筛选dataframe数据:筛选指定字符串数据列中包含A字符或者B字符的所有数据行(包含指定字符中的至少其中一个的所有数据行)