Pandas pivot Table pivot_table

Source: Internet
Author: User
Tags pandas pivot



The function pandas.pivot_table can be used to create spreadsheet-style pivot tables.

It takes a number of arguments

Data: A DataFrame Object
Values: A column or a list of columns to aggregate
Index: A column, Grouper, array which has the same length as data, or list of them. Keys to group by on the pivot table index. If An array was passed, it is being used as the same manner as column values.
Columns: A column, Grouper, array which has the same length as data, or list of them. Keys to group by on the Pivot table column. If An array was passed, it is being used as the same manner as column values.
Aggfunc: function to use as aggregation, defaulting to Numpy.mean

ImportNumPy as NPImportPandas as PDImportDATETIMEDF= PD. DataFrame ({'A': [' One',' One',' Both','three'] * 6,                   'B': ['A','B','C'] * 8,                   'C': ['Foo','Foo','Foo','Bar','Bar','Bar'] * 4,                   'D': Np.random.randn (24),                   'E': Np.random.randn (24),                   'F': [Datetime.datetime (i, 1) forIinchRange (1, 13)] +[Datetime.datetime (, I, 15) forIinchRange (1, 13)]}) pd.pivot_table (DF, index=['A','B'], columns=['C'], values='D', aggfunc=np.sum) pd.pivot_table (DF, index=['C'], columns=['A','B'], values='D', aggfunc= 'sum') pd.pivot_table (DF, index=['A','B'], columns=['C'], values=['D','E'], aggfunc=np.sum) pd.pivot_table (DF, index=['A','B'], columns=['C'], values=['D','E'], aggfunc=[Np.sum]) pd.pivot_table (DF, index=['A','B'], columns=['C'], values=['D','E'], aggfunc={'D': Len,'E': Np.sum}) Pd.pivot_table (DF, index=['A','B'], columns=['C'], values=['D','E'], aggfunc={'D': Len,'E': [Np.sum, Np.mean]}) Pd.pivot_table (DF, index=PD. Grouper (freq='M', key='F'), columns='C', values='D', Aggfunc=np.sum)# kinda like resample



Pandas pivot Table pivot_table

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.