Python | Pandas dataframe.aggregate()

19 Feb, 2021
Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric Python packages. Pandas is one of those packages and makes importing and analyzing data much easier.
Dataframe.aggregate() function is used to apply some aggregation across one or more column. Aggregate using callable, string, dict, or list of string/callables. Most frequently used aggregations are:
sum: Return the sum of the values for the requested axismin: Return the minimum of the values for the requested axismax: Return the maximum of the values for the requested axis
Syntax: DataFrame.aggregate(func, axis=0, *args, **kwargs)
Parameters:func : callable, string, dictionary, or list of string/callables. Function to use for aggregating the data. If a function, must either work when passed a DataFrame or when passed to DataFrame.apply. For a DataFrame, can pass a dict, if the keys are DataFrame column names.axis : (default 0) {0 or ‘index’, 1 or ‘columns’} 0 or ‘index’: apply function to each column. 1 or ‘columns’: apply function to each row.
Chapters
descriptions off, selected
captions settings, opens captions settings dialog
captions off, selected
English
This is a modal window.
Beginning of dialog window. Escape will cancel and close the window.
End of dialog window.
Returns: Aggregated DataFrame
For link to CSV file Used in Code, click here
Example #1: Aggregate ‘sum’ and ‘min’ function across all the columns in data frame.
# importing pandas packageimport pandas as pd  # making data frame from csv filedf = pd.read_csv("nba.csv")  # printing the first 10 rows of the dataframedf[:10]
Aggregation works with only numeric type columns.
# Applying aggregation across all the columns # sum and min will be found for each # numeric type column in df dataframe  df.aggregate(['sum', 'min'])
Output:For each column which are having numeric values, minimum and sum of all values has been found. For dataframe df , we have four such columns Number, Age, Weight, Salary. Example #2:
In Pandas, we can also apply different aggregation functions across different columns. For that, we need to pass a dictionary with key containing the column names and values containing the list of aggregation functions for any specific column.
# importing pandas packageimport pandas as pd  # making data frame from csv filedf = pd.read_csv("nba.csv")  # We are going to find aggregation for these columnsdf.aggregate({"Number":['sum', 'min'],              "Age":['max', 'min'],              "Weight":['min', 'sum'],               "Salary":['sum']})
Output:Separate aggregation has been applied to each column, if any specific aggregation is not applied on a column then it has NaN value corresponding to it.Python | Pandas dataframe.aggregate() | GeeksforGeeks - YouTubeGeeksforGeeks529K subscribersPython | Pandas dataframe.aggregate() | GeeksforGeeksWatch laterShareCopy linkInfoShoppingTap to unmuteIf playback doesn't begin shortly, try restarting your device.More videosMore videosYou're signed outVideos you watch may be added to the TV's watch history and influence TV recommendations. To avoid this, cancel and sign in to YouTube on your computer.CancelConfirmSwitch cameraShareInclude playlistAn error occurred while retrieving sharing information. Please try again later.Watch on0:000:000:00 / 3:11•Live•<div class="player-unavailable"><h1 class="message">An error occurred.</h1><div class="submessage"><a href="https://www.youtube.com/watch?v=RCLtgM2LLhs" target="_blank">Try watching this video on www.youtube.com</a>, or enable JavaScript if it is disabled in your browser.</div></div>
Python pandas-dataFrame
Python pandas-dataFrame-methods
Python-pandas
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Python Dictionary
Different ways to create Pandas Dataframe
Enumerate() in Python
Read a file line by line in Python
Python String | replace()
How to Install PIP on Windows ?
*args and **kwargs in Python
Python Classes and Objects
Iterate over a list in Python
Python OOPs Concepts