Member-only story

All You Need to Know of the Pandas Transform Function

Edward Jones
2 min readDec 16, 2020

--

Why and how you should use the Pandas Transform function in your code

Photo by Pascal Müller on Unsplash

After reading this article, you will know:

  1. How you should use the Pandas transform function in combination with groupby()
  2. Use cases of the transform function

1. Pandas transform function

The Pandas transform is a relatively unknown function in the pandas packages.

You use it in conjunction with the groupby function to aggregate data but at the same time you keep your orignal shape.

If you use groupby with apply to summarize data, you notice that your data gets reduced on the level of the groupby variable.

However, if you want to keep your original shape, you should use transform.

2. Use cases

Imagine, we would want to know the earliest invoicedate of each customer.

Using a normal aggregation function

df.groupby('CustomerID')['InvoiceDate'].min()

Notice that your total rows have been reduced to 3 rows instead of 7 rows since the output only displays the minimum date for an unique customerID.

--

--

No responses yet