lundi 11 janvier 2021

Aggregating rows with same id number and inputting column value based on aggregation

df = pandas.DataFrame( { 
    "ID" : ["1123", "2325", "9788", "1123", "9788" , "5421"] , 
    "Type" : ["Red", "Black", "Black", "Black", "Red", "Black"] } )

The df should look like this - note that ID number 1123 and 9788 have both Black and Red 'Type'

     ID     Type
0   1123     Red
1   2323    Black
2   9788    Black
3   1123    Black
4   9788     Red
5   5421    Black

I would like to write some code that aggregates rows. If the ID number has both Black and Red 'Type' I would like it to read Red otherwise Black as is shown below.

     ID     Type
0   1123    Red
1   2323    Black
2   9788    Red
3   5421    Black

Aucun commentaire:

Enregistrer un commentaire