Python Forum
pandas dataframe group by count index
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
pandas dataframe group by count index
#1
Dear Python Experts,
I am trying to group by the column Continent and count each country name (index) in it as well as sum the popluation.
The groupby I have written does...nothing.

    elevenstat = eleven.groupby(['Continent',"PopEst"]).sum()
I think I miss the part that counds the index.

Attached an image showing the code snipped an output.

[Image: groupby.jpg]

I would very much appreciate any help!
Reply
#2
You'll need to post more than 1 line of code in order to receive a respectable answer  Smile
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply
#3
Hi sparkz_alot,
Thanks for your reply.
What else would you like to know?
Reply
#4
If you're grouping by continent, then why are you also grouping by population?
Reply
#5
If you need to group dataset by continents and sum population and count countries (stored in index), you dont need to group by the index, you just need one grouping (by continent), but you need to do two aggregations - sum and count. And if your index (countries) contains only unique values, then country counting is same as counting any column in the dataframe.

You can do it in two steps, when you do sum / count seperately and then you merge results. Or you can do it in one pass with .agg() function.

Spoiled examples:

Example dataframe:

Using .agg() :

"Simpler" approach with separate steps:
If your index is not unique, probably simplest solution is to add index as another column (country) to dataframe and instead count() use nunique() on countries.

And while .agg() is not so well known function, 10 Minutes to pandas contains more than enough informations to deduce separate summing/counting followed by merge.
Reply
#6
Hi nilamo,
Many thank for your reply.
I took out the group by "PopEst" but it did not make a difference.
Good idea nevertheless.

@zivoni,
Thats it. Two aggregate functions sum and count(or nunique()) and using agg().
I was looking for that.
Thank you for the simply solution as well. I did not think about that and then joining the
tables.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Pandas dataframe indexing pythonNovice 1 993 Jun-16-2022, 04:43 PM
Last Post: jefsummers
  How can I convert specific rows from excel to pandas dataframe? mcva 1 1,797 Apr-20-2020, 09:14 AM
Last Post: pyzyx3qwerty
  Histogram using pandas dataframe not showing proper output ift38375 1 2,184 Jul-04-2019, 10:43 PM
Last Post: scidam
  Count Letters in a Sentence (without using the Count Function) bhill 3 5,165 Jun-19-2018, 02:52 AM
Last Post: bhill
  pandas dataframe next rows value metalray 2 10,140 Mar-06-2017, 11:31 AM
Last Post: metalray
  pandas dataframe.replace regex metalray 3 24,626 Feb-24-2017, 12:58 PM
Last Post: zivoni
  pandas dataframe substracting columns: key error metalray 2 7,027 Feb-24-2017, 07:59 AM
Last Post: metalray

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020