官术网_书友最值得收藏!

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.

主站蜘蛛池模板: 遂宁市| 亳州市| 家居| 濉溪县| 广宁县| 兴隆县| 腾冲县| 桐城市| 叙永县| 阿城市| 射阳县| 新竹县| 仲巴县| 普陀区| 红河县| 晋城| 壶关县| 武乡县| 新沂市| 西乌| 唐山市| 通化县| 聂拉木县| 鲁山县| 盈江县| 双桥区| 汝州市| 东光县| 肇东市| 车险| 庄河市| 昭通市| 承德县| 博湖县| 赤水市| 凌云县| 临沭县| 云安县| 黔西县| 吉隆县| 丹江口市|