Dropping Columns and Rows in PANDAS

charlie fountain
2 min readJun 2, 2021

Charlie Fountain

Getting started with pandas can be very overwhelming. Today, I am going to teach you about the drop function. The drop function is used to remove or (drop) specific information from a data frame that we no longer need. This is just one of the steps to cleaning our data.

To drop a specific row or column, you have to use .drop(). On the inside of the parenthesis, you need to call what you are dropping. For Example, if you wanted to drop the first row of your data frame, you would do df = df.drop([1], axis = 0). This drops the first row of your data set.

This is called Syntax and shows you the variety of methods you can use to drop data.

Syntax DataFrame.drop(self, labels=None, axis=0, index=None, columns=None, level=None, inplace=False, errors='raise')

Let's pretend we were given this data set but want to drop columns Q and R . How would we do it?

Note: Assume the data frame is set equal to df

df.drop(['Q', 'R'], axis=1)

This will drop columns Q and R and return our data frame looking like this…

It knows the drop columns Q and r because when it reads axis=1 it knows we are talking about columns.

Congratulations on dropping your columns! Now, let us try to drop a row.

Let's return to the original data frame.

Dropping a row is very similar to dropping a column but only easier! Because our rows are indexed all you have to do is put in the number of the rows you want to drop.

df.drop([0, 1])

This will output our data frame to look like this…

As you can tell rows 0 and 1 were dropped and we are left with row 2’s information. This can be very useful when some rows or columns are filled with missing data or other things we don't want in our data set.

Now you know how to drop columns and rows! Hope this helped!

--

--