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
In [1]:
## How to drop ROW and COLUMN in a Pandas DataFrame
def Kickstarter_Example_86():
    print()
    print(format('How to drop ROW and COLUMN in a Pandas DataFrame','*^82'))

    import warnings
    warnings.filterwarnings("ignore")

    # load libraries
    import pandas as pd

    # Create a dataframe
    data = {'name': ['Jason', 'Molly', 'Tina', 'Jake', 'Amy'],
            'year': [2012, 2012, 2013, 2014, 2014],
            'reports': [4, 24, 31, 2, 3]}
    df = pd.DataFrame(data, index = ['Cochice', 'Pima', 'Santa Cruz', 'Maricopa', 'Yuma'])
    print(); print(df)

    # Drop an observation (row)
    print(); print(df.drop(['Cochice', 'Pima']))

    # Drop a variable (column) # Note: axis=1 denotes that we are referring to a column, not a row
    print(); print(df.drop('reports', axis=1))

    # Drop a row if it contains a certain value (in this case, “Tina”)
    print(); print(df[df.name != 'Tina'])

    # Drop a row by row number (in this case, row 3)
    print(); print(df.drop(df.index[2]))

    # can be extended to dropping a range
    print(); print(df.drop(df.index[[2,3]]))

    # dropping relative to the end of the DF.
    print(); print(df.drop(df.index[-2]))

    # Keep top 3
    print(); print(df[:3])

    # Drop bottom 3 
    print(); print(df[:-3])

Kickstarter_Example_86()
*****************How to drop ROW and COLUMN in a Pandas DataFrame*****************

             name  year  reports
Cochice     Jason  2012        4
Pima        Molly  2012       24
Santa Cruz   Tina  2013       31
Maricopa     Jake  2014        2
Yuma          Amy  2014        3

            name  year  reports
Santa Cruz  Tina  2013       31
Maricopa    Jake  2014        2
Yuma         Amy  2014        3

             name  year
Cochice     Jason  2012
Pima        Molly  2012
Santa Cruz   Tina  2013
Maricopa     Jake  2014
Yuma          Amy  2014

           name  year  reports
Cochice   Jason  2012        4
Pima      Molly  2012       24
Maricopa   Jake  2014        2
Yuma        Amy  2014        3

           name  year  reports
Cochice   Jason  2012        4
Pima      Molly  2012       24
Maricopa   Jake  2014        2
Yuma        Amy  2014        3

          name  year  reports
Cochice  Jason  2012        4
Pima     Molly  2012       24
Yuma       Amy  2014        3

             name  year  reports
Cochice     Jason  2012        4
Pima        Molly  2012       24
Santa Cruz   Tina  2013       31
Yuma          Amy  2014        3

             name  year  reports
Cochice     Jason  2012        4
Pima        Molly  2012       24
Santa Cruz   Tina  2013       31

          name  year  reports
Cochice  Jason  2012        4
Pima     Molly  2012       24

Relevant Projects

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.

Choosing the right Time Series Forecasting Methods
There are different time series forecasting methods to forecast stock price, demand etc. In this machine learning project, you will learn to determine which forecasting method to be used when and how to apply with time series forecasting example.

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.

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.

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

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.

Deep Learning with Keras in R to Predict Customer Churn
In this deep learning project, we will predict customer churn using Artificial Neural Networks and learn how to model an ANN in R with the keras deep learning package.

Data Science Project on Wine Quality Prediction in R
In this R data science project, we will explore wine dataset to assess red wine quality. The objective of this data science project is to explore which chemical properties will influence the quality of red wines.

Predict Macro Economic Trends using Kaggle Financial Dataset
In this machine learning project, you will uncover the predictive value in an uncertain world by using various artificial intelligence, machine learning, advanced regression and feature transformation techniques.