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

后端 未结 9 1793
不思量自难忘°
不思量自难忘° 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: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
    

提交回复
热议问题