• Post author:
  • Post category:Pandas
  • Post last modified:December 5, 2024
  • Reading time:15 mins read
You are currently viewing Pandas Drop Multiple Columns From DataFrame

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.

Advertisements

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.

Key Points –

  • Columns can be dropped by specifying their names as a list in the drop() function with the axis=1 parameter.
  • You can drop columns by their index positions using df.columns or df.iloc[] to specify the desired indices.
  • The drop() method allows for removing multiple columns simultaneously by passing a list of column names or indices.
  • The inplace=True parameter modifies the DataFrame directly, removing the need to assign the result to a new variable.
  • You can drop columns between two specific columns using either their names with .loc[] or their index positions with .iloc[].
  • The axis=1 parameter is essential for column-wise operations when using drop() to remove columns.

Quick Examples of Pandas Drop Multiple Columns

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


# Quick examples of pandas drop multiple columns

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

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

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.

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.

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)

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)

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)

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)

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

FAQ on Pandas Drop Multiple Columns From DataFrame

How do I drop multiple columns from a DataFrame in Pandas?

To drop multiple columns from a Pandas DataFrame, use the drop() method and specify the column names in a list. You can also set the axis=1 parameter to indicate that you’re dropping columns (not rows).

Can I drop columns by their index positions?

You can drop columns using their index positions by first selecting the column names via df.columns and then passing the desired index positions to drop().

Can I drop multiple columns in place (without creating a new DataFrame)?

You can drop multiple columns in place without creating a new DataFrame by setting the inplace parameter to True in the drop() method. This modifies the original DataFrame directly.

How do I drop columns using column index positions instead of column names?

To drop columns using column index positions instead of column names, you can use the df.columns attribute to access the column names by index and then drop them.

What happens if I try to drop a column that doesn’t exist?

If you try to drop a column that doesn’t exist in the DataFrame, Pandas will raise a KeyError. To avoid this, you can pass the errors='ignore' argument, which will silently ignore any missing columns.

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 !!