python dataframe pandas drop multiple column using column name

Question:

I am beginner to python
I understand that to drop a column you use df.drop(‘column name’, axis=1, inplace =True)

file format is in .csv file

I wan to use above syntax for large data sets and more in robust way

suppose I have 500 columns and I want to keep column no 100 to 140 using column name not by indices and rest want to drop , how would I write above syntax so that I can achieve my goal and also in 100 to 140 column , I want to drop column no 105, 108,110 by column name

Asked By: Praveen Rai

||

Answers:

Instead of using a string parameter for the column name, use a list of strings refering to the column names you want to delete.

Answered By: Benjamin Rio
df = df.loc[:, 'col_100_name' : 'col_140_name']

.loc always selects using both ends inclusive. Here I am selecting all rows, and only the columns that you want to select (by names).

After this (or before – it doesn’t matter) you can drop the other columns by names as usual:

df.drop(['col_105_name', 'col_108_name', 'col_110_name'], axis=1, inplace=True)
Answered By: Vladimir Fokow
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.