How to delete a column from a Pandas DataFrame?



To delete a column from a Pandas DataFrame, we can simply use del df["column name"]

Steps

  • Create a two-dimensional, size-mutable, potentially heterogeneous tabular data, df.

  • Print the input DataFrame.

  • Delete a column, del df["z"].

  • Print the updated DataFrame, df.

Example

 Live Demo

import pandas as pd

df = pd.DataFrame(
   {
      "x": [5, 2, 1, 9],
      "y": [4, 1, 5, 10],
      "z": [4, 1, 5, 0]
   }
)

print "Input DataFrame is:
", df del df["z"] print "After deleting column-z:
", df

Output

Input DataFrame is:
   x  y  z
0  5  4  4
1  2  1  1
2  1  5  5
3  9 10  0

After deleting column-z:
   x   y
0  5   4
1  2   1
2  1   5
3  9  10

Advertisements