pandas 数据框删除常量列 [英] pandas dataframe remove constant column

查看:47
本文介绍了 pandas 数据框删除常量列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据框,可能有也可能没有具有相同值的列.例如

I have a dataframe that may or may not have columns that are the same value. For example

    row    A    B
    1      9    0
    2      7    0
    3      5    0
    4      2    0

我只想返回

   row    A  
   1      9    
   2      7    
   3      5    
   4      2

是否有一种简单的方法来识别这些列是否存在,然后将其删除?

Is there a simple way to identify if any of these columns exist and then remove them?

推荐答案

我相信此选项将比此处的其他答案更快,因为它会遍历数据帧一次,以进行比较,如果不唯一,则会短路找到了价值.

I believe this option will be faster than the other answers here as it will traverse the data frame only once for the comparison and short-circuit if a non-unique value is found.

>>> df

   0  1  2
0  1  9  0
1  2  7  0
2  3  7  0

>>> df.loc[:, (df != df.iloc[0]).any()] 

   0  1
0  1  9
1  2  7
2  3  7

这篇关于 pandas 数据框删除常量列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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