The groupby method allows you to group rows of data together and call aggregate functions
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
Company
Person
Sales
0
GOOG
Sam
200
1
GOOG
Charlie
120
2
MSFT
Amy
340
3
MSFT
Vanessa
124
4
FB
Carl
243
5
FB
Sarah
350
** Now you can use the .groupby() method to group rows together based off of a column name. For instance let’s group based off of Company. This will create a DataFrameGroupBy object:**
df.groupby('Company')
<pandas.core.groupby.DataFrameGroupBy object at 0x000001B93BF3DF60>
You can save this object as a new variable:
by_comp = df.groupby("Company")
<pandas.core.groupby.DataFrameGroupBy object at 0x000001B93D50EF98>