How to delete column(s) of Pandas DataFrame?

Pandas DataFrame – Delete Column(s)

You can delete one or multiple columns of a DataFrame.

To delete or remove only one column from Pandas DataFrame, you can use either del keyword, pop() function, or drop() function on the dataframe.

To delete multiple columns from Pandas Dataframe, use drop() function on the DataFrame.

In this tutorial, you’ll learn how to delete one or more columns in a DataFrame, with the help of example programs.

Examples

1. Delete a column using del keyword

In this example, we will create a DataFrame and then delete a specified column using del keyword. The column is selected for deletion, using the column label.

Python Program

import pandas as pd

mydictionary = {'names': ['Somu', 'Kiku', 'Amol', 'Lini'],
	'physics': [68, 74, 77, 78],
	'chemistry': [84, 56, 73, 69],
	'algebra': [78, 88, 82, 87]}

# Create dataframe
df_marks = pd.DataFrame(mydictionary)
print('Original DataFrame\n--------------')
print(df_marks)

# Delete a column
del df_marks['chemistry']
print('\n\nDataFrame after deleting column\n--------------')
print(df_marks)
Run Code Copy

Output

Original DataFrame
--------------
  names  physics  chemistry  algebra
0  Somu       68         84       78
1  Kiku       74         56       88
2  Amol       77         73       82
3  Lini       78         69       87


DataFrame after deleting column
--------------
  names  physics  algebra
0  Somu       68       78
1  Kiku       74       88
2  Amol       77       82
3  Lini       78       87

We have deleted chemistry column from the dataframe.

2. Delete a column using pop() function

In this example, we will create a DataFrame and then use pop() function on the dataframe to delete a specific column. The column is selected for deletion, using the column label.

Python Program

import pandas as pd

mydictionary = {'names': ['Somu', 'Kiku', 'Amol', 'Lini'],
	'physics': [68, 74, 77, 78],
	'chemistry': [84, 56, 73, 69],
	'algebra': [78, 88, 82, 87]}

# Create dataframe
df_marks = pd.DataFrame(mydictionary)
print('Original DataFrame\n--------------')
print(df_marks)

# Delete column
df_marks.pop('chemistry')
print('\n\nDataFrame after deleting column\n--------------')
print(df_marks)
Run Code Copy

Output

Original DataFrame
--------------
  names  physics  chemistry  algebra
0  Somu       68         84       78
1  Kiku       74         56       88
2  Amol       77         73       82
3  Lini       78         69       87


DataFrame after deleting column
--------------
  names  physics  algebra
0  Somu       68       78
1  Kiku       74       88
2  Amol       77       82
3  Lini       78       87

We have deleted chemistry column from the dataframe.

3. Delete a column using drop() function

In this example, we will use drop() function on the dataframe to delete a specific column. We use column label to select a column for deletion.

Python Program

import pandas as pd

mydictionary = {'names': ['Somu', 'Kiku', 'Amol', 'Lini'],
	'physics': [68, 74, 77, 78],
	'chemistry': [84, 56, 73, 69],
	'algebra': [78, 88, 82, 87]}

# Create dataframe
df_marks = pd.DataFrame(mydictionary)
print('Original DataFrame\n--------------')
print(df_marks)

# Delete column
df_marks = df_marks.drop(['chemistry'], axis=1)
print('\n\nDataFrame after deleting column\n--------------')
print(df_marks)
Run Code Copy

Output

Python Delete Single Column

4. Delete multiple columns using drop() function

In this example, we will use drop() function on the dataframe to delete multiple columns. We use array of column labels to select columns for deletion.

Python Program

import pandas as pd

mydictionary = {'names': ['Somu', 'Kiku', 'Amol', 'Lini'],
	'physics': [68, 74, 77, 78],
	'chemistry': [84, 56, 73, 69],
	'algebra': [78, 88, 82, 87]}

# Create dataframe
df_marks = pd.DataFrame(mydictionary)
print('Original DataFrame\n--------------')
print(df_marks)

# Delete columns
df_marks = df_marks.drop(['algebra', 'chemistry'], axis=1)
print('\n\nDataFrame after deleting column\n--------------')
print(df_marks)
Run Code Copy

Output

Python Delete Multiple Columns

Summary

In this Pandas Tutorial, we learned how to delete a column from Pandas DataFrame using del keyword, pop() method and drop() method, with the help of well detailed Python Examples.

Related Tutorials

Code copied to clipboard successfully 👍