How to select all columns, except one column in pandas?

后端 未结 9 1765
不思量自难忘°
不思量自难忘° 2020-11-29 14:51

I have a dataframe look like this:

import pandas
import numpy as np
df = DataFrame(np.random.rand(4,4), columns = list(\'abcd\'))
df
      a         b                


        
相关标签:
9条回答
  • 2020-11-29 15:19

    I think a nice solution is with the function filter of pandas and regex (match everything except "b"):

    df.filter(regex="^(?!b$)")
    
    0 讨论(0)
  • Here is another way:

    df[[i for i in list(df.columns) if i != '<your column>']]
    

    You just pass all columns to be shown except of the one you do not want.

    0 讨论(0)
  • 2020-11-29 15:21

    Another slight modification to @Salvador Dali enables a list of columns to exclude:

    df[[i for i in list(df.columns) if i not in [list_of_columns_to_exclude]]]
    

    or

    df.loc[:,[i for i in list(df.columns) if i not in [list_of_columns_to_exclude]]]
    
    0 讨论(0)
  • 2020-11-29 15:24

    Here is a one line lambda:

    df[map(lambda x :x not in ['b'], list(df.columns))]
    

    before:

    import pandas
    import numpy as np
    df = pd.DataFrame(np.random.rand(4,4), columns = list('abcd'))
    df
    
           a           b           c           d
    0   0.774951    0.079351    0.118437    0.735799
    1   0.615547    0.203062    0.437672    0.912781
    2   0.804140    0.708514    0.156943    0.104416
    3   0.226051    0.641862    0.739839    0.434230
    

    after:

    df[map(lambda x :x not in ['b'], list(df.columns))]
    
            a          c          d
    0   0.774951    0.118437    0.735799
    1   0.615547    0.437672    0.912781
    2   0.804140    0.156943    0.104416
    3   0.226051    0.739839    0.434230
    
    0 讨论(0)
  • 2020-11-29 15:29

    Don't use ix. It's deprecated. The most readable and idiomatic way of doing this is df.drop():

    >>> df
    
              a         b         c         d
    0  0.175127  0.191051  0.382122  0.869242
    1  0.414376  0.300502  0.554819  0.497524
    2  0.142878  0.406830  0.314240  0.093132
    3  0.337368  0.851783  0.933441  0.949598
    
    >>> df.drop('b', axis=1)
    
              a         c         d
    0  0.175127  0.382122  0.869242
    1  0.414376  0.554819  0.497524
    2  0.142878  0.314240  0.093132
    3  0.337368  0.933441  0.949598
    

    Note that by default, .drop() does not operate inplace; despite the ominous name, df is unharmed by this process. If you want to permanently remove b from df, do df.drop('b', inplace=True).

    df.drop() also accepts a list of labels, e.g. df.drop(['a', 'b'], axis=1) will drop column a and b.

    0 讨论(0)
  • 2020-11-29 15:32

    When the columns are not a MultiIndex, df.columns is just an array of column names so you can do:

    df.loc[:, df.columns != 'b']
    
              a         c         d
    0  0.561196  0.013768  0.772827
    1  0.882641  0.615396  0.075381
    2  0.368824  0.651378  0.397203
    3  0.788730  0.568099  0.869127
    
    0 讨论(0)
提交回复
热议问题