Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dropping columns in a dataframe

I'm on Python 2.7. I have a dataframe with 200 columns and need to drop a few.

I can use the below to drop the last n columns. How do I write it so i can drop the first 10, then column 22, then 26, 10th from the last, and last 5. All in one line.

df2 = df.iloc[:, :-5]
like image 306
Moosa Avatar asked May 01 '17 17:05

Moosa


People also ask

How do I drop columns from a data frame?

You can drop column in pandas dataframe using the df. drop(“column_name”, axis=1, inplace=True) statement.

How do I drop multiple columns in a DataFrame in Python?

We can use Pandas drop() function to drop multiple columns from a dataframe. Pandas drop() is versatile and it can be used to drop rows of a dataframe as well. To use Pandas drop() function to drop columns, we provide the multiple columns that need to be dropped as a list.


1 Answers

Use np.r_:

import numpy as np
df.drop(df.columns[np.r_[:10, 22, 26, -10, -5:0]], axis=1)

np.r_ concatenates several slices. For example, np.r_[1:3, 5, 7:9, -3:0] returns array([ 1, 2, 5, 7, 8, -3, -2, -1]). You can use this to index into df.columns. For a DataFrame of 40 columns (named A1:A40),

df.columns[np.r_[:3, 5, 7:9, -2:0]]
Out: Index(['A1', 'A2', 'A3', 'A6', 'A8', 'A9', 'A39', 'A40'], dtype='object')

And finally, since it takes index labels, you can pass this to df.drop. The resulting DataFrame will have the following columns:

df.drop(df.columns[np.r_[:3, 5, 7:9, -2:0]], axis=1).columns
Out: 
Index(['A4', 'A5', 'A7', 'A10', 'A11', 'A12', 'A13', 'A14', 'A15', 'A16',
       'A17', 'A18', 'A19', 'A20', 'A21', 'A22', 'A23', 'A24', 'A25', 'A26',
       'A27', 'A28', 'A29', 'A30', 'A31', 'A32', 'A33', 'A34', 'A35', 'A36',
       'A37', 'A38'],
      dtype='object') 
like image 126
ayhan Avatar answered Oct 13 '22 19:10

ayhan