Python 3 pandas Marking data in dataframes using strings vs Regex
Question: So I have 2 ways of doing the same thing and was wondering which one is more
efficient:

The first way loads a list from a text file or array and marks up a dataframe
using the list:

    
    
    import pandas as pd
    
    ban_list = ['Al Gore', 'Kim jong-un','Kim jong un','Kim Jong Un', 'Al Sharpton','Kim jong il', 'Richard Johnson', 'Dick Johnson']
    
    df=pd.DataFrame({'Users': [ 'Al Gore', 'Kim jong il', 'Kim jong un', 'Al Sharpton', 'James', 'Richard Johnson', 'Bill Gates', 'Alf pig', 'Dick Johnson', 'Python Monte'],
                     'Time': ['D','D','N','D','L','N', 'N','L','L','N']})
    
    df['Banned'] = ''
    
    
    for i in range(len(ban_list)):
        df.loc[df.Users.str.contains(ban_list[i]) & (df.Banned == ''),'Banned'] = 'Yes'
    

The second way uses regex patterns instead of a list of names

    
    
    import pandas as pd
    
    ban_list = ['^(?i)Al(\s)(Gore|Sharpton)$', '^(?i)Kim\sjong(\s|-)(il|un)$', '^(?i)(Dick|Richard)\sJohnson$']
    
    df=pd.DataFrame({'Users': [ 'Al Gore', 'Kim jong il', 'Kim jong un', 'Al Sharpton', 'James', 'Richard Johnson', 'Bill Gates', 'Alf pig', 'Dick Johnson', 'Python Monte'],
                     'Time': ['D','D','N','D','L','N', 'N','L','L','N']})
    
    df['Banned'] = ''
    
    
    for i in range(len(ban_list)):
        df.loc[df.Users.str.contains(ban_list[i]) & (df.Banned == ''),'Banned'] = 'Yes'
    

Both set of code works and does about the same thing. So far the problems are
the first one can is not case sensitive and the second one has a warning
`UserWarning: This pattern has match groups. To actually get the groups, use
str.extract. " groups, use str.extract.", UserWarning)`

The array in the first way loads a large list and the second way has regex
with multiple steps. Which one should I use for efficiency? or are there other
ways to improve this?


Answer: Seems a little cleaner (at least to me) to use `isin` since you have a nice
list of the banned users (you can then map the True/False to Yes/'':

    
    
    df['Banned'] = df.Users.isin(ban_list).map({True:'Yes',False:''})
    print df
    
      Time            Users Banned
    0    D          Al Gore    Yes
    1    D      Kim jong il    Yes
    2    N      Kim jong un    Yes
    3    D      Al Sharpton    Yes
    4    L            James       
    5    N  Richard Johnson    Yes
    6    N       Bill Gates       
    7    L          Alf pig       
    8    L     Dick Johnson    Yes
    9    N     Python Monte       
    

Of course, if True/False is good enough you can just to the first part of the
command:

    
    
    df['Banned'] = df.Users.isin(ban_list)
    print df
    
      Time            Users Banned
    0    D          Al Gore   True
    1    D      Kim jong il   True
    2    N      Kim jong un   True
    3    D      Al Sharpton   True
    4    L            James  False
    5    N  Richard Johnson   True
    6    N       Bill Gates  False
    7    L          Alf pig  False
    8    L     Dick Johnson   True
    9    N     Python Monte  False
    

**Edit** : If you had a second list I would do it as follows:

    
    
    Adminlist = ['Bill Gates']
    df['Banned'] = (df.Users.isin(ban_list).map({True:'Yes',False:''}) +
                    df.Users.isin(Adminlist).map({True:'Admin',False:''}))
    print df
    
      Time            Users Banned
    0    D          Al Gore    Yes
    1    D      Kim jong il    Yes
    2    N      Kim jong un    Yes
    3    D      Al Sharpton    Yes
    4    L            James       
    5    N  Richard Johnson    Yes
    6    N       Bill Gates  Admin
    7    L          Alf pig       
    8    L     Dick Johnson    Yes
    9    N     Python Monte       
    

