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

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

    I think the best way to do is the way mentioned by @Salvador Dali. Not that the others are wrong.

    Because when you have a data set where you just want to select one column and put it into one variable and the rest of the columns into another for comparison or computational purposes. Then dropping the column of the data set might not help. Of course there are use cases for that as well.

    x_cols = [x for x in data.columns if x != 'name of column to be excluded']
    

    Then you can put those collection of columns in variable x_cols into another variable like x_cols1 for other computation.

    ex: x_cols1 = data[x_cols]
    
    0 讨论(0)
  • 2020-11-29 15:37
    df[df.columns.difference(['b'])]
    
    Out: 
              a         c         d
    0  0.427809  0.459807  0.333869
    1  0.678031  0.668346  0.645951
    2  0.996573  0.673730  0.314911
    3  0.786942  0.719665  0.330833
    
    0 讨论(0)
  • 2020-11-29 15:38

    You can use df.columns.isin()

    df.loc[:, ~df.columns.isin(['b'])]
    

    When you want to drop multiple columns, as simple as:

    df.loc[:, ~df.columns.isin(['col1', 'col2'])]
    
    0 讨论(0)
提交回复
热议问题