How to delete column(s) of Pandas DataFrame? - 4 Python Examples


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)

Explanation

  1. The program imports the pandas library, which is essential for handling and manipulating data in tabular formats.
  2. A dictionary named mydictionary is defined, containing keys ('names', 'physics', 'chemistry', and 'algebra') with corresponding lists of values for each key.
  3. A DataFrame named df_marks is created using the pd.DataFrame() function, taking mydictionary as its input to organize the data into rows and columns.
  4. The original DataFrame is printed to the console, showing all columns and their respective rows of data.
  5. The del statement is used to remove the column 'chemistry' from the DataFrame, altering its structure.
  6. The modified DataFrame, which no longer includes the 'chemistry' column, is printed to display the updated structure with the remaining columns: 'names', 'physics', and 'algebra'.

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)

Explanation

  1. This Python program demonstrates the creation and manipulation of a Pandas DataFrame.
  2. The dictionary mydictionary contains data with keys as column names ('names', 'physics', 'chemistry', 'algebra') and values as lists representing the column data.
  3. The pd.DataFrame() function converts the dictionary into a DataFrame named df_marks.
  4. The program prints the original DataFrame using the print() function.
  5. The pop() method is used to remove the 'chemistry' column from the DataFrame.
  6. The modified DataFrame (after removing the 'chemistry' column) is printed to show the changes.

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)

Explanation

  1. The program begins by importing the pandas library for data manipulation.
  2. A dictionary mydictionary is created with keys ('names', 'physics', 'chemistry', and 'algebra') and corresponding lists of values.
  3. A DataFrame df_marks is created using the pd.DataFrame() function with mydictionary as the input.
  4. The original DataFrame is printed to display all columns and rows.
  5. The drop() method is used to delete the column 'chemistry'. The argument axis=1 specifies that the operation is being performed on columns (not rows).
  6. The updated DataFrame, with the column 'chemistry' removed, is printed to display the modified structure.

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)

Explanation

  1. The program imports the pandas library for data manipulation.
  2. A dictionary named mydictionary is created, which contains keys ('names', 'physics', 'chemistry', and 'algebra') with corresponding lists of values.
  3. A DataFrame named df_marks is created using the pd.DataFrame() function with mydictionary as input.
  4. The original DataFrame is printed to show all columns and rows.
  5. The drop() method is used to delete the columns 'algebra' and 'chemistry'. The argument axis=1 specifies that columns are being removed.
  6. The modified DataFrame, with the remaining columns 'names' and 'physics', is printed to display the updated structure.

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.


Python Libraries