DATA MUNGING PYTHON PANDAS DATAFRAME PANDAS CHEATSHEET PANDAS DATAFRAME TUTORIAL PANDAS COLUMN

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


Companies using this Recipe
1 employee of KPMG
1 employee of YASH Technologies
1 employee of Altimetrik
1 employee of LTI
1 employee of ANAC
1 employee of Renovite Technologies
1 employee of ICU Medical
1 employee of S&P Global