在 Pandas 中删除以特定字符串结尾的多列 [英] Drop multiple columns that end with certain string in Pandas

查看:67
本文介绍了在 Pandas 中删除以特定字符串结尾的多列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含很多列使用后缀_o"的数据框.有没有办法删除标签末尾带有_o"的所有列?

I have a dataframe with a lot of columns using the suffix '_o'. Is there a way to drop all the columns that has '_o' in the end of its label?

这篇文章中,我看到了一种方法使用过滤器功能删除以某些内容开头的列.但是如何删除以某事结尾的那些?

In this post I've seen a way to drop the columns that start with something using the filter function. But how to drop the ones that end with something?

推荐答案

有几种方法.

潘多尼克

df = df.loc[:, ~df.columns.str.endswith('_o')]]

df = df[df.columns[~df.columns.str.endswith('_o')]]]

列表推导式

df = df[[x for x in df if not x.endswith('_o')]]

df = df.drop([x for x in df if x.endswith('_o')], 1)

这篇关于在 Pandas 中删除以特定字符串结尾的多列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆