2016-09-28 72 views
2

我有一個獨特的問題,我主要希望找到加快此代碼的一些方法。我有一組存儲在數據幀的字符串,每個中有好幾個名字,我知道名字的數量這一步之前,像這樣:快速搜索許多字符串的許多字典鍵

print df 

description      num_people  people  
'Harry ran with sally'    2    []   
'Joe was swinging with sally'   2    [] 
'Lola Dances alone'     1    [] 

我使用的是字典的鍵,我我期待在描述發現,像這樣:

my_dict={'Harry':'1283','Joe':'1828','Sally':'1298', 'Cupid':'1982'} 

,然後使用iterrows搜索像這樣每個字符串匹配項:

for index, row in df.iterrows(): 
    row.people=[key for key in my_dict if re.findall(key,row.desciption)] 

和運行時它結束了

print df 

description      num_people  people  
'Harry ran with sally'    2    ['Harry','Sally']   
'Joe was swinging with sally'   2    ['Joe','Sally'] 
'Lola Dances alone'     1    ['Lola'] 

我看到的問題是,該代碼仍然相當慢,以完成工作,並且我有大量的說明和1000鍵。執行此操作有沒有更快的方式,例如可能使用找到的人數?

回答

2

更快溶液:

#strip ' in start and end of text, create lists from words 
splited = df.description.str.strip("'").str.split() 
#filtering 
df['people'] = splited.apply(lambda x: [i for i in x if i in my_dict.keys()]) 
print (df) 
        description num_people   people 
0   'Harry ran with Sally'   2 [Harry, Sally] 
1 'Joe was swinging with Sally'   2 [Joe, Sally] 
2   'Lola Dances alone'   1   [Lola] 

時序

#[30000 rows x 3 columns] 
In [198]: %timeit (orig(my_dict, df)) 
1 loop, best of 3: 3.63 s per loop 

In [199]: %timeit (new(my_dict, df1)) 
10 loops, best of 3: 78.2 ms per loop 
df['people'] = [[],[],[]] 
df = pd.concat([df]*10000).reset_index(drop=True) 
df1 = df.copy() 

my_dict={'Harry':'1283','Joe':'1828','Sally':'1298', 'Lola':'1982'} 

def orig(my_dict, df): 
    for index, row in df.iterrows(): 
     df.at[index, 'people']=[key for key in my_dict if re.findall(key,row.description)] 
    return (df) 


def new(my_dict, df): 
    df.description = df.description.str.strip("'") 
    splited = df.description.str.split() 
    df.people = splited.apply(lambda x: [i for i in x if i in my_dict.keys()]) 
    return (df) 


print (orig(my_dict, df)) 
print (new(my_dict, df1))