Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
timer SALE ENDS IN
0 Days
:
00 Hours
:
00 Minutes
:
00 Seconds
Arrow up icon
GO TO TOP
Python Data Analysis, Second Edition

You're reading from   Python Data Analysis, Second Edition Data manipulation and complex data analysis with Python

Arrow left icon
Product type Paperback
Published in Mar 2017
Publisher Packt
ISBN-13 9781787127487
Length 330 pages
Edition 2nd Edition
Languages
Arrow right icon
Author (1):
Arrow left icon
Ivan Idris Ivan Idris
Author Profile Icon Ivan Idris
Ivan Idris
Arrow right icon
View More author details
Toc

Table of Contents (22) Chapters Close

Python Data Analysis - Second Edition
Credits
About the Author
About the Reviewers
www.PacktPub.com
Customer Feedback
Preface
1. Getting Started with Python Libraries FREE CHAPTER 2. NumPy Arrays 3. The Pandas Primer 4. Statistics and Linear Algebra 5. Retrieving, Processing, and Storing Data 6. Data Visualization 7. Signal Processing and Time Series 8. Working with Databases 9. Analyzing Textual Data and Social Media 10. Predictive Analytics and Machine Learning 11. Environments Outside the Python Ecosystem and Cloud Computing 12. Performance Tuning, Profiling, and Concurrency Key Concepts
Useful Functions Online Resources

Pivot tables


A pivot table, as used in Excel, summarizes data. So far, the data in CSV files that we have seen in this chapter 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 ch-03.ipynb. The Pandas API has a top-level pivot_table() function and a corresponding DataFrame method. With the aggfunc parameter, we can specify the aggregation function to, say, use the NumPy sum() function. 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 preceding code can be found in ch-03.ipynb in...

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime
Visually different images