利用Python进行数据分析(12)-高阶应用transform

如题所述

第1个回答  2022-07-01

本文中详解介绍了 pandas 中 transform() 方法的使用

Accepted combinations are:

{0 or ‘index’, 1 or ‘columns’}, default 0 If 0 or ‘index’: apply function to each column. If 1 or ‘columns’: apply function to each row.

Positional arguments to pass to func.

Keyword arguments to pass to func.

A DataFrame that must have the same length as self.

If the returned DataFrame has a different length than self.

transform方法通常是和groupby方法一起连用的

每个位置被均值取代

内建的聚合函数直接传递别名,max\min\sum\mean

向tranform中直接传递函数

在这个网站上有一个完整的实例,解释了transform方法的使用

You can see in the data that the file contains 3 different orders (10001, 10005 and 10006) and that each order consists has multiple products (aka skus).

The question we would like to answer is: “What percentage of the order total does each sku represent?”

For example, if we look at order 10001 with a total of $576.12, the break down would be:

求出不同商品在所在订单的价钱占比

先求出一列占比的值,再和原始数据进行合并merge

Transform + groupby连用:先分组再求和

相似回答