[英]Pandas how to apply a function to groupby().first()
我有一个 df,代码是:
df = """
ValOption RB test contrat
0 SLA 4 3 23
1 AC 5 4 12
2 SLA 5 5 23
3 AC 2 4 39
4 SLA 5 5 26
5 AC 3 4 52
6 SLA 4 3 64
0 SLA 4 3 23
1 AC 5 4 12
2 SLA 5 5 23
3 AC 2 4 39
4 SLA 5 5 26
5 AC 5 4 52
6 SLA 4 3 64
"""
df = pd.read_csv(StringIO(df.strip()), sep='\s+')
output:
ValOption RB test contrat
0 SLA 4 3 23
1 AC 5 4 12
2 SLA 5 5 23
3 AC 2 4 39
4 SLA 5 5 26
5 AC 3 4 52
6 SLA 4 3 64
0 SLA 4 3 23
1 AC 5 4 12
2 SLA 5 5 23
3 AC 2 4 39
4 SLA 5 5 26
5 AC 5 4 52
6 SLA 4 3 64
现在我将它分组并通过以下方式获得第一个:
df_u=df.groupby(['RB','test']).first()
output:
然后我想对每一行应用 function,出于某种原因我必须使用 pd.apply() function:
def func(row):
v1=row['RB']*3
v2=row['test']-1
return v1+v2
df_u['new_col']=df_u.apply(lambda row:func(row), axis=1)
注意:在实际业务中function很复杂很长,需要使用pd.apply()
然后我得到一个错误:
KeyError: ('RB', 'occurred at index (2, 4)')
您必须reset_index
才能访问“RB”和“测试”行。 使用.values
将值设置为new_col
:
df_u['new_col'] = df_u.reset_index().apply(func, axis=1).values
print(df_u)
# Output:
ValOption contrat new_col
RB test
2 4 AC 39 9
3 4 AC 52 12
4 3 SLA 23 14
5 4 AC 12 18
5 SLA 23 19
更新
如何将 new_col 返回到原来的 df?
df = df.merge(df.drop_duplicates(['RB', 'test'])
.assign(new_col=func)[['RB', 'test', 'new_col']],
on=['RB', 'test'], how='left')
# Output
ValOption RB test contrat new_col
0 SLA 4 3 23 14
1 AC 5 4 12 18
2 SLA 5 5 23 19
3 AC 2 4 39 9
4 SLA 5 5 26 19
5 AC 3 4 52 12
6 SLA 4 3 64 14
7 SLA 4 3 23 14
8 AC 5 4 12 18
9 SLA 5 5 23 19
10 AC 2 4 39 9
11 SLA 5 5 26 19
12 AC 5 4 52 18
13 SLA 4 3 64 14
更新2
我 drop_duplicates 的原因是为了节省时间,让它更快,因为行的长度是 60k,如果我应用到每一行,它会花费很多时间,而不是如果我先删除重复的,我不需要应用到每一行row,我直接给同列值行赋值
Apply 是一个 for 循环,使用矢量化:
df['new_col'] = (df['RB']*3) + (df['test']-1)
表现
对于 140,000 条记录,上述操作耗时 361 微秒:
%timeit (df1['RB']*3) + (df1['test']-1)
361 µs ± 9.02 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
对于 14 条记录(这不是错误),之前的操作耗时 935 µs:
%timeit df.drop_duplicates(['RB', 'test']).apply(func, axis=1)
935 µs ± 5.44 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.