我有一个204行x507列的数据集。据我所知,为了删除包含单词“Dog”的列,例如,您可以使用:
df.loc[:,~df.columns.str.startswith('Dog')]
我还了解到,您可以通过使用以下“str.contains”代码,使用多个条件删除列,例如“Dog”和“Cat”:
df.loc[:,~df.columns.str.contains('Dog|Cat')]
但是,当您将“|”(or)函数应用于下面的“columns.str.startswith”时,它似乎不起作用,并且输出为204行x 0列
df.loc[:,~df.columns.str.startswith('Dog|Cat')]
Out204 rows × 0 columns
这是为什么?是否有一种方法可以使用“df.columns.str.startswith”函数使用多个条件删除列?
对于一些字符串,我更喜欢正则表达式方法:
如果要在^{} 中使用多个值,请使用
tuple
如下:相关问题 更多 >
编程相关推荐