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

Jingwei Li

Graduate Research assistance at Stony Brook University
linkedin profile url

ProjectPro is an awesome platform that helps me learn much hands-on industrial experience with a step-by-step walkthrough of projects. There are two primary paths to learn: Data Science and Big Data.... Read More

Relevant Projects

Build a Multi ClassText Classification Model using Naive Bayes
Implement the Naive Bayes Algorithm to build a multi class text classification model in Python.

Insurance Pricing Forecast Using XGBoost Regressor
In this project, we are going to talk about insurance forecast by using linear and xgboost regression techniques.

Time Series Analysis with Facebook Prophet Python and Cesium
Time Series Analysis Project - Use the Facebook Prophet and Cesium Open Source Library for Time Series Forecasting in Python

Ecommerce product reviews - Pairwise ranking and sentiment analysis
This project analyzes a dataset containing ecommerce product reviews. The goal is to use machine learning models to perform sentiment analysis on product reviews and rank them based on relevance. Reviews play a key role in product recommendation systems.

Build Regression (Linear,Ridge,Lasso) Models in NumPy Python
In this machine learning regression project, you will learn to build NumPy Regression Models (Linear Regression, Ridge Regression, Lasso Regression) from Scratch.

Build a Speech-Text Transcriptor with Nvidia Quartznet Model
In this Deep Learning Project, you will leverage transfer learning from Nvidia QuartzNet pre-trained models to develop a speech-to-text transcriptor.

Build a Review Classification Model using Gated Recurrent Unit
In this Machine Learning project, you will build a classification model in python to classify the reviews of an app on a scale of 1 to 5 using Gated Recurrent Unit.

Recommender System Machine Learning Project for Beginners-2
Recommender System Machine Learning Project for Beginners Part 2- Learn how to build a recommender system for market basket analysis using association rule mining.

Expedia Hotel Recommendations Data Science Project
In this data science project, you will contextualize customer data and predict the likelihood a customer will stay at 100 different hotel groups.

House Price Prediction Project using Machine Learning in Python
Use the Zillow Zestimate Dataset to build a machine learning model for house price prediction.