There's more...

There are a few main flavors of syntax that you will encounter when performing an aggregation. The following four blocks of pseudocode summarize the main ways you can perform an aggregation with the groupby method:

  1. Using agg with a dictionary is the most flexible and allows you to specify the aggregating function for each column:
>>> df.groupby(['grouping', 'columns']) 
.agg({'agg_cols1':['list', 'of', 'functions'],
'agg_cols2':['other', 'functions']})
  1. Using agg with a list of aggregating functions applies each of the functions to each of the aggregating columns:
>>> df.groupby(['grouping', 'columns'])['aggregating', 'columns'] 
.agg([aggregating, functions])
  1. Directly using a method following the aggregating columns instead of agg, applies just that method to each aggregating column. This way does not allow for multiple aggregating functions:
>>> df.groupby(['grouping', 'columns'])['aggregating', 'columns'] 
.aggregating_method()
  1. If you do not specify the aggregating columns, then the aggregating method will be applied to all the non-grouping columns:
>>> df.groupby(['grouping', 'columns']).aggregating_method()

In the preceding four code blocks it is possible to substitute a string for any of the lists when grouping or aggregating by a single column.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.168.8