<jupyter_start><jupyter_text>Grouping DataWith DataFrames giving us the opportunity to store huge grids of data, we will sometimes want to group particular parts of our data set for analysis. Pandas’ ‘groupby’ method gives us an easy way to do so.<jupyter_code>import pandas as pd

data = {'Opponent':
        ["<NAME>","Newtown FC", 
         "<NAME>", "<NAME>"],
       'Location':
        ["Home","Away","Away","Home"],
        'GoalsFor':
        [2,4,3,0],
        'GoalsAgainst':
        [4,0,2,2]}
matches = pd.DataFrame(data)
matches<jupyter_output><empty_output><jupyter_text>An obvious way to group this data is by home and away matches. use the ‘.groupby()’ method to do so.<jupyter_code>HAMatches = matches.groupby('Location').mean()
HAMatches
#Describes the dataset for each variable within - this is awesome!
matches.groupby('Location').describe()
matches.groupby('Location').describe().transpose()['Away']

print("All that work done in just " + 
      str(len("Matches.groupby('Location').describe().transpose()['Away']"))
     + " characters!")<jupyter_output>All that work done in just 58 characters!
{'fraction_non_alphanumeric': 0.1423455684870188, 'fraction_numerical': 0.008952551477170993, 'mean_word_length': 4.375, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, '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': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '106290', 'license': 'MIT', 'n_tokens_mistral': 359, 'n_tokens_neox': 343, 'n_words': 104, 'path': 'Data_Analysis_crash_course/Data Analysis Part Two Pandas/Grouping_Data.ipynb', 'repo_name': 'OTeeEnabor/FC_Python_Enabor', 'size': '1141'}