Skip to main content
St Louis

Back to all posts

How to Delete A Column In Pandas?

Published on
4 min read
How to Delete A Column In Pandas? image

Best Data Management Tools to Buy in September 2025

+
ONE MORE?

To delete a column in pandas, you can use the .drop() method along with the axis=1 parameter. You will need to specify the name of the column you want to delete within the method. For example, if you have a DataFrame called df and you want to delete a column named column_name, you can use the following code: df.drop('column_name', axis=1, inplace=True). This will delete the specified column from the DataFrame df.

How to delete a column in pandas using loc[] method?

To delete a column in pandas using the loc[] method, you can specify the columns you want to keep and assign it back to the original dataframe. Here is an example:

import pandas as pd

Create a sample dataframe

data = {'A': [1, 2, 3], 'B': [4, 5, 6], 'C': [7, 8, 9]} df = pd.DataFrame(data)

Delete column 'B' using loc[]

df = df.loc[:, ['A', 'C']]

print(df)

This will delete column 'B' from the dataframe df and print the resulting dataframe with columns 'A' and 'C' only.

How to delete a column in pandas by dropping it from the DataFrame?

You can delete a column in pandas by using the drop() method on the DataFrame. You need to specify the column name that you want to delete along with the axis parameter set to 1 to indicate that you are dropping a column. Here's an example:

import pandas as pd

Create a sample DataFrame

data = {'A': [1, 2, 3], 'B': [4, 5, 6], 'C': [7, 8, 9]} df = pd.DataFrame(data)

Drop column 'B' from the DataFrame

df = df.drop('B', axis=1)

print(df)

This will output:

A C 0 1 7 1 2 8 2 3 9

Now, the column 'B' has been deleted from the DataFrame.

How to delete a column in pandas by specifying the column name?

You can delete a column in pandas by specifying the column name using the drop() method.

Here's an example:

import pandas as pd

Create a sample DataFrame

data = {'A': [1, 2, 3], 'B': [4, 5, 6], 'C': [7, 8, 9]}

df = pd.DataFrame(data)

Delete column 'B'

df = df.drop('B', axis=1)

print(df)

This will output:

A C 0 1 7 1 2 8 2 3 9

In this example, we use the drop() method to delete column 'B' by specifying the column name and setting axis=1 to indicate we are dropping a column.

How to safely delete a column in pandas without affecting other columns?

To safely delete a column in pandas without affecting other columns, you can use the drop() function. Here's an example of how to do this:

import pandas as pd

Create a sample DataFrame

data = { 'A': [1, 2, 3], 'B': [4, 5, 6], 'C': [7, 8, 9] } df = pd.DataFrame(data)

Drop column 'B' without affecting other columns

df.drop('B', axis=1, inplace=True)

print(df)

In this example, we are dropping column 'B' from the DataFrame df using the drop() function and setting the axis parameter to 1 to indicate that we are dropping a column. The inplace=True parameter ensures that the change is made directly to the original DataFrame df.

How to delete a column in pandas by selecting all columns except the one to be deleted?

You can delete a column in pandas by selecting all columns except the one to be deleted using the following code:

import pandas as pd

Create a sample dataframe

data = {'A': [1, 2, 3], 'B': [4, 5, 6], 'C': [7, 8, 9]} df = pd.DataFrame(data)

Delete column 'B' by selecting all columns except 'B'

df = df.loc[:, df.columns.difference(['B'])]

print(df)

In this code, we use the df.columns.difference(['B']) function to get a list of all columns except the column to be deleted ('B'). We then use this list to select the columns we want to keep in the dataframe using df.loc[:, ...]. Finally, we overwrite the original dataframe with the new dataframe that only contains the selected columns.

How to delete a column in pandas by setting axis=1 in drop() method?

To delete a column in pandas by setting axis=1 in the drop() method, you can use the following syntax:

import pandas as pd

Create a sample dataframe

data = {'A': [1, 2, 3], 'B': [4, 5, 6], 'C': [7, 8, 9]} df = pd.DataFrame(data)

Delete column 'B' by setting axis=1

df = df.drop('B', axis=1)

print(df)

In this example, the column 'B' is deleted from the DataFrame by specifying axis=1 in the drop() method. The resulting DataFrame will only contain columns 'A' and 'C'.