Deleting unwanted data

After adding that data to our dataframe in the previous section, we can see the need to delete unwanted data. We need a way to undo our mistakes and get rid of data that we aren't going to use. Like adding data, we can use dictionary syntax to delete unwanted columns, just as we would when removing keys from a dictionary. Both del df['<column_name>'] and df.pop('<column_name>') will work, provided that there is indeed a column with that name; otherwise, we will get a KeyError. The difference here is that while del removes it right away, pop() will return the column that we are removing. Remember that both of these operations will change our original dataframe, so use them with care.

Let's use dictionary notation to ...

Get Hands-On Data Analysis with Pandas now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.