Skip to content
Advertisement

Replace duplicate value with NaN using groupby

Dataset(MWE)

JavaScript

I am trying to replace duplicates from columns {people_vaccinated,people_fully_vaccinated,people_vaccinated_per_hundred} with NaN while using groupby() on location. I tried some solution online, but couldn’t get them working for me, so instead used the below logic

JavaScript

The above logic fails when you have consecutive nulls(more than 2). I need to replace duplicates(while keeping the first instance) with NaNs. What is the best way to do this? You can observe from the above snippet that column people_fully_vaccinated has duplicate values

Sample output

JavaScript

Advertisement

Answer

mask + duplicated

We can define a list of column names, then for each column inside a for loop mask the duplicate values per unique location

JavaScript

JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement