Book Image

Python Data Analysis

By : Ivan Idris
Book Image

Python Data Analysis

By: Ivan Idris

Overview of this book

Table of Contents (22 chapters)
Python Data Analysis
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Key Concepts
Online Resources
Index

Pivot tables


A pivot table, as known from Excel, summarizes data. The data in CSV files that we have seen in this chapter so far has been in flat files. The pivot table aggregates data from a flat file for certain columns and rows. The aggregating operation can be sum, mean, standard deviations, and so on. We will reuse the data generating code from data_aggregation.py. The pandas API has a top-level pivot_table() function and corresponding DataFrame method. With the aggfunc parameter, we can specify the aggregation function to use the NumPy sum() function, for instance. The cols parameter tells pandas the column to be aggregated. Create a pivot table on the Food column as follows:

print pd.pivot_table(df, cols=['Food'], aggfunc=np.sum)

The pivot table we get contains totals for each food item:

Food    chocolate   icecream      soup
Number   8.000000  15.000000  19.00000
Price    5.986585  10.440071  13.83338

[2 rows x 3 columns]

The following code can be found in pivot_demo.py in this book...