<jupyter_start><jupyter_text>Group By **In this tutorial we are going to look at weather data from various cities and see how group by can be used to run some analytics.**<jupyter_code>import pandas as pd
df = pd.read_csv("weather_by_cities.csv")
df<jupyter_output><empty_output><jupyter_text>For this dataset, get following answers, 1. What was the maximum temperature in each of these 3 cities? 2. What was the average windspeed in each of these 3 cities?<jupyter_code>g = df.groupby("city")
g<jupyter_output><empty_output><jupyter_text>**DataFrameGroupBy object looks something like below,**<jupyter_code>for city, data in g:
    print("city:",city)
    print("\n")
    print("data:",data)<jupyter_output>city: mumbai


data:         day    city  temperature  windspeed  event
4  1/1/2017  mumbai           90          5  Sunny
5  1/2/2017  mumbai           85         12    Fog
6  1/3/2017  mumbai           87         15    Fog
7  1/4/2017  mumbai           92          5   Rain
city: new york


data:         day      city  temperature  windspeed  event
0  1/1/2017  new york           32          6   Rain
1  1/2/2017  new york           36          7  Sunny
2  1/3/2017  new york           28         12   Snow
3  1/4/2017  new york           33          7  Sunny
city: paris


data:          day   city  temperature  windspeed   event
8   1/1/2017  paris           45         20   Sunny
9   1/2/2017  paris           50         13  Cloudy
10  1/3/2017  paris           54          8  Cloudy
11  1/4/2017  paris           42         10  Cloudy<jupyter_text>**This is similar to SQL,****SELECT * from weather_by_cities GROUP BY city**<jupyter_code>g.get_group('mumbai')
g.sum()
g.temperature.count()
g.get_group('mumbai').max()
g.max()<jupyter_output><empty_output><jupyter_text>m=mean<jupyter_code>g.mean()
g.min()
g.describe()
g.size()
g.count()
%matplotlib inline
g.plot()<jupyter_output><empty_output><jupyter_text>Group data using custom function: Let's say you want to group your data using custom function. Here the requirement is to create three groups    Days when temperature was between 80 and 90    Days when it was between 50 and 60    Days when it was anything else For this you need to write custom grouping function and pass that to groupby<jupyter_code>def grouper(df, idx, col):
    if 80 <= df[col].loc[idx] <= 90:
        return '80-90'
    elif 50 <= df[col].loc[idx] <= 60:
        return '50-60'
    else:
        return 'others'
g = df.groupby(lambda x: grouper(df, x, 'temperature'))
g
for key, d in g:
    print("Group by Key: {}\n".format(key))
    print(d)
help(df.groupby)<jupyter_output>Help on method groupby in module pandas.core.frame:

groupby(by=None, axis=0, level=None, as_index: bool = True, sort: bool = True, group_keys: bool = True, squeeze: bool = <object object at 0x0000015D373A07D0>, observed: bool = False, dropna: bool = True) -> 'DataFrameGroupBy' method of pandas.core.frame.DataFrame instance
    Group DataFrame using a mapper or by a Series of columns.
    
    A groupby operation involves some combination of splitting the
    object, applying a function, and combining the results. This can be
    used to group large amounts of data and compute operations on these
    groups.
    
    Parameters
    ----------
    by : mapping, function, label, or list of labels
        Used to determine the groups for the groupby.
        If ``by`` is a function, it's called on each value of the object's
        index. If a dict or Series is passed, the Series or dict VALUES
        will be used to determine the groups (the Series' values are first
        aligned; see[...]
{'fraction_non_alphanumeric': 0.08735188757233397, 'fraction_numerical': 0.04712041884816754, 'mean_word_length': 2.6703741152679474, 'pattern_counts': {'":': 0, '<': 28, '<?xml version=': 0, '>': 25, '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': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '23479', 'license': 'MIT', 'n_tokens_mistral': 1254, 'n_tokens_neox': 1135, 'n_words': 428, 'path': 'Pandas/07_group_by/group_by.ipynb', 'repo_name': 'sunilrudraraju/data_science', 'size': '3629'}