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)

Removing rows using a slice

Slicing can be used to remove records from a data frame. It is a process similar to Boolean selection where we select out all of the rows except for the ones you want removed.

Suppose we want to remove all but the first three records from sp500. The slice to perform this task is [:3] which returns the first three rows.

Remember, that since this is a slice, the result is a view into the original data frame. The rows have not been removed from the sp500 data, and changes to these three rows will change the data in sp500. The proper action to prevent this is to make a copy of the slice which results in a new data frame with the data for the specified rows copied.