【发布时间】:2018-08-25 14:19:51
【问题描述】:
我有一个 1200 万行数据集,其中 3 列作为唯一标识符,另外 2 列带有值。我正在尝试做一个相当简单的任务:
- 按三个标识符分组。这会产生大约 260 万个独特的组合
- 任务 1:计算列 Val1
的中位数
- 任务 2:在给定 Val2 的某些条件下计算列 Val1 的平均值
这是我的结果,使用pandas 和data.table(目前都是最新版本,在同一台机器上):
+-----------------+-----------------+------------+
| | pandas | data.table |
+-----------------+-----------------+------------+
| TASK 1 | 150 seconds | 4 seconds |
| TASK 1 + TASK 2 | doesn't finish | 5 seconds |
+-----------------+-----------------+------------+
我认为我可能对 pandas 做错了 - 将 Grp1 和 Grp2 转换为类别并没有太大帮助,在 .agg 和 .apply 之间切换也没有太大帮助。有什么想法吗?
以下是可重现的代码。
数据框生成:
import numpy as np
import pandas as pd
from collections import OrderedDict
import time
np.random.seed(123)
list1 = list(pd.util.testing.rands_array(10, 750))
list2 = list(pd.util.testing.rands_array(10, 700))
list3 = list(np.random.randint(100000,200000,5))
N = 12 * 10**6 # please make sure you have enough RAM
df = pd.DataFrame({'Grp1': np.random.choice(list1, N, replace = True),
'Grp2': np.random.choice(list2, N, replace = True),
'Grp3': np.random.choice(list3, N, replace = True),
'Val1': np.random.randint(0,100,N),
'Val2': np.random.randint(0,10,N)})
# this works and shows there are 2,625,000 unique combinations
df_test = df.groupby(['Grp1','Grp2','Grp3']).size()
print(df_test.shape[0]) # 2,625,000 rows
# export to feather so that same df goes into R
df.to_feather('file.feather')
Python 中的任务 1:
# TASK 1: 150 seconds (sorted / not sorted doesn't seem to matter)
df.sort_values(['Grp1','Grp2','Grp3'], inplace = True)
t0 = time.time()
df_agg1 = df.groupby(['Grp1','Grp2','Grp3']).agg({'Val1':[np.median]})
t1 = time.time()
print("Duration for complex: %s seconds ---" % (t1 - t0))
Python 中的任务 1 + 任务 2:
# TASK 1 + TASK 2: this kept running for 10 minutes to no avail
# (sorted / not sorted doesn't seem to matter)
def f(x):
d = OrderedDict()
d['Median_all'] = np.median(x['Val1'])
d['Median_lt_5'] = np.median(x['Val1'][x['Val2'] < 5])
return pd.Series(d)
t0 = time.time()
df_agg2 = df.groupby(['Grp1','Grp2','Grp3']).apply(f)
t1 = time.time()
print("Duration for complex: %s seconds ---" % (t1 - t0)) # didn't complete
等效的R代码:
library(data.table)
library(feather)
DT = setDT(feater("file.feather"))
system.time({
DT_agg <- DT[,.(Median_all = median(Val1),
Median_lt_5 = median(Val1[Val2 < 5]) ), by = c('Grp1','Grp2','Grp3')]
}) # 5 seconds
【问题讨论】:
-
使用函数:
%timeit在python中做计时 -
data.table在您在列上设置键时进行排序。对排序列的操作通常更快。 -
@Wen
DT = setDT(feater("file.feather"))不相关,数据帧来自SQL数据库,SQL提取时间大致相同。我使用feather只是为了确保 R 和 Python 都处理完全相同的数据,并且该示例是可重现的。稍后我将使用%timeit更新帖子,但我认为这并不重要 - 处理时间真的很长,有时甚至没有完成 -
@SergeyBushmanov 对数据框进行排序后,python 中的性能并没有提高。我更新了我的代码以反映这一点。
-
"为什么 R 的 data.table 比 pandas 快这么多?"因为马修·道尔很聪明。 data.table 中的众多优化之一是,当使用 by 语句时,内存只分配给最大的组并重复使用。这为那些需要为每个组分配和释放内存的任务节省了大量时间。
标签: r pandas data.table