7 Ways to Drop Column in Pandas DataFrame

Introduction

Like any other programming language and framework, there are multiple ways to drop columns in Pandas. In this tutorial we will explore the following methods –

  • df.drop()
  • Del Statement
  • df.pop()
  • DataFrame Slicing
  • List Comprehension
  • df.dropna()
  • Iterative Method

 

1. Using df.drop to Drop Column in Pandas

In this example, we are using df.drop method to drop column in Pandas. We create a sample DataFrame and then use the df.drop method to drop the ‘B’ column. Here axis=1 argument signifies that we are dropping a column.

 

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)

print("Original DataFrame")
print(df)

# Drop a single column

 

 

 

To finish reading, please visit source site

Leave a Reply