I am learning pandas through a course. In the groupby video of the course I got confused over this statement.
df.set_index('STNAME').groupby(level=0)['CENSUS2010POP'].agg(avg=np.average, sum=np.sum).head()
Output is:
In this statement the subscript operator ['CENSUS2010POP'] is placed after groupby. My question is that groupby returns a groupby object in which it has tuples. The tuples are pairs of the data on which grouping was done and dataframes based on that grouping condition. How can we subscript a groupby object in the first place. Moreover when I write the query in the following way it still works.
df.set_index('STNAME')['CENSUS2010POP'].groupby(level=0).agg(avg=np.average, sum=np.sum).head()
Why does placing the subscript operator after groupby work? Why do both statements work the same way? Is there even a difference between both statements?
