Select your country to view prices and course dates.

x

Recent Searches
Category

Microsoft Copilot Courses

Category

Microsoft Power BI

Category

AI for Business

Category

Microsoft Excel

Category

Professional Development

Category

Microsoft Office 365

Category

Excel Specialist

Category

Microsoft Project

Category

R Programming

Category

Python

Category

SQL

Category

Power Apps

Category

SharePoint

Category

Power Automate

Category

Microsoft Teams

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

HTML Courses

Category

WordPress

Category

Canva Courses

Category

Microsoft Access

Category

Webinars

Course

Copilot for M365

Course

Power BI Beginner

Course

ChatGPT Beginner

Course

Excel Beginner

Course

Achieving Leadership & Success

Course

Microsoft 365 Beginner

Course

Financial Modelling

Course

Project Beginner

Course

R Programming Beginner

Course

Python Beginner

Course

SQL Beginner

Course

Power Apps Beginner

Course

SharePoint Beginner

Course

Teams Essentials

Course

Visio Essentials

Course

PowerPoint Level 1

Course

Word Beginner

Course

Microsoft Outlook Beginner

Course

InDesign Lite

Course

Photoshop Training Intro

Course

Illustrator Training Intro

Course

Premiere Training Intro

Course

After Effects Training Intro

Course

Acrobat Essentials

Course

Captivate Training

Course

Animate Training Intro

Course

HTML Training Intro

Course

WordPress Essentials

Course

Canva Beginners

Course

Microsoft Access Essentials

Course

Copilot for Word

Course

Power BI Intermediate

Course

Excel Intermediate

Course

Microsoft 365 Intermediate

Course

Analysis and Dashboards

Course

Project Intermediate

Course

R Programming Intermediate

Course

Python Intermediate

Course

SQL Intermediate

Course

Power Apps Intermediate

Course

SharePoint Intermediate

Course

PowerPoint Level 2

Course

Word Intermediate

Course

InDesign Training Intro

Course

Photoshop Lite

Course

Illustrator Training Advanced

Course

Premiere Basics Training

Course

Acrobat Forms

Course

Canva Intermediate

Course

Microsoft Access Advanced

Course

Copilot for Excel

Course

Power BI Advanced

Course

Excel Advanced

Course

Microsoft 365 Advanced

Course

Excel VBA

Course

Project Advanced

Course

R Programming Advanced

Course

Python Advanced

Course

SQL Advanced

Course

Power Apps Advanced

Course

SharePoint Advanced (Site Owner)

Course

Word Advanced

Course

InDesign Training Advanced

Course

Photoshop Training Advanced

Course

Advanced Premiere Training

Course

Advanced After Effects Training

Course

Canva Advanced

Course

Copilot for PowerPoint

Course

Power BI DAX

Course

Excel Expert

Course

Machine Learning in R

Course

SharePoint Advanced (Document Governance)

Course

InDesign Interactivity Training

Course

Copilot for Outlook and Teams

Course

InDesign Accessibility Training

Course

Power Automate Beginner

Course

Power Automate Intermediate

Course

Microsoft Outlook Advanced

Course

AI Prompting Fundamentals

Course

Power Automate Advanced

Course

Excel Tables and Pivot Tables

Course

Data Transformation with Power Query

Course

Excel Macro Mastery

Course

Power BI Desktop Advanced Reporting

Course

AI for Business Leaders and Managers

Course

Data Visualisation with Power BI Desktop

Course

Anger Management & Negotiation Skills

Course

Assertiveness & Confidence

Course

Building Resilience

Course

Coaching and Mentoring

Course

Communications

Course

Communications & Quality Client Service Training

Course

Critical Thinking and Problem Solving

Course

Cultural Diversity in the Workplace

Course

Embracing Change

Course

Growing Emotional Intelligence

Course

Minute Taking

Course

Persuasion and Negotiation Skills

Course

Presentation Skills and Public Speaking

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

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 and Pandas check out our Python Training Courses. Available remote online across South-East Asia. 

Trusted Globally by Leading Organisations

At Nexacu, we are proud to be the trusted training partner for hundreds of leading organisations across Australia, New Zealand, and around the world. From government agencies to multinational corporations, we help teams build practical skills and achieve real outcomes through expert-led training.

  • 400+ companies rely on Nexacu for workforce development
  • Trusted by government agencies at all levels
  • Delivering training across 9 countries and growing

Why Nexacu? 

step by step courseware

Step by Step Courseware

Custom workbook included with a step by step exercises

Facility Image 2
Facility Image 3
Facility Image 1

Interactive real time training

Interactive, Real-Time Training

Learn with expert instructors, wherever you are

Trusted by Business

Trusted by Business

Procured by Government

Procured by Goverment

Reviews Not Found

Valued by Individuals