Moosa
Moosa

Reputation: 3216

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]

Upvotes: 6

Views: 1538

Answers (1)

user2285236
user2285236

Reputation:

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') 

Upvotes: 8

Related Questions