Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

pandas

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         c         d 0  0.418762  0.042369  0.869203  0.972314 1  0.991058  0.510228  0.594784  0.534366 2  0.407472  0.259811  0.396664  0.894202 3  0.726168  0.139531  0.324932  0.906575 

How I can get all columns except column b?

like image 382
markov zain Avatar asked Apr 21 '15 05:04

markov zain


People also ask

How do I select all columns except two pandas?

You can use the following syntax to exclude columns in a pandas DataFrame: #exclude column1 df. loc[:, df.

How do I exclude one column from a Dataframe?

We can exclude one column from the pandas dataframe by using the loc function. This function removes the column based on the location. Parameters: dataframe: is the input dataframe.


1 Answers

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 
like image 177
Marius Avatar answered Oct 19 '22 12:10

Marius