[英]Pandas how to apply a function to groupby().first()
I have a df,the code is:我有一个 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: 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
Now I group it and get the first by:现在我将它分组并通过以下方式获得第一个:
df_u=df.groupby(['RB','test']).first()
output: output:
Then I want to apply a function to each row,and for some reason I have to use pd.apply() function:然后我想对每一行应用 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)
Notice:In real business the function is very complicated and long,so I need to use pd.apply()注意:在实际业务中function很复杂很长,需要使用pd.apply()
Then I get an error:然后我得到一个错误:
KeyError: ('RB', 'occurred at index (2, 4)')
You have to reset_index
to access the row 'RB' & 'test'.您必须
reset_index
才能访问“RB”和“测试”行。 Use .values
to set values to new_col
:使用
.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
Update更新
How to return the new_col to the original df?
如何将 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
Update2更新2
The reason I drop_duplicates is for saving time,make it faster because the length of row is 60k,if I apply to each row,it spend lots of time,instead if I drop the duplicated first,I don't need to apply to each row,I directly assign the value to the same column value row
我 drop_duplicates 的原因是为了节省时间,让它更快,因为行的长度是 60k,如果我应用到每一行,它会花费很多时间,而不是如果我先删除重复的,我不需要应用到每一行row,我直接给同列值行赋值
Apply is a for-loop like, use vectorization: Apply 是一个 for 循环,使用矢量化:
df['new_col'] = (df['RB']*3) + (df['test']-1)
Performance表现
For 140,000 records, the operation above took 361 µs:对于 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)
For 14 records (it's not a mistake), the previous operation took 935 µs:对于 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.