2 回答
TA贡献1847条经验 获得超11个赞
如果我正确理解您的问题,则需要以下内容:
def myfunc(group):
# only modify cost if there are nan's
if len(group) != group.cost.count():
# set all cost values to the mean
group['cost'] = group.cost.sum() / len(group)
# multiply by 1.5 if the duration equals 1
group['cost'][group.duration == 1] = group['cost'] * 1.5
return group
df.groupby('channel').apply(myfunc)
duration cost channel
0 2 60 TV1
1 1 120 TV2
2 2 100 TV3
3 1 90 TV1
4 2 80 TV2
5 2 100 TV3
6 2 60 TV1
7 1 120 TV2
8 1 150 TV3
TA贡献1804条经验 获得超2个赞
在新版本的Pandas中,代码应更改为
def myfunc(group):
# only modify cost if there are nan's
if len(group) != group.cost.count():
# set all cost values to the mean
group['cost'] = group.cost.sum() / len(group)
# multiply by 1.5 if the duration equals 1
_ = group.set_value(group[group.duration == 1].index, 'cost', group['cost'] * 1.5)
return group
添加回答
举报