I have a dataframe df that looks like this
No Country Sex Age Group
1 Spain M Young
2 Norway F Middle
3 Mexico M Elderly
...
My aim is to group this data based on country first, then do analysis based on sex and age groups, using .value_counts()
Thus, I used groupby on this via df2 = df.groupby(df.Country)and looped like this
for d in df2:
print(type(d))
I am getting this output:
<class 'tuple'>
<class 'tuple'>
<class 'tuple'>
...
I wanted to use
for d in df2:
print(pd.DataFrame(d).Sex.value_counts())
but I am getting this error
DataFrame constructor not properly called!
I had asked a similar question yesterday, but that was limited to a very particular query. What I want to learn is to know how to "group" layers into chunks and then do analysis on those particular chunks.
Thanks!
<class 'tuple'>is because.groupby()returns a tuple of size 2, where the first element is your grouping (i.e. the country) and the second is the data for that group, which is already a dataframe.