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__")

  

猜你喜欢

转载自www.cnblogs.com/wcyMiracle/p/12448110.html