如何在数据框列值中查找字符串

How to find a string in a dataframe column value

我有一个名为 tf_df 的数据框,它有多个列,包括一个名为 'Val_Combined' 的列。我想从 ``Val_Combinedcontain the string'VALID'` 的项目中创建一个新的数据框(它也可能包含其他字符)。我尝试了以下方法:

t1_valid_df = t1_df[(t1_df['Val_Combined'].contains('VALID'))]

这会引发错误:

AttributeError: 'Series' object has no attribute 'contains'

实现我的目标的正确方法是什么?

你应该使用 str.contains

试试这个。

t1_valid_df = t1_df[(t1_df['Val_Combined'].str.contains('VALID'))]