python agg函数_Python pandas 使用自定义agg函数通过groupby创建新列

我的 dataframe :

from random import random, randint

from pandas import DataFrame

t = DataFrame({"metasearch":["A","B","A","B","A","B","A","B"],

"market":["A","B","A","B","A","B","A","B"],

"bid":[random() for i in range(8)],

"clicks": [randint(0,10) for i in range(8)],

"country_code":["A","A","A","A","A","B","A","B"]})

我想为每个市场都适合LinearRegression,所以我:

1)组df-组= t.groupby(by =“ market”)

2)准备要适合模型的功能-

from sklearn.linear_model import LinearRegression

def group_fitter(group):

lr = LinearRegression()

X = group["bid"].fillna(0).values.reshape(-1,1)

y = group["clicks"].fillna(0)

lr.fit(X, y)

return lr.coef_[0] # THIS IS A SCALAR

3)创建一个新的系列,以市场为指数,以系数为值:

s = groups.transform(group_fitter)

但是第3步失败:KeyError :(“ bid_cpc”,“在出价时出现”)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值