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