How to drop ROW and COLUMN in a Pandas DataFrame?
0

How to drop ROW and COLUMN in a Pandas DataFrame?

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

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

Big Data Project Learn to prepare data for your next machine learning project
Text data requires special preparation before you can start using it for any machine learning project.In this ML project, you will learn about applying Machine Learning models to create classifiers and learn how to make sense of textual data.
Big Data Project 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.
Big Data Project Anomaly Detection Using Deep Learning and Autoencoders
Deep Learning Project- Learn about implementation of a machine learning algorithm using autoencoders for anomaly detection.
Big Data Project Credit Card Fraud Detection as a Classification Problem
In this data science project, we will predict the credit card fraud in the transactional dataset using some of the predictive models.
Big Data Project Human Activity Recognition Using Smartphones Data Set
In this deep learning project, you will build a classification system where to precisely identify human fitness activities.
Big Data Project 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.
Big Data Project Data Science Project-All State Insurance Claims Severity Prediction
Data science project in R to develop automated methods for predicting the cost and severity of insurance claims.
Big Data Project 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.
Big Data Project 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.
Big Data Project 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.