Use the agg() method to apply aggregate methods to columns of a DataFrame.
The method takes as argument a method name, a list of method names or a dictionary of column name to method names.
The required argument func specifies the functions to apply on the DataFrame columns.
Valid values for this argument are: 'count', 'sum', 'min', 'max', 'mean', 'std', 'percentile', 'unique', 'median', 'var'.
Acceptable formats for the functions are: string, dictionary or list of strings or functions.
Accepted combination are:
- String function name
- List of string functions
- Dictionary containing column name as key and aggregate function name (string or list of strings) as value
Example Prerequisite
>>> df = DataFrame("employee_info") >>> df first_name marks dob joined_date employee_no 100 abcd None None None 101 abcde None None 1902-05-12 112 None None None 2018-05-12
Example: Use dictionary of column names to lists of method names
>>> df.agg({'employee_no' : ['min', 'sum'], 'first_name' : ['min', 'mean']}) min_employee_no sum_employee_no min_first_name 0 100 313 abcd
Example: Apply the methods min and sum to all the columns
>>> df.agg(['min', 'sum']) min_employee_no sum_employee_no min_first_name min_marks sum_marks min_dob min_joined_date 0 100 313 abcd None None None 1902-05-12
Example: Apply the method mean to all the columns
>>> df.agg('mean') mean_employee_no mean_marks mean_dob mean_joined_date 0 104.333333 None None 1960-05-11
Example: Apply the method mean and unique to selected columns
>>> df1 = df.select(['employee_no', 'first_name', 'joined_date'])
>>> df1.agg(['mean', 'unique']) mean_employee_no unique_employee_no unique_first_name mean_joined_date unique_joined_date 0 104.333333 3 2 1960-05-11 2
Example: Apply the method percentile to all the columns
>>> df.agg('percentile') percentile_employee_no percentile_marks 0 101 None