assign values with several condition. Best practices

Viewed 29

My input, after outer join two tables on taskname df:

taskname    taskid_x    projectid_x taskid_y    projectid_y user_x  user_y
task1            nan    nan            38             15    nan         nan
task2            12     1              36             15    nan         nan
task3            nan    nan            33             15    nan         nan
task4            nan    nan            34             15    nan         nan
task5            nan    nan            35             15    nan         nan
task6            17     1              nan           nan    nan         nan
task7            31     1              nan           nan    nan         nan

I need to assign values(name of user) in columns user_x and user_y with several conditions: first each user have N task that should be assigned(for example only 2 tasks from df), second both task_x and taskid_y cannot be assigned to same user(for example ['user_x']!= ['user_y']in same taskname). MY output:

taskname    taskid_x    projectid_x taskid_y    projectid_y user_x  user_y
task1        nan             nan       35        15         user2   nan
task2        nan             nan       38        15        user2    user1
task3        nan             nan       33        15         nan     user1
task4        nan             nan       36        15         nan     user1
task5        17              1          nan     nan         user1    nan
task6        12              1         368       15         user1   nan
task7        31              1         nan       nan        user1   nan

That my code:

current_user = 'user1'
factor_to_select_one = 6
factor_to_select_two = 2
factor_to_current = factor_to_select_one
j = 0

for i, (taskid_x, taskid_y) in enumerate(zip(df['taskid_x'], df['taskid_y'])):
    if pd.notnull(taskid_x) & pd.notnull(taskid_y):
        df['user_x'].at[i] = current_user
        j = j + 1
    elif pd.notnull(taskid_x) & pd.isnull(taskid_y):
        df['usesr_x'].at[i] = current_user
        j = j + 1
    elif pd.isnull(taskid_x) & pd.notnull(taskid_y):
        df['user_y'].at[i] = current_user
        j = j + 1
       
    if j >= factor_to_current:
        print(j)
        break
  
if 'user1' in current_user:
    current_annotator = 'user2' 
    j=0
    factor_to_current = factor_to_select_two

df.sort_values(['user_x','user_y'], na_position='first',inplace=True, ignore_index=True)
for i, (user_x, user_y) in enumerate(zip(df['user_x'], df['user_y'])):
    if pd.isnull(user_x) & pd.isnull(user_y):
        df['user_x'].at[i] = current_user
        j = j + 1
    elif pd.notnull(user_x) & pd.isnull(user_y):
        df['user_y'].at[i] = current_user
        j = j + 1
    elif pd.isnull(user_x) & pd.notnull(user_y):
        df['user_x'].at[i] = current_user
        j = j + 1
       
    if j >= factor_to_current:
        print(j)
        break

So I think code is too heavy and not really best practices. Any idea to do less code more efficient?

0 Answers
Related