Categories
Mastering Development

Group by and update value based on another column using python

I have a data frame with below columns, using which need to create new columns by grouping Name. Status should be updated as active to entire group if it is active for any one St.

data = {'Name':['Tom', 'Tom', 'krish', 'jack','jack','Nam','sue'],
        'St':['S-123', 'S-290', 'S-123', 'S-147','S-98','S-123','S-38'],
        'Status':['Inactive','active','active','inactive','active','Inactive','Inactive']}
  
df = pd.DataFrame(data)

The desired output is

    Name     St    Status New Status
0    Tom  S-123  Inactive     active
1    Tom  S-290    active     active
2  krish  S-123    active     active
3   jack  S-147  inactive     active
4   jack   S-98    active     active
5    Nam  S-123  Inactive   Inactive
6    sue   S-38  Inactive   Inactive

Leave a Reply

Your email address will not be published. Required fields are marked *