pandas
  1. pandas-drop-columns-in-pandas

Dropping Columns in Pandas

In Pandas, dropping columns is a common operation when working with DataFrames. This guide covers the syntax, example, output, explanation, use cases, important points, and a summary of dropping columns in Pandas.

Syntax

import pandas as pd

# Dropping a single column
df.drop('column_name', axis=1, inplace=True)

# Dropping multiple columns
df.drop(['column1', 'column2'], axis=1, inplace=True)
  • column_name: The name of the column to be dropped.
  • axis: Specifies whether to drop along rows (axis=0) or columns (axis=1).
  • inplace: If True, the changes are made in-place; if False (default), a new DataFrame with columns dropped is returned.

Example

import pandas as pd

# Creating a DataFrame
data = {'Name': ['Alice', 'Bob', 'Charlie'],
        'Age': [25, 30, 35],
        'City': ['New York', 'San Francisco', 'Los Angeles']}

df = pd.DataFrame(data)

# Dropping the 'City' column
df.drop('City', axis=1, inplace=True)

# Displaying the updated DataFrame
print(df)

Output

    Name  Age
0  Alice   25
1    Bob   30
2 Charlie  35

Explanation

  • The drop method is used to remove specified columns from a DataFrame.
  • The axis=1 parameter indicates that the operation is performed along columns.
  • Setting inplace=True modifies the original DataFrame; otherwise, a new DataFrame with columns dropped is returned.

Use

  • Dropping columns is useful when certain columns are not needed for analysis or when reshaping the data.
  • It allows for creating a more focused DataFrame that includes only the relevant information.

Important Points

  • Always double-check that the columns you are dropping are not essential for your analysis.
  • Using inplace=True modifies the original DataFrame and does not return a new one.

Summary

Dropping columns in Pandas is a straightforward operation that helps in refining and focusing the DataFrame for analysis. The flexibility to drop single or multiple columns provides control over the dataset's structure. Whether for cleaning or reshaping data, the drop method is a valuable tool in the Pandas library.

Published on: