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)

Adding new columns with [] and .insert()

New columns can be added to a data frame using the [] operator. Let's add a new column named RoundedPrice which will represent the rounding of values in the Price column.

The way that pandas performs this is by first selecting the Price column's data from sp500 and then all of the values in that Series are rounded. Then pandas aligns this new Series with the copy DataFrame and adds it as a new column named RoundedPrice. The new column is added to the end of the columns index.

The .insert() method can be used to add a new column at a specific location. The following inserts the RoundedPrice column between Sector and Price: