Skip to content
Advertisement

How to remove duplicate values in one column but keep the rows pandas?

I have dataframe as per below Country: China, China, China, United Kingdom, United Kingdom,United Kingdom Country code: CN, CN, CN, UK, UK, UK Port Name: Yantian, Shekou, Quanzhou, Plymouth, Cardiff, Bird port

I want to remove the duplicates in the first two columns, only keep as: Country: China, , , United Kingdom, , Country code: CN, , , UK, , Port Name: Yantian, Shekou, Quanzhou, Plymouth, Cardiff, Bird port

I have tried df.drop_duplicates, but it will drop the whole rows.

Advertisement

Answer

You could use the pd.Series.duplicated method:

JavaScript

prints

index Country Country code Port Name
0 China CN Yantian
1 NaN NaN Shekou
2 NaN NaN Quanzhou
3 United Kingdom UK Plymouth
4 NaN NaN Cardiff
5 NaN NaN Bird port
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement