Pandas Drop Multiple Columns From DataFrame

  • Post author:
  • Post category:Pandas
  • Post last modified:October 5, 2023
  • Reading time:9 mins read

In this pandas drop multiple columns article, I will explain how to remove/delete/drop multiple columns from DataFrame with examples. drop() method is used to remove columns and rows according to the specific column(label) names and indexes corresponding axes.

Note that the drop() method by default returns a DataFrame(copy) after dropping specified columns. In case you want to remove columns in place then you should use inplace=True.

1. Quick Examples of Pandas Drop Multiple Columns

Below are some quick examples of how to drop multiple columns from Pandas DataFrame.


# Below are quick examples

# Drop multiple columns by Name
df.drop(["Courses", "Fee"], axis = 1, inplace=True)

# Drop multiple columns by Index
df.drop(df.columns[[1,2]], axis = 1, inplace=True)

# Drop multiple columns between two columns
df.loc[:, 'Courses':'Fee'].columns, axis = 1, inplace=True)

# Drop multiple columns between two indexes
df.drop(df.iloc[:, 1:2], axis=1, inplace=True)

2. Pandas Drop Multiple Columns

In Pandas Dataframe sometimes you would be required to drop multiple columns by name, index, within the range of index, and between two columns, all these could be done using the drop() method.

drop() method removes the columns from the DataFrame, by default it doesn’t remove the existing DataFrame instead it returns a new DataFrame without the columns specified with the drop method. In order to remove columns on the existing DataFrame object use inplace=True param.

If a column you want to remove is not present on the DataFrame it returns an error message and you can handle this error using errors param.

You can also drop the index of the DataFrame using index param.

Now, Let’s learn with examples. first, create a DataFrame with a dictionary of lists. On our DataFrame, we have columns Courses, Fee and Duration.


import pandas as pd
technologies = ({
    'Courses':["Spark","PySpark","Hadoop","Python","pandas","Oracle","Java"],
    'Fee' :[20000,25000,26000,22000,24000,21000,22000],
    'Duration':['30day', '40days' ,'35days', '40days', '60days', '50days', '55days']
              })
df = pd.DataFrame(technologies)
print(df)

Yields below output.

Pandas Drop multiple Columns

3. Drop Multiple Columns By Name

When you have a list of columns to drop, create a list object with the column’s name and use it with the drop() method or directly use the list. The Below examples delete columns Courses and Fee from Pandas DataFrame.


# Drop Multiple Columns By Name
df2=df.drop(["Courses", "Fee"], axis = 1)
print("After dropping the columns of DataFrame:\n", df2)

Yields below output. Use inplace=True to update the self DataFrame.

Pandas Drop multiple Columns

Related: You can also use the drop() function to drop the first and the last column of the DataFrame.

4. Drop Multiple Columns by Index

If you wanted to drop multiple columns by index, unfortunately, the drop() method doesn’t take an index as a param, but we can overcome this by getting column names by index using df.columns[]. Use the below example to delete columns 0 and 1 (index starts from 0) index. for more examples, refer to remove multiple columns by index.


# Drop Multiple Columns by Index
df2=df.drop(df.columns[[0,1]], axis = 1)
print(df2)

Yields the same output as above.

5. Drop Columns from the List

If you have a list of columns and you wanted to delete all columns from the list, use the below approach.


# Drop Columns from List 
lisCol = ["Courses","Fee"]
df2=df.drop(lisCol, axis = 1)
print(df2)

6. Remove Columns Between Specified Columns

You can use drop() method with the loc[] attribute of Pandas to remove a specific portion of the columns given DatFrame. Use [ : , 'Courses':'Fee'] to drop the one and second columns. You can use inplace the param of the drop() function to implement this code on the original object.


# Remove Columns Between Specified Columns
df.drop(df.loc[:, 'Courses':'Fee'].columns, axis = 1, inplace=True)
print(df)

7. Remove Columns Between Specified Indexes.

Alternatively, you can use the drop() method with <a href="https://sparkbyexamples.com/pandas/pandas-iloc-usage-with-examples/">iloc[]</a> an attribute to remove all columns between specific columns. Use [: , 1:2] for deleting the second column. For instance, df.drop(df.iloc[:, 1:2], inplace=True, axis=1), removes Fee column.


# Remove Columns Between Specified Indexes. 
df.drop(df.iloc[:, 1:2], inplace=True, axis=1)
print(df)

8. Complete Example For Reference


import pandas as pd
technologies = ({
    'Courses':["Spark","PySpark","Hadoop","Python","pandas","Oracle","Java"],
    'Fee' :[20000,25000,26000,22000,24000,21000,22000],
    'Duration':['30day', '40days' ,'35days', '40days', '60days', '50days', '55days']
              })
df = pd.DataFrame(technologies)
print(df)

# Drop multiple columns by Name
df2=df.drop(["Courses", "Fee"], axis = 1)
print(df2)

# Drop multiple columns by Index
df2=df.drop(df.columns[[0,1]], axis = 1)
print(df2)

# Drop Columns from List
lisCol = ["Courses","Fee"]
df2=df.drop(lisCol, axis = 1)
print(df2)

# Drop columns between two columns
df2=df.drop(df.loc[:, 'Courses':'Fee'].columns, axis = 1)
print(df)

df.drop(df.iloc[:, 1:2], inplace=True, axis=1)
print(df)

Conclusion

In this pandas drop multiple columns article, you have learned how to remove or delete multiple columns from DataFrame by name, label, and index. Also, you have learned how to remove columns between two columns and many more examples.

Happy Learning !!

Naveen (NNK)

Naveen (NNK) is a Data Engineer with 20+ years of experience in transforming data into actionable insights. Over the years, He has honed his expertise in designing, implementing, and maintaining data pipelines with frameworks like Apache Spark, PySpark, Pandas, R, Hive and Machine Learning. Naveen journey in the field of data engineering has been a continuous learning, innovation, and a strong commitment to data integrity. In this blog, he shares his experiences with the data as he come across. Follow Naveen @ @ LinkedIn

Leave a Reply