1

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!

2
  • 1
    the reason you get <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. Commented Jun 28, 2018 at 8:08
  • Thanks @asongtoruin ! Commented Jun 28, 2018 at 8:14

1 Answer 1

2

I think better is use:

s = df.groupby('Country').Sex.value_counts()
print (s)
Country  Sex
Mexico   M      1
Norway   F      1
Spain    M      1
Name: Sex, dtype: int64

But if want use loops is necessary unpack tuples name of group and df by adding another variable i:

df2 = df.groupby(df.Country)
for i, d in df2:
    print(type(d))
<class 'pandas.core.frame.DataFrame'>
<class 'pandas.core.frame.DataFrame'>
<class 'pandas.core.frame.DataFrame'>

Or use GroupBy.apply:

def func(x):
    print (x)
    a = x.Sex.value_counts()
    #another code 
    return a

df2 = df.groupby(df.Country).apply(func)
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! Groupby.apply is most useful to me!
@Vibhu - Yes, it is obviously nice use, but there is one gotcha, see Warning

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.