我有一个数据框:
df = pd.DataFrame({"ID": ["ID1", "ID2", "ID3",
"ID4", "ID5"],
"Item": ["Item1", "Item2", "Item3",
"Item4","Item5"],
"Catalog1": ["cat1", "1Cat12", "Cat35",
"1cat3","Cat5"],
"Catalog2": ["Cat11", "Cat12", "Cat35",
"1Cat1","2cat5"],
"Catalog3": ["cat6", "Ccat2", "1Cat9",
"1cat3","Cat7"],
"Price": ["716", "599", "4400",
"150","139"]})
我需要找到所有在名称以 Catalog 开头的列中包含字符串“cat1”或“Cat1”的行(这些列的数量可能有所不同,因此我不能只列出它们)。
我试过:
filter_col = [col for col in df if col.startswith('Catalog')]
df_res = df.loc[(filter_col.str.contains('(?i)cat1'))]
但我犯了错误:
AttributeError:'list' 对象没有属性 'str'