Recent Searches
Category

Microsoft Power BI

Category

Microsoft Copilot Courses

Category

AI for Business

Category

Microsoft Windows 11 Courses

Category

Microsoft 365

Category

Microsoft Excel

Category

Excel Specialist

Category

Microsoft Project

Category

R Programming

Category

Python

Category

Power Apps

Category

SQL

Category

SharePoint

Category

Microsoft Teams

Category

Power Automate

Category

Microsoft Visio

Category

Microsoft PowerPoint

Category

Microsoft Word

Category

Microsoft Outlook

Category

Adobe InDesign Courses

Category

Adobe Photoshop Courses

Category

Adobe Illustrator Courses

Category

Adobe Premiere Pro Training

Category

Adobe After Effects Training

Category

Adobe Acrobat Courses

Category

Adobe Captivate Training

Category

Adobe Animate Training

Category

Canva Courses

Category

HTML Courses

Category

WordPress

Category

Professional Development

Category

Microsoft Access

Category

Webinars

Course

Power BI Beginner

Course

Copilot for M365

Course

Windows 11 End User Course

Course

Excel Beginner

Course

Financial Modelling

Course

Project Beginner

Course

R Programming Beginner

Course

Python Beginner

Course

Power Apps Beginner

Course

SQL Beginner

Course

SharePoint Beginner

Course

Teams Essentials

Course

Visio Essentials

Course

PowerPoint Level 1

Course

Word Intermediate

Course

Word Beginner

Course

Word Advanced

Course

Microsoft Outlook Beginner to Intermediate

Course

InDesign Lite

Course

Photoshop Lite

Course

Illustrator Training Intro

Course

Premiere Training Intro

Course

After Effects Training Intro

Course

Acrobat Essentials

Course

Captivate Training

Course

Animate Training Intro

Course

Canva AI

Course

HTML Training Intro

Course

Achieving Leadership & Success

Course

Microsoft Access Essentials

Course

Copilot for M365 On Demand

Course

Power BI Intermediate

Course

Copilot for Word

Course

ChatGPT Beginner

Course

Microsoft 365 Beginner

Course

Excel Intermediate

Course

Analysis and Dashboards

Course

Project Intermediate

Course

R Programming Intermediate

Course

Python Intermediate

Course

Power Apps Intermediate

Course

SQL Intermediate

Course

SharePoint Intermediate

Course

PowerPoint Level 2

Course

InDesign Training Intro

Course

Photoshop Training Intro

Course

Acrobat Forms

Course

Anger Management & Negotiation Skills

Course

Copilot for M365 Live Online

Course

Power BI Advanced

Course

Copilot for Excel

Course

AI Prompting Fundamentals

Course

Microsoft 365 Intermediate

Course

Excel Advanced

Course

Excel VBA

Course

Project Advanced

Course

R Programming Advanced

Course

Python Advanced

Course

Power Apps Intermediate | Power Automate + Power BI Integration

Course

SharePoint Advanced (Site Owner)

Course

InDesign Training Advanced

Course

Assertiveness & Confidence

Course

Microsoft Access Advanced

Course

Power BI DAX

Course

Copilot for PowerPoint

Course

Excel Expert

Course

Machine Learning in R

Course

Power Apps Advanced

Course

SharePoint Advanced (Document Governance)

Course

Building Resilience

Course

Copilot for Outlook and Teams

Course

Coaching and Mentoring

Course

Illustrator Training Advanced

Course

Premiere Basics Training

Course

Advanced After Effects Training

Course

Canva Beginners

Course

WordPress Essentials

Course

Communications

Course

AI for Business Leaders and Managers

Course

Photoshop Training Advanced

Course

Advanced Premiere Training

Course

Canva Intermediate

Course

Communications & Quality Client Service Training

Course

InDesign Interactivity Training

Course

Canva Advanced

Course

Critical Thinking and Problem Solving

Course

InDesign Accessibility Training

Course

Cultural Diversity in the Workplace

Course

Microsoft Outlook Advanced

Course

Embracing Change

Course

SQL Advanced

Course

Growing Emotional Intelligence

Course

Planner Premium

Course

Minute Taking

Course

Excel Tables and Pivot Tables

Course

Power Automate Beginner

Course

Performance Management

Course

Data Transformation with Power Query

Course

Excel Macro Mastery

Course

Power Automate Intermediate

Course

Persuasion and Negotiation Skills

Course

Power BI Desktop Advanced Reporting

Course

Presentation Skills and Public Speaking

Course

Data Visualisation with Power BI Desktop

Course

Practical Project Management

Course

Respect, Equity and Diversity (RED)

Course

Resumé Writing and Interview Skills

Course

Stress Management

Course

Team Leadership, Management and Development

Course

Time Management Intensive

Course

Train the Trainer

Course

Write Effective Business Documents

Course

Dealing with Difficult People

Course

Managing Difficult Conversations

Course

Managing the Virtual Workplace

Course

Customer Service Training

Course

Technical Writing

The pandas DataFrame groupby() method

Tamara Shatar | May 13, 2021
Pandas Header

Creating groups



In this example, we will work with a DataFrame that contains annual life expectancy and food availability (energy) data for 175 countries.

groupby()

 

groupby()

We can group by country and use the resulting groupby object to simplify reporting group statistics:

groupby Python
groupby Python


To report multiple statistics for each group, we can use the groupby object in combination with the agg() (aggregate) method.

groupby Python
groupby Python

Alternatively, we can change the grouping criteria, to instead report summaries of values by year.
groupby Python
groupby Python



Accessing groups of data

Individual groups can be accessed from the groupby object using the get_group() method. It can be used to simplify accessing subsets of data for exporting or other operations, like plotting.

groupby Python
groupby Python
groupby Python image
groupby Python image

 

Looping



The groupby object can also be used to perform iterative operations. We can loop over each group, retrieving both the data and the name of the group. The name of the group is the value of the variable used to group the data. The code below can be used to automate the process of exporting each group to its own csv file. Each file is saved with the group name.

groupby Python code
groupby Python code

By grouping on multiple columns, we can avoid using nested loops. In the example below, a column has been added (early_years) that indicates whether the data was collected prior to 1975 or after this date. By grouping the data on both country and entity, we can send data for each country, to two separate files; one for data prior to 1975 and one for data collected since 1975. Note, that the group names consist of tuples. These have been concatenated using the join() function to create a single name from each element of the tuple.

groupby Python screenshot
groupby Python screenshot


Filtering on number of data points

The groupby() method can also be used to determine the total number of rows of data for each group and used in combination with the filter() method to remove groups if they contain insufficient data.

The first line of code produces a regular DataFrame, that contains data only for those countries that have more than 53 years of data. By regrouping and reporting the count, we can see how many years of data are available for each of the remaining groups.

groupby Python example
groupby Python example

Learn more about Python, check out our Python Training Courses. Use the skills test to determine where you sit and then book into our basics, data analysis in python, and python intermediate. We also offer python certification. Available in person in our CBD locations or remote online across Australia. 

Trusted Nationwide by Leading Organisations

at Nexacu, we're proud to be the trusted training partner for hundreds of leading organisations accross Australia and New Zealand. From government departments to top corporates, we help teams upskill and succeed everyday

  • 400+ companies rely on Nexacu for team training
  • Trusted by federal, state, and local government agencies
  • Delivering training across 9 countries

Why Nexacu?

Books Icon

Step by Step Courseware

Custom workbook included with a step by step exercises

Facility Image 2
Facility Image 3
Facility Image 1

Refresh Icon

Free Refresher

Resit your course for free within 6 Months

More than 1,300 Business trust Nexacu

Trusted by Business

Procured by Government

Procured by Goverment

Reviews Not Found

Valued by Individuals