Urgent.News

What's breaking now, across thousands of outlets.

Tech

pandas GroupBy: How to Summarize a DataFrame Without Losing Track of Your Rows

By Michael Nocito , data analyst · Published August 7, 2026 By the end of this page you can take a DataFrame, summarize it by any column or combination of columns, get several statistics at once with sensible names, and account for every row that went in, including the ones pandas would otherwise drop without telling you. It is about twenty-five minutes, and every output shown was produced by…

1. Understanding groupby: split, apply, combine

The groupby operation in pandas works through a three-step process. First, it splits the DataFrame into separate mini-tables based on unique values in the grouping column(s). In this case, the DataFrame is divided into three groups: East, South, and West. Second, it applies a specified function, such as calculating the mean, to each mini-table individually. Lastly, it combines the results into a new table with one row per group. This process is referred to as split-apply-combine.

2. The basic usage: one column and one statistic

The fundamental command for summarizing a DataFrame using groupby is:

df.groupby('region')['amount'].mean()

This computes the mean of the 'amount' column for each unique 'region' value. For example, the East region has a mean amount of 158.00, South has 83.33, and West has 186.67. Note that the mean calculation in pandas automatically skips missing values (np.nan). When applied to the 'amount' column, it results in a Series with the group values as the index.

3. Multiple statistics simultaneously: using agg and named aggregation

Instead of computing just one statistic, you can calculate multiple statistics at once using the agg function. For instance:

df.groupby('region')['amount'].agg(['mean', 'sum', 'size', 'count'])

This returns a multi-index DataFrame, where each group has four summary statistics: mean, sum, count of rows, and size of the group. You can also use named aggregation for cleaner output:

df.groupby('region')['amount'].agg(mean='mean', total='sum', cnt='size', count='count')

This gives the same results but with named columns for each statistic.

Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.

Read the original at dev.to →

More in Tech

10 Ways to Improve Your iPhone's Battery Life

When you're at home or work with regular access to an iPhone charger, you probably don't need to worry much about your iPhone's battery. On days when you're away from home for long stretches, you might want to make some adjustments to extend battery life. We've rounded up 10 tips that can cut down on battery drain. 1.

How to Find Duplicate Rows in SQL (and Decide What Counts as One)

By Michael Nocito , data analyst · Published August 7, 2026 By the end of this page you can check any table for duplicate rows, list every copy, and mark which one to keep, all with queries you…

  • Decide what defines a duplicate (all columns or specific column)
  • Compare total row count to distinct key count using COUNT() vs COUNT(DISTINCT key)
  • Group rows by duplicate columns, keep groups with COUNT() > 1, mark extras with ROWNUMBER

Month-over-Month Growth in SQL: LAG, the Growth Formula, and the Traps

By Michael Nocito , data analyst · Published August 7, 2026 By the end of this page you can write a month-over-month growth query and trust its answer.

  • Month-over-month growth calculated using LAG function
  • LAG function fetches previous month's revenue for comparison
  • Three common traps can lead to incorrect growth calculations

NULL in SQL: Why = NULL Finds Nothing and What to Write Instead

By Michael Nocito , data analyst · Published August 7, 2026 By the end of this page you can predict what any query does when it meets a missing value, which is the skill that separates "my query returned nothing and I don't know why" from a two-second fix.

  • NULL represents unknown or missing values in SQL
  • Comparing with = NULL always returns unknown and excludes rows
  • IS NULL operator checks specifically for NULL values

More from Friday 21 August →