Groupby,apply Usage Notes

Source: Internet
Author: User

GroupBy GROUP by a guideline for dataframe

1. The common invocation form is:

 df[ " a   "]. Groupyby (Df[ '  b   " ]) df. Groupyby (df[  " b  " ,  " c  " ]) #   hierarchical index  DF. Groupyby ([  " b  , "  c   ' ]) #   

The above can be understood as the series as a grouping key, Y can also be any of the appropriate length of the array as a grouping key, not currently tried

2. Common methods:

Df. Groupyby (df['b']). Mean ()# non-numeric column data skip DF directly . Groupyby (df['b'). Size ()# Returns a series of grouped sizes (each value corresponds to the number of current key elements)

3. Examples of groupyby applications:

As given in the CSV file, columns contains [' City ', ' state ', ' Population '], so that the average of the population of all cities in each canton can be

df['population'. GroupBy (['State']). Mean ()

Note: The Groupyby function is described in Http://pandas.pydata.org/pandas-docs/stable/generated/pandas. Dataframe.groupby.html#pandas. Dataframe.groupby

Apply function-can be used for Dataframe and series operations, return is dataframe and series

1. Understanding: Apply splits the object he applies to a small level, then allows each sub-section after the split to implement the function operation in parentheses, and then combine the results of the operation together

2.apply can be said to be very common, here only a basic use case, and understand the use of lambda

energy[' energysupply']=energy['Energysupply'].apply (  Lambda x:x*1000000)

Note: Apply function description http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.apply.html#pandas.DataFrame.apply

Groupby,apply Usage Notes

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.