简体   繁体   English

根据值删除Pandas中的DataFrame列

[英]Deleting DataFrame column in Pandas based on value

I have a dataframe something like this: 我有一个像这样的数据框:

    Col0    Col1    Col2    Col3
1   a       b       g       a
2   a       d       z       a
3   a       g       x       a
4   a       h       p       a
5   a       b       c       a

I need to remove the columns where the value is 'a'. 我需要删除值为'a'的列。 No other cells contain the value 'a'(Ex. Here Col1 and Col2 will have no cells with value 'a').I have around 1000 columns and I'm not really sure what all columns have the value 'a'. 没有其他单元格包含值'a'(例如,此处Col1和Col2将没有值为'a'的单元格。)我有大约1000列,我不确定所有列的值是否为'a'。 The dataframe required should be something like this., 所需的数据框应该是这样的。,

    Col1    Col2
1   b       g   
2   d       z    
3   g       x    
4   h       p    
5   b       c    

What's the best way to do this? 最好的方法是什么?

Use any if need check if at least one True or all if need check all True s with boolean indexing and loc , because filter columns: 使用any如果需要检查,如果至少一个Trueall ,如果需要检查所有的True与S boolean indexingloc ,因为过滤列:

print (df)
  Col0 Col1 Col2 Col3
0    a    a    g    a
1    a    d    z    a
2    a    g    x    a
3    a    h    p    a
4    a    b    c    a


df2 = df.loc[:, ~(df == 'a').any()]
print (df2)
  Col2
0    g
1    z
2    x
3    p
4    c

df1 = df.loc[:, ~(df == 'a').all()]
print (df1)
  Col1 Col2
0    a    g
1    d    z
2    g    x
3    h    p
4    b    c

Detail: 详情:

print (df == 'a')

   Col0   Col1   Col2  Col3
0  True   True  False  True
1  True  False  False  True
2  True  False  False  True
3  True  False  False  True
4  True  False  False  True

df2 = df.loc[:, (df != 'a').any()]
print (df2)
  Col1 Col2
0    a    g
1    d    z
2    g    x
3    h    p
4    b    c

df1 = df.loc[:, (df != 'a').all()]
print (df1)
  Col2
0    g
1    z
2    x
3    p
4    c

print (df != 'a')

    Col0   Col1  Col2   Col3
0  False  False  True  False
1  False   True  True  False
2  False   True  True  False
3  False   True  True  False
4  False   True  True  False

EDIT: 编辑:

For check mixed types - numeric with strings are 2 possible solutions convert all to string s or compare numpy arrays: 对于检查混合类型 - 带字符串的数字是2种可能的解决方案将所有转换为string s或比较numpy数组:

df.astype(str) == 'a'

Or: 要么:

df.values == 'a'

Option 1 选项1
Using pd.DataFrame.dropna with pd.DataFrame.mask pd.DataFrame.dropnapd.DataFrame.mask pd.DataFrame.dropna使用
The concept is that I replace 'a' with np.nan and then conveniently use dropna . 这个概念是我用np.nan替换'a'然后方便地使用dropna

This drops the column even it has one a . 即使它有一个a这也会使列掉落。

df.mask(df.astype(str).eq('a')).dropna(1)

  Col1 Col2
1    b    g
2    d    z
3    g    x
4    h    p
5    b    c

This requires that all elements of the column be a 这要求列的所有元素都是a

df.mask(df.astype(str).eq('a')).dropna(1, how='all')

  Col1 Col2
1    b    g
2    d    z
3    g    x
4    h    p
5    b    c

Option 2 选项2
Creative way using np.where to find the unique column positions that have 'a' 使用np.where具有'a'的唯一列位置的创造性方式
This is cool because np.where will return a tuple of arrays that give the positions of all True values in an array. 这很酷,因为np.where将返回一个数组元组,它给出数组中所有True值的位置。 The second array of the tuple will be all the column positions. 元组的第二个数组将是所有列位置。 I grab a unique set of those and find the other column names. 我抓住一组独特的,找到其他列名。

df[df.columns.difference(
       df.columns[np.unique(np.where(df.astype(str).eq('a'))[1]
)])]

  Col1 Col2
1    b    g
2    d    z
3    g    x
4    h    p
5    b    c

Or similarly with pd.DataFrame.drop 或者类似于pd.DataFrame.drop

df.drop(df.columns[np.unique(np.where(df.astype(str).eq('a'))[1])], 1)

  Col1 Col2
1    b    g
2    d    z
3    g    x
4    h    p
5    b    c

Option 3 选项3
Probably bad way of doing it. 这可能是糟糕的做法。

df.loc[:, ~df.astype(str).sum().str.contains('a')]

  Col1 Col2
1    b    g
2    d    z
3    g    x
4    h    p
5    b    c

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM