其他分享
首页 > 其他分享> > 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,df,age,transform,result,price2,apply,price1
来源: https://www.cnblogs.com/wcyMiracle/p/12448110.html