How to perform a group by function in Python?
To perform a groupby function, we need to create a dataframe.Let us create a dataframe.
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
The dataframe looks like below
Now we 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')
We can save this object as a new variable:
by_comp = df.groupby("Company")
And then call aggregate methods off the object:
by_comp.mean()