<jupyter_start><jupyter_text>https://www.geeksforgeeks.org/python-pandas-dataframe-groupby/  Groupby<jupyter_code>#  Pandas dataframe.groupby()

       # df.groupby()
       
       # df.first()
       
       # df.get_group
Pandas dataframe.groupby() function is used to split the data into groups based on some criteria.

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 iterable
    

    axis         : int, default 0
    

    level        : If the axis is a MultiIndex (hierarchical), group by a particular level or levels
    

    as_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 output
                   

    sort         : 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 pieces
    

    squeeze      : Reduce the dimensionality of the return type if possible, otherwise return a consistent type
    

    Returns      : GroupBy object<jupyter_output><empty_output><jupyter_text>1. Dataset : nba<jupyter_code>import pandas as pd
df = pd.read_csv("nba.csv")
df.head()<jupyter_output><empty_output><jupyter_text>1. Use groupby( ) function to group the data based on the “Team”<jupyter_code>df['Team'].value_counts().sort_index(ascending=True)
df['Team'].nunique()
df['Team'].shape
gk = df.groupby('Team')

# Print the first value in each group
gk.first()<jupyter_output><empty_output><jupyter_text>Finding the values contained in the "Boston Celtics" group<jupyter_code>gk.get_group('Boston Celtics')<jupyter_output><empty_output><jupyter_text>2. Use groupby( ) function to form groups based on more than one category (i.e. Use more than one column to perform the splitting)<jupyter_code>df['Position'].value_counts().sort_index(ascending=True)
df['Position'].nunique()
df['Position'].shape
# 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 group 
gkk.first()<jupyter_output><empty_output><jupyter_text>2. Dataset : Titanic<jupyter_code>train = pd.read_csv("titanic.csv")
train.head()
train.groupby('Survived').mean()
train.groupby('Sex').mean()
train.groupby(['Sex','Survived']).count()
train.groupby(['Sex','Survived'])['Age'].count()
train.groupby(['Pclass','Survived']).count()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11027027027027027, 'fraction_numerical': 0.002162162162162162, 'mean_word_length': 3.8531468531468533, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, 'https://': 1, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '86144', 'license': 'CNRI-Python', 'n_tokens_mistral': 887, 'n_tokens_neox': 854, 'n_words': 240, 'path': '8_Grouping and Aggregating/1_Groupby/2_Groupby_basic.ipynb', 'repo_name': 'sureshmecad/Pandas', 'size': '2775'}