Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/342.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python:通过搜索子字符串查找表_Python_Python 2.7_Pandas_Lookup_String Search - Fatal编程技术网

Python:通过搜索子字符串查找表

Python:通过搜索子字符串查找表,python,python-2.7,pandas,lookup,string-search,Python,Python 2.7,Pandas,Lookup,String Search,我有一个数据框,其中有一列用于应用程序用户代理。我需要做的是从本专栏中识别特定的应用程序。比如说, NewWordsWithFriendsFree/2.3 CFNetwork/672.1.15 Darwin/14.0.0将在wordswithfriends中分类 iPhone3,1; iPhone OS 7.1.2; com.fingerarts.sudoku2; 143441-1,24 will be Sudoku by FingerArts etc. 我将有另一个数据帧,其中包含需要匹配的

我有一个数据框,其中有一列用于应用程序用户代理。我需要做的是从本专栏中识别特定的应用程序。比如说,

NewWordsWithFriendsFree/2.3 CFNetwork/672.1.15 Darwin/14.0.0
将在
wordswithfriends
中分类

iPhone3,1; iPhone OS 7.1.2; com.fingerarts.sudoku2; 143441-1,24 will be Sudoku by FingerArts etc.
我将有另一个数据帧,其中包含需要匹配的字符串。比如说,

Keyword                 Game 
NewWordsWithFriends     Words With Friends
com.fingerarts.sudoku   Sudoku by FingerArts
如何对熊猫数据帧执行这样的查找?例如,dataframe如下所示

user    date                 user-agent
 A      2015-09-02 13:45:56  NewWordsWithFriendsFree/2.3 CFNetwork/672.1.15 Darwin/14.0.0
 B      2015-08-31 23:04:21  iPhone3,1; iPhone OS 7.1.2; com.fingerarts.sudoku2; 143441-1,24

在查找之后,我需要一个新的列
GameName

实现这一点的一种可能方法是:

import pandas as pd                                                              

# some example data
qry = pd.DataFrame.from_dict({"Keyword": ["NewWordsWithFriends",                 
                                          "com.fingerarts.sudoku"],              
                              "Game": ["Words With Friends",                     
                                       "Sudoku by FingerArts"]})                 

df = pd.DataFrame.from_dict({"user-agent" : ["NewWordsWithFriendsFree/2.3 CFNetwork/672.1.15 Darwin/14.0.0",     
                                             "iPhone3,1; iPhone OS 7.1.2; com.fingerarts.sudoku2; 143441-1,24"]})

keywords = qry.Keyword.tolist()                                                  
games = qry.Game.tolist()                                                        

def select(x):                                                                   
    for key, game in zip(keywords, games):                                       
        if key in x:                                                             
            return game                                                          

df["GameName"] = df["user-agent"].apply(select)  
这将提供:

In [41]: df
Out[41]: 
                                          user-agent              GameName
0  NewWordsWithFriendsFree/2.3 CFNetwork/672.1.15...    Words With Friends
1  iPhone3,1; iPhone OS 7.1.2; com.fingerarts.sud...  Sudoku by FingerArts
如果需要对大型数据集执行此操作,则需要测试此解决方案的性能,并查看它是否足够快

如果不是,可能会优化字符串的测试方式:

为所有可能的游戏设置一个外部循环,然后使用
。应用
返回每列游戏的结果可能会加快速度,因为这样可以避免在每次调用
select()
等时对所有游戏进行循环

要识别瓶颈,您可以使用
line\u profiler
(请参阅)

get_关键字
函数的另一个实现可能是

def get_keyword(user_agent):
    for keyword in map_df['Keyword']:
        if keyword in user_agent:
            return keyword
获取映射的另一种方法是创建
系列

mapping = pd.Series(map_df['Game'].values , index = map_df.Keyword )

看看什么更快会很有趣
mapping = pd.Series(map_df['Game'].values , index = map_df.Keyword )