<jupyter_start><jupyter_text>GroupbyThe groupby method allows you to group rows of data together and call aggregate functions<jupyter_code>import pandas as pd
# Create dataframe
data = {'Company':['GOOG','GOOG','MSFT','MSFT','FB','FB'],
       'Person':['Sam','Charlie','Amy','Vanessa','Carl','Sarah'],
       'Sales':[200,120,340,124,243,350]}
df = pd.DataFrame(data)
df
# GroupBy company
byComp = df.groupby('Company')
byComp
# Mean by company
# byComp.mean will only make mean of sales only 
# because of integers data-type
byComp.mean()
# SUM 
byComp.sum()
# Standard Deviations
byComp.std()
# Some indexing
print(f"\n\n 1. SUM of sales in FB: \n\n {byComp.sum().loc['FB']}")
print(f"\n\n 2. SUM of sales in GOOG: \n\n {byComp.sum().loc['FB']}")
print(f"\n\n 3. SUM of sales in MSFT: \n\n {byComp.sum().loc['FB']}")
# Counting the number of instances in a column
df.groupby('Company').count()
# Max value 
# Alphabeical order in string value
df.groupby('Company').max()
# Min value
# Alphabeical order in string value
df.groupby('Company').min()
# DESCRIBE
# This is very useful
# Used if want everything at a time
df.groupby('Company').describe()
# Transposed form
df.groupby('Company').describe().transpose<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.16747181964573268, 'fraction_numerical': 0.016908212560386472, 'mean_word_length': 5.2462311557788945, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '602282', 'license': 'MIT', 'n_tokens_mistral': 459, 'n_tokens_neox': 431, 'n_words': 123, 'path': 'Pandas/GroupBy.ipynb', 'repo_name': 'HimanshuPoptani/Machine-Learing', 'size': '1242'}