<jupyter_start><jupyter_text>PANDAS - GROUPBY<jupyter_code>import numpy as np
import pandas as pd

data = {'Company':['GOOGL','GOOGL','MSFT','MSFT','FB','FB'], 
        'Person':['Sam','Charlie','Amy','Vanessa','Carl','Sarah'],
        'Sales':[200,120,340,124,243,350]}
df=pd.DataFrame(data)
df
bycomp = df.groupby('Company') # pass the column name and it returns groupby object
bycomp
# call aggregate functions on groupby object
bycomp.mean() # it will automatically ignore non-numeric columns lik 'Person'
bycomp.sum()
bycomp.std()
bycomp.min()
bycomp.max()
bycomp.std()
bycomp.sum().loc['FB']
df.groupby('Company').count() # common way to use grouby
df.groupby('Company').describe() # describe returns a dataframe. descriptive statistics
df.groupby('Company').describe().transpose() # switch rows and columns
df.groupby('Company').describe().transpose()['FB'] # select whichever company you are interested in<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.16861081654294804, 'fraction_numerical': 0.019088016967126194, 'mean_word_length': 6.552, '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': '359900', 'license': 'MIT', 'n_tokens_mistral': 329, 'n_tokens_neox': 298, 'n_words': 74, 'path': 'S06Pandas/L07GroupBy.ipynb', 'repo_name': 'chaithrakc/datascience-bootcamp', 'size': '943'}