Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Drop Rows or Columns from a pandas DataFrame

Things on this page are fragmentary and immature notes/thoughts of the author. Please read with your own judgement!

Drop Rows

import pandas as pd

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"])
df
Loading...
df.drop(["Cochice", "Pima"])
Loading...

Drop Columns

import pandas as pd

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"])
df
Loading...
df.drop("reports", axis=1, inplace=False)
Loading...
df.drop(df.columns[[0, 1]], axis=1)
Loading...