Skip to main content
Engineering LibreTexts

16.3: Removing Rows/Columns

  • Page ID
    39308
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    Finally, after reading a .csv file into a DataFrame, there are times when you want to manually delete certain rows and/or columns that are not going to be of interest.

    The easiest syntax for deleting a row (say, Santa’s Little Helper) is:

    Code \(\PageIndex{1}\) (Python):

    simpsons = simpsons.drop('SLH')

    The .drop() method takes the index of the undesired row as an argument, Like most of the methods we’ve seen so far, it returns a modified copy of the DataFrame it’s called on, so you have to reassign this to the original variable (or use the inplace=True argument).

    You can even delete multiple rows at the same time by enclosing the undesired indices in boxies:

    Code \(\PageIndex{2}\) (Python):

    simpsons = simpsons.drop(['Homer','Marge','SLH'])

    Deleting a column is even more common, since many tables “in the wild” have many, many columns, only a few of which you may care about in your analysis. You can whack one entirely with the del operator, just like we did for Serieses (p. 111):

    Code \(\PageIndex{3}\) (Python):

    del simpsons['IQ']


    This page titled 16.3: Removing Rows/Columns is shared under a CC BY-SA 4.0 license and was authored, remixed, and/or curated by Stephen Davies (allthemath.org) via source content that was edited to the style and standards of the LibreTexts platform; a detailed edit history is available upon request.