How to drop a column in Pandas

You can drop a column in Pandas with the following code. I highly recommend you This book to learn Python. You will see 3 examples to drop a column in Pandas DataFrame in this article.

Step 1: Install Pandas Library

Install the Pandas library using this code, if it is not installed.

pip install pandas

Example 1: Drop two Columns

In this example, I will drop two columns of the Pandas DataFrame and save the results in the same DataFrame.

# Import the required libraries
import pandas as pd
import numpy as np

# Initialize a NumPy Array
a = np.array([[45, 60, 25], [10, 15, 16], [77, 58, 39]])

# Create DataFrame
df = pd.DataFrame(a, columns=['A', 'B', 'C'])

# Display the Original DataFrame
print(df)

# Drop two Columns and changes applied to original dataframe using inplace
df.drop(['A', 'C'], axis = 1, inplace = True)

# Display the DataFrame
print(df)

Output:

    A   B   C
0  45  60  25
1  10  15  16
2  77  58  39
    B
0  60
1  15
2  58

Example 2: Drop Single Column

In this example, I will drop a single column from the DataFrame and save the results in a new DataFrame.

# Import the Pandas library as pd
import pandas as pd

# Initialize a dictionary
dict = {'Students':['Hussain', 'Amini', 'Alexandar', 'John'],
        'Scores':[91, 93, 88, 95]}

# Create DataFrame
df = pd.DataFrame(dict)

# Display the Original DataFrame
print(df)

# Drop Single Column
df1 = df.drop(['Scores'], axis=1)

# Display new DataFrame
print(df1)

Output:

    Students  Scores
0    Hussain      91
1      Amini      93
2  Alexandar      88
3       John      95
    Students
0    Hussain
1      Amini
2  Alexandar
3       John

Example 3

In this example, I will read a CSV file as a Pandas DataFrame and drop two columns and save the output in the same DataFrame.

# Import the Pandas library as pd
import pandas as pd

# Read the CSV file as Pandas DataFrame
df = pd.read_csv("house_price.csv")

# Display the Original DataFrame
print(df)

# Drop two Columns and changes applied to original dataframe using inplace
df.drop(['Rooms', 'House_Age'], axis = 1, inplace = True)

# Display the DataFrame
print(df)

Output:

   Area  Rooms  House_Age  Price
0  5000      5          5  75000
1  4000      4          5  65000
2  3000      3          1  60000
3  2000      3          1  58000
4  1500      2          1  50000
5  7000      5          5  90000
6  6000      5          3  85000
7  6500      4          7  80000
8  8000      5         15  95000
   Area  Price
0  5000  75000
1  4000  65000
2  3000  60000
3  2000  58000
4  1500  50000
5  7000  90000
6  6000  85000
7  6500  80000
8  8000  95000

Free Learning Resources

Leave a Comment

Your email address will not be published.