Search code examples
pythonpandas

Check if Series has Values in Range


I have a Pandas dataframe that has user information and also has a column for their permissions:

UserName    Permissions
John Doe             02
John Doe             11
 Example             09
 Example             08
   User3             11

I am trying to create a new column called User Class that is based on their Permissions (looking at all of the users permissions). If a user has all permissions <10, they are considered Admin. If a user has all permission >=10, they are considered User. However if they have permissions that are both <10 and >=10, then they will be coded as Admin/User. So my resulting output would be:

UserName    Permissions    User Class
John Doe             02    Admin/User
John Doe             11    Admin/User
 Example             09         Admin
 Example             08         Admin
   User3             11          User

What would be the best way to do this? My original idea was to do:

for UserName, User_df in df.groupby(by='UserName'):
    LT10 = (User_df['Permissions'] < 10).any()
    GTE10 = (User_df['Permissions'] >= 10).any()
    if (LT10 & GTE10):
        UserClass = 'Admin/User'
    elif LT10:
        UserClass = 'Admin'
    elif GTE10:
        UserClass = 'User'
    df.at[User_df.index, 'User Class'] = UserClass

However these seems very inefficient because df has ~800K records


Solution

  • Another possible solution:

    df['User Class'] = (
        df.groupby('UserName')['Permissions']
        .transform(lambda x: 'Admin' if (x < 10).all() else 
                   'User' if (x >= 10).all() else 'Admin/User'))
    

    Output:

       UserName  Permissions  User Class
    0  John Doe            2  Admin/User
    1  John Doe           11  Admin/User
    2   Example            9       Admin
    3   Example            8       Admin
    4     User3           11        User