Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Data Science by (17.6k points)

I had a dataframe and did a groupby in FIPS and summed the groups that worked fine.

kl = ks.groupby('FIPS')

kl.aggregate(np.sum)

I just want a normal Dataframe back but I have a pandas.core.groupby.DataFrameGroupBy object.

1 Answer

0 votes
by (41.4k points)

The result of kl.aggregate(np.sum) is a normal DataFrame, you just have to assign it to a variable to further use it. With some random data:

>>> df = DataFrame({'A' : ['foo', 'bar', 'foo', 'bar',

>>>                         'foo', 'bar', 'foo', 'foo'],

...                  'B' : ['one', 'one', 'two', 'three',

...                         'two', 'two', 'one', 'three'],

...                  'C' : randn(8), 'D' : randn(8)})

>>> grouped = df.groupby('A')

>>> grouped

<pandas.core.groupby.DataFrameGroupBy object at 0x04E2F630>

>>> test = grouped.aggregate(np.sum)

>>> test

            C         D

A                      

bar -1.852376  2.204224

foo -3.398196 -0.045082

If you wish to learn more about Pandas visit this Pandas Tutorial.

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...