Python | Pandas dataframe.groupby() - GeeksforGeeks

28 Jun, 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.
Pandas groupby is used for grouping the data according to the categories and apply a function to the categories. It also helps to aggregate data efficiently.
Pandas dataframe.groupby() function is used to split the data into groups based on some criteria. pandas objects can be split on any of their axes. The abstract definition of grouping is to provide a mapping of labels to group names.
Syntax: DataFrame.groupby(by=None, axis=0, level=None, as_index=True, sort=True, group_keys=True, squeeze=False, **kwargs)
Parameters :by : mapping, function, str, or iterableaxis : int, default 0level : If the axis is a MultiIndex (hierarchical), group by a particular level or levelsas_index : For aggregated output, return object with group labels as the index. Only relevant for DataFrame input. as_index=False is effectively “SQL-style” grouped outputsort : Sort group keys. Get better performance by turning this off. Note this does not influence the order of observations within each group. groupby preserves the order of rows within each group.group_keys : When calling apply, add group keys to index to identify piecessqueeze : Reduce the dimensionality of the return type if possible, otherwise return a consistent type
Returns : GroupBy object
For link to CSV file Used in Code, click here
Example #1: Use groupby() function to group the data based on the “Team”.
# importing pandas as pdimport pandas as pd  # Creating the dataframe df = pd.read_csv("nba.csv")  # Print the dataframedf
Now apply the groupby() function.
# applying groupby() function to# group the data on team value.gk = df.groupby('Team')  # Let's print the first entries# in all the groups formed.gk.first()
Output :
Let’s print the value contained any one of group. For that use the name of the team. We use the function get_group() to find the entries contained in any of the groups.
# Finding the values contained in the "Boston Celtics" groupgk.get_group('Boston Celtics')
Output : Example #2: Use groupby() function to form groups based on more than one category (i.e. Use more than one column to perform the splitting).
# importing pandas as pdimport pandas as pd  # Creating the dataframe df = pd.read_csv("nba.csv")  # First grouping based on "Team"# Within each team we are grouping based on "Position"gkk = df.groupby(['Team', 'Position'])  # Print the first value in each groupgkk.first()
Output :
groupby() is a very powerful function with a lot of variations. It makes the task of splitting the dataframe over some criteria really easy and efficient.
sonusharma
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
How to Install PIP on Windows ?
Enumerate() in Python
Different ways to create Pandas Dataframe
Iterate over a list in Python
Python String | replace()
*args and **kwargs in Python
Reading and Writing to text files in Python
Create a Pandas DataFrame from Lists
Convert integer to string in Python