21-pandas_apply和transform

风流不在谈锋胜,袖手无言味最长。这篇文章主要讲述21-pandas_apply和transform相关的知识,希望能为你提供帮助。

import numpy as np import pandas as pd #1.聚合一次 df=pd.DataFrame({"age":[18,20,22,22,23,23], "name":["A","B","C","D","E","F"], "price1":[1000,900,800,700,600,600], "price2":[10,9,8,7,6,6]}) result=df.groupby("age")["price1","price2"].apply(max).add_prefix("A__") print(result) result=df.groupby("age")["price1","price2"].transform(max).add_prefix("A__") print(result)#transform按照自己的索引计算,可以自定义操作 result=df.groupby("age")["price1","price2"].transform(lambda x:x.sum()).add_prefix("A__")

【21-pandas_apply和transform】

    推荐阅读