How to drop ROW and COLUMN in a Pandas DataFrame?
DATA MUNGING DATA CLEANING PYTHON MACHINE LEARNING RECIPES PANDAS CHEATSHEET     ALL TAGS

How to drop ROW and COLUMN in a Pandas DataFrame?

How to drop ROW and COLUMN in a Pandas DataFrame?

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

0

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

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
?

Relevant Projects

Loan Eligibility Prediction using Gradient Boosting Classifier
This data science in python project predicts if a loan should be given to an applicant or not. We predict if the customer is eligible for loan based on several factors like credit score and past history.

Ensemble Machine Learning Project - All State Insurance Claims Severity Prediction
In this ensemble machine learning project, we will predict what kind of claims an insurance company will get. This is implemented in python using ensemble machine learning algorithms.

Topic modelling using Kmeans clustering to group customer reviews
In this Kmeans clustering machine learning project, you will perform topic modelling in order to group customer reviews based on recurring patterns.

Build an Image Classifier for Plant Species Identification
In this machine learning project, we will use binary leaf images and extracted features, including shape, margin, and texture to accurately identify plant species using different benchmark classification techniques.

Solving Multiple Classification use cases Using H2O
In this project, we are going to talk about H2O and functionality in terms of building Machine Learning models.

Predict Employee Computer Access Needs in Python
Data Science Project in Python- Given his or her job role, predict employee access needs using amazon employee database.

Predict Credit Default | Give Me Some Credit Kaggle
In this data science project, you will predict borrowers chance of defaulting on credit loans by building a credit score prediction model.

Data Science Project in Python on BigMart Sales Prediction
The goal of this data science project is to build a predictive model and find out the sales of each product at a given Big Mart store.

Zillow’s Home Value Prediction (Zestimate)
Data Science Project in R -Build a machine learning algorithm to predict the future sale prices of homes.

Customer Market Basket Analysis using Apriori and Fpgrowth algorithms
In this data science project, you will learn how to perform market basket analysis with the application of Apriori and FP growth algorithms based on the concept of association rule learning.