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

Identifying Product Bundles from Sales Data Using R Language
In this data science project in R, we are going to talk about subjective segmentation which is a clustering technique to find out product bundles in sales data.

Machine Learning or Predictive Models in IoT - Energy Prediction Use Case
In this machine learning and IoT project, we are going to test out the experimental data using various predictive models and train the models and break the energy usage.

Forecast Inventory demand using historical sales data in R
In this machine learning project, you will develop a machine learning model to accurately forecast inventory demand based on historical sales data.

Customer Churn Prediction Analysis using Ensemble Techniques
In this machine learning churn project, we implement a churn prediction model in python using ensemble techniques.

Predict Census Income using Deep Learning Models
In this project, we are going to work on Deep Learning using H2O to predict Census income.

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.

PySpark Tutorial - Learn to use Apache Spark with Python
PySpark Project-Get a handle on using Python with Spark through this hands-on data processing spark python tutorial.

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.

Predict Churn for a Telecom company using Logistic Regression
Machine Learning Project in R- Predict the customer churn of telecom sector and find out the key drivers that lead to churn. Learn how the logistic regression model using R can be used to identify the customer churn in telecom dataset.

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.