通过熊猫获得最大利益的最快方法

时间:2018-08-22 14:23:57

标签: python pandas performance

给出了一个包含3列的数据框:['User', 'Feature', 'Value']。我需要为每个用户获得TOP-N功能。

我尝试了几种选择,但它们似乎都太慢了。实际上,pandas最大的方法似乎比手工制作的方法要慢,如以下代码所示:

import pandas as pd                                                                                                                                                                                         
import numpy as np                                                                                                                                                                                          
import time                                                                                                                                                                                                 

np.random.seed(0)                                                                                                                                                                                           

max_rows = 1000000                                                                                                                                                                                          
n_users = 10000                                                                                                                                                                                             
max_entries = 100                                                                                                                                                                                           

df = pd.DataFrame()                                                                                                                                                                                         
df['Value'] = [i *  0.1 for i in range(max_rows)]                                                                                                                                                           
df['User'] = ['u%i'%i  for i in np.random.randint(0, n_users, size=max_rows)]                                                                                                                               
df['Feature'] = ['f%i'%i for i in range(max_rows)]                                                                                                                                                          

# PART 1                                                                                                                                                                                                    
t0 = time.time()                                                                                                                                                                                            
dfg = df.groupby(['User', 'Feature'], sort=False, as_index=True).sum()                                                                                                                                      
t1 = time.time()                                                                                                                                                                                            
print "first groupby takes: ", t1-t0,  "for nrows=", max_rows                                                                                                                                               


# PART 2                                                                                                                                                                                                    
#Option1                                                                                                                                                                                                    
dfg2 = dfg.groupby(dfg.index.get_level_values(0),                                                                                                                                                           
                   group_keys=False,                                                                                                                                                                        
                   sort=False).apply(lambda x: x.sort_values('Value', ascending=False).\                                                                                                                    
                                     iloc[:max_entries])                                                                                                                                                    
t2 = time.time()                                                                                                                                                                                            
print "second groupby takes:", t2-t1                                                                                                                                                                        


# PART 2                                                                                                                                                                                                    
#Option2                                                                                                                                                                                                    
dfg = dfg.reset_index(level='Feature')                                                                                                                                                                      
dfg3 = dfg.groupby(dfg.index.get_level_values(0), sort=False, group_keys=False).\                                                                                                                           
       apply(lambda x: x.nlargest(max_entries, 'Value'))                                                                                                                                                    
t3 = time.time()                                                                                                                                                                                            
print "Third groupby takes", t3-t2                                                                                                                                                                          

示例输出:

n [29]: first groupby takes:  0.914537191391 for nrows= 1000000                                                                                                                                            
second groupby takes: 17.5580070019                                                                                                                                                                         
Third groupby takes 59.3013348579                                                                                                                                                                           

有没有办法使其更快?我应该避免像熊猫这样的问题吗?

为什么最大的东西这么慢?我期望它会更快。

1 个答案:

答案 0 :(得分:4)

您可以使用head

Your solution 1

%timeit dfg.groupby(dfg.index.get_level_values(0),group_keys=False,sort=False).apply(lambda x: x.sort_values('Value', ascending=False).iloc[:max_entries])    
1 loop, best of 3: 9.1 s per loop

My solution

%timeit dfg.sort_values('Value',ascending=False).groupby(level=0).head(max_entries)
1 loop, best of 3: 201 ms per loop