python pandas 中的Groupby:快速方法 [英] Groupby in python pandas: Fast Way

查看:65
本文介绍了python pandas 中的Groupby:快速方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想缩短python pandas中groupby的时间. 我有以下代码:

I want to improve the time of a groupby in python pandas. I have this code:

df["Nbcontrats"] = df.groupby(['Client', 'Month'])['Contrat'].transform(len)

目标是计算客户一个月内有多少份合同,并将此信息添加到新列(Nbcontrats)中.

The objective is to count how many contracts a client has in a month and add this information in a new column (Nbcontrats).

  • Client:客户端代码
  • Month:数据提取月份
  • Contrat:合同编号
  • Client: client code
  • Month: month of data extraction
  • Contrat: contract number

我想缩短时间.下面,我只处理部分真实数据:

I want to improve the time. Below I am only working with a subset of my real data:

%timeit df["Nbcontrats"] = df.groupby(['Client', 'Month'])['Contrat'].transform(len)
1 loops, best of 3: 391 ms per loop

df.shape
Out[309]: (7464, 61)

如何缩短执行时间?

推荐答案

使用DataFrameGroupBy.size方法:

df.set_index(['Client', 'Month'], inplace=True)
df['Nbcontrats'] = df.groupby(level=(0,1)).size()
df.reset_index(inplace=True)

最繁重的工作是将结果分配回源DataFrame的一列.

The most work goes into assigning the result back into a column of the source DataFrame.

这篇关于python pandas 中的Groupby:快速方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆