Python Data Analysis(Second Edition)
上QQ阅读APP看书,第一时间看更新

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 this book's code bundle.