0
id       date         idx   comments
 1       01-05-2018    0      null
 2       02-05-2018    0      null
 3       03-05-2018    Y      null
 4       04-05-2018    Y      null 

when idx = 0, comments column needs to be updated as 'flow reported as null for id (mention the respective id) and date (mention the respective date)'

sammywemmy
  • 27,093
  • 4
  • 17
  • 31
  • Please be clear on the question, add dummy data and write what the output you want would look like. – prp Feb 24 '20 at 11:27

1 Answers1

0

Using list comprehension with zip and f-strings:

df['comments'] = [f'flow reported as null for id {i} and date {d}' if idx == '0' else 'NULL' 
                  for i, d, idx in zip(df['id'], df['date'], df['idx'])]

   id        date idx                                           comments
0   1  01-05-2018   0  flow reported as null for id 1 and date 01-05-...
1   2  02-05-2018   0  flow reported as null for id 2 and date 02-05-...
2   3  03-05-2018   Y                                               NULL
3   4  04-05-2018   Y                                               NULL

Or using apply:

df['comments'] = (
    df.apply(lambda x: f'flow reported as null for id {x["id"]} and date {x["date"]}' 
             if x['idx'] == '0' else 'NULL', axis=1)
)
Erfan
  • 40,971
  • 8
  • 66
  • 78