分组数据帧上的高效操作

2024-05-14 21:54:31 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个非常大的数据框架,我需要一个基于另一列的组内排序。我知道如何迭代组,对组执行操作,并将所有这些组合并回一个数据帧,但是这很慢,我觉得有更好的方法来实现这一点。这是输入和我想要的。输入:

ID   price
1    100.00
1    80.00
1    90.00
2    40.00
2    40.00
2    50.00

输出:

ID   price  order
1    100.00 3
1    80.00  1
1    90.00  2
2    40.00  1
2    40.00  2 (could be 1, doesn't matter too much)
2    50.00  3

由于这超过了约5kk的记录和约250000个IDs,因此效率非常重要。你知道吗


Tags: 数据方法框架id排序记录orderbe
2条回答

如果速度是你想要的,那么下面的应该是相当不错的,尽管它有点复杂,因为它在numpy中使用了复数排序。这类似于在包^{}中编写聚合排序方法时使用的方法(my me)。你知道吗

# get global sort order, for sorting by ID then price
full_idx = np.argsort(df['ID'] + 1j*df['price'])

# get min of full_idx for each ID (note that there are multiple ways of doing this)
n_for_id = np.bincount(df['ID'])
first_of_idx = np.cumsum(n_for_id)-n_for_id 

# subtract first_of_idx from full_idx
rank = np.empty(len(df),dtype=int)
rank[full_idx] = arange(len(df)) - first_of_idx[df['ID'][full_idx]]
df['rank'] = rank+1

在我的机器上,5m行需要2秒,这比使用pandas的groupby.rank快了大约100倍(尽管我实际上没有运行5m行的pandas版本,因为它需要太长的时间;我不确定@ayhan是如何在30秒内完成的,也许是pandas版本的不同?)。你知道吗

如果你使用这个,那么我建议彻底测试它,因为我没有。你知道吗

您可以使用rank

df["order"] = df.groupby("ID")["price"].rank(method="first")
df
Out[47]: 
   ID  price  order
0   1  100.0    3.0
1   1   80.0    1.0
2   1   90.0    2.0
3   2   40.0    1.0
4   2   40.0    2.0
5   2   50.0    3.0

在一个具有250000个ID(i5-3330)的5m行数据集上,大约需要30秒:

df = pd.DataFrame({"price": np.random.rand(5000000), "ID": np.random.choice(np.arange(250000), size = 5000000)})
%time df["order"] = df.groupby("ID")["price"].rank(method="first")
Wall time: 36.3 s

相关问题 更多 >

    热门问题