0

I have this pandas dataframe, I am trying to assign status identification numbers at Status column to each user depending whether the LAST_ACCESSED column for the user (AD_USER_NAME) is [Offline, = 0, Available/Online = 1, Away = 2] [Table before][1] So that the resulting table will be like this [Table After][2] [1]: https://i.stack.imgur.com/3I5BA.png [2]: https://i.stack.imgur.com/bXuTJ.png I tried to use this code for the 'If function' loop but it didn't work

def flag(df1):
    
    if (df1['LAST_ACCESSED'] = df1['None'] and df1['LAST_ACCESSED'] = 0]):
        return 0
    elif (df1['LAST_ACCESSED'] > 0 and (df1['LAST_ACCESSED'] = df1['Run_date_Time']):
        return 1
    elif (df1['LAST_ACCESSED'] > 5 and df1['LAST_ACCESSED'] < df1['Run_date_Time']):
        return 2

df1['Status'] = df1.apply(flag, axis = 1)
wwii
  • 23,232
  • 7
  • 37
  • 77
  • Can you add Sample Data? – Irfanuddin Jul 12 '22 at 23:02
  • `(df1['LAST_ACCESSED'] = df1['None']` **-->** `(df1['LAST_ACCESSED'] == df1['None']` – wwii Jul 13 '22 at 00:19
  • Please don't post images of code, data, or Tracebacks. Copy and paste it as text then format it as code (select it and type `ctrl-k`) … [Why should I not upload images of code/data/errors when asking a question?](https://meta.stackoverflow.com/questions/285551/why-should-i-not-upload-images-of-code-data-errors-when-asking-a-question) – wwii Jul 13 '22 at 00:21
  • [How to make good reproducible pandas examples](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) – wwii Jul 13 '22 at 00:21

1 Answers1

0

Make a boolean array for each condition then use them when assigning values. Use & instead of and. Use == for comparison, = is used for assignments.

zero_mask = (df1['LAST_ACCESSED'] == 0) & (df1['LAST_ACCESSED'] == df1['None'])
one_mask = (df1['LAST_ACCESSED'] > 0) & (df1['LAST_ACCESSED'] == df1['Run_date_Time'])
two_mask = (df1['LAST_ACCESSED'] > 5) & (df1['LAST_ACCESSED'] < df1['Run_date_Time'])

df1.loc[zero_mask,'Status'] = 0
df1.loc[one_mask,'Status'] = 1
df1.loc[two_mask,'Status'] = 2
wwii
  • 23,232
  • 7
  • 37
  • 77