Book Image

Learning pandas - Second Edition

By : Michael Heydt
Book Image

Learning pandas - Second Edition

By: Michael Heydt

Overview of this book

You will learn how to use pandas to perform data analysis in Python. You will start with an overview of data analysis and iteratively progress from modeling data, to accessing data from remote sources, performing numeric and statistical analysis, through indexing and performing aggregate analysis, and finally to visualizing statistical data and applying pandas to finance. With the knowledge you gain from this book, you will quickly learn pandas and how it can empower you in the exciting world of data manipulation, analysis and science.
Table of Contents (16 chapters)

Deleting columns

Columns can be deleted from a DataFrame by using the del keyword or the .pop() or .drop() method of the data frame. The behavior of each of these differs slightly:

  • del will simply delete the Series from the DataFrame (in-place)
  • pop() will both delete the Series and return the Series as a result (also in-place)
  • drop(labels, axis=1) will return a new data frame with the column(s) removed (the original DataFrame object is not modified)

The following demonstrates using del to delete the BookValue column from a copy of the sp500 data:

The following uses the .pop() method to remove the Sector column:

The .pop() method has the benefit that it gives us the popped columns.

The .drop() method can be used to remove both rows and columns. To use it to remove columns, specify axis=1: