How do I swap the duplicates in a row with a blank without affecting the corresponding rows using python?

Viewed 35

Let's say we have the following data on excel,

Column1 | Column2 | Column3 | .... Column n
  A     |   10    |    a
  A     |   10    |    b
  A     |   10    |    c
  B     |   15    |    d
  B     |   15    |    e
  B     |   15    |    f
  C     |   20    |    g
  C     |   20    |    h
  .         
  .
  .

I would like to modify it to,

Column1 | Column2 | Column3 | .... Column n
  A     |   10    |    a
        |         |    b
        |         |    c
  B     |   15    |    d
        |         |    e
        |         |    f
  C     |   20    |    g
        |         |    h
  .         
  .
  .

I tried using the drop_duplicates (from pandas) technique but it deletes other rows too.

I can do the task manually, but I am trying to find a way of achieving the above through using python, any thoughts?

2 Answers

You can first find the indices of the duplicates

dup_index = df.duplicates().index

Then you can replace the values

df.Column1.replace(dup_index,'')

If you don't want blank values as rchurt said in the comment , groupby() can also be a good option if you don't want blanks and let your data to be as it is.

You can use mask with duplicated:

cols = ['Column1', 'Column2']
df[cols] = df[cols].mask(df.duplicated(cols), '')
df

Output:

  Column1 Column2 Column3
0       A      10       a
1                       b
2                       c
3       B      15       d
4                       e
5                       f
6       C      20       g
7                       h
Related