How to drop ROW and COLUMN in a Pandas DataFrame?

This recipe helps you drop ROW and COLUMN in a Pandas DataFrame

Recipe Objective

Have you ever tried to remove a column or row on the basis of condition ? So this is a code snippet is for you.

So this is the recipe on how we can drop ROW and COLUMN in a Pandas DataFrame

Master the Art of Data Cleaning in Machine Learning

Step 1 - Import the library

import pandas as pd

We have imported only pandas which will be needed for the dataset.

Step 2 - Setting up the Data

We have created a dictionary of data and passed it in pd.DataFrame to make a dataframe with columns 'last_name', 'age', 'Comedy_Score' and 'Rating_Score'. raw_data = { 'last_name': ['Copper', 'Koothrappali', 'Hofstadter', 'Wolowitz', 'Fowler'], 'age': [42, 38, 36, 41, 35], 'Comedy_Score': [9, 7, 8, 8, 5], 'Rating_Score': [25, 25, 49, 62, 70] } df = pd.DataFrame(raw_data, columns = [ 'first_name', 'last_name', 'age', 'Comedy_Score', 'Rating_Score'], index = ['Sheldon', 'Raj', 'Leonard', 'Howard', 'Amy']) print(df)

Step 3 - Droping Rows and Columns

Here we will be removing rows and columns on some conditions.

    • Droping an observation (row)

print(df.drop(['Sheldon', 'Amy']))

    • Droping a variable (column)

print(df.drop('age', axis=1))

    • Droping a row if it contains a certain value (in this case, 'Cooper')

print(df[df.last_name != 'Copper'])

    • Droping a row by row number (in this case, row 3)

print(df.drop(df.index[2]))

    • Keeping top 3 rows only

print(df[:3])

    • Droping last 3 rows

print(df[:-3])

So the output comes as:

            last_name  age  Comedy_Score  Rating_Score
Sheldon        Copper   42             9            25
Raj      Koothrappali   38             7            25
Leonard    Hofstadter   36             8            49
Howard       Wolowitz   41             8            62
Amy            Fowler   35             5            70

            last_name  age  Comedy_Score  Rating_Score
Raj      Koothrappali   38             7            25
Leonard    Hofstadter   36             8            49
Howard       Wolowitz   41             8            62

            last_name  Comedy_Score  Rating_Score
Sheldon        Copper             9            25
Raj      Koothrappali             7            25
Leonard    Hofstadter             8            49
Howard       Wolowitz             8            62
Amy            Fowler             5            70

            last_name  age  Comedy_Score  Rating_Score
Raj      Koothrappali   38             7            25
Leonard    Hofstadter   36             8            49
Howard       Wolowitz   41             8            62
Amy            Fowler   35             5            70

            last_name  age  Comedy_Score  Rating_Score
Sheldon        Copper   42             9            25
Raj      Koothrappali   38             7            25
Howard       Wolowitz   41             8            62
Amy            Fowler   35             5            70

            last_name  age  Comedy_Score  Rating_Score
Sheldon        Copper   42             9            25
Raj      Koothrappali   38             7            25
Leonard    Hofstadter   36             8            49

            last_name  age  Comedy_Score  Rating_Score
Sheldon        Copper   42             9            25
Raj      Koothrappali   38             7            25
?

Download Materials

What Users are saying..

profile image

Gautam Vermani

Data Consultant at Confidential
linkedin profile url

Having worked in the field of Data Science, I wanted to explore how I can implement projects in other domains, So I thought of connecting with ProjectPro. A project that helped me absorb this topic... Read More

Relevant Projects

Personalized Medicine: Redefining Cancer Treatment
In this Personalized Medicine Machine Learning Project you will learn to classify genetic mutations on the basis of medical literature into 9 classes.

Create Your First Chatbot with RASA NLU Model and Python
Learn the basic aspects of chatbot development and open source conversational AI RASA to create a simple AI powered chatbot on your own.

Multilabel Classification Project for Predicting Shipment Modes
Multilabel Classification Project to build a machine learning model that predicts the appropriate mode of transport for each shipment, using a transport dataset with 2000 unique products. The project explores and compares four different approaches to multilabel classification, including naive independent models, classifier chains, natively multilabel models, and multilabel to multiclass approaches.

Build CNN Image Classification Models for Real Time Prediction
Image Classification Project to build a CNN model in Python that can classify images into social security cards, driving licenses, and other key identity information.

Predictive Analytics Project for Working Capital Optimization
In this Predictive Analytics Project, you will build a model to accurately forecast the timing of customer and supplier payments for optimizing working capital.

Build a Customer Churn Prediction Model using Decision Trees
Develop a customer churn prediction model using decision tree machine learning algorithms and data science on streaming service data.

Image Classification Model using Transfer Learning in PyTorch
In this PyTorch Project, you will build an image classification model in PyTorch using the ResNet pre-trained model.

LLM Project to Build and Fine Tune a Large Language Model
In this LLM project for beginners, you will learn to build a knowledge-grounded chatbot using LLM's and learn how to fine tune it.

Loan Eligibility Prediction Project using Machine learning on GCP
Loan Eligibility Prediction Project - Use SQL and Python to build a predictive model on GCP to determine whether an application requesting loan is eligible or not.

Build Classification Algorithms for Digital Transformation[Banking]
Implement a machine learning approach using various classification techniques in Python to examine the digitalisation process of bank customers.