Home > other >  how i can change dataframe and remove duplicate cell
how i can change dataframe and remove duplicate cell

Time:10-03

I have dataferame like this:

DF before

I want change it to this:

DF after

CodePudding user response:

here is one way to do it

An MRE would have helped shared the result with this answer

#Mask the value with empty string when value for matches previous row
df['Model']=df['Model'].mask(df['Model'].eq(df['Model'].shift(1)),'' )
df

CodePudding user response:

You can use df.groupby with the group_keys = True.

df.groupby("Model", group_keys=True).apply(lambda x: x).drop('Model',axis=1)

                tip   segment    pd    gear
Model                                      
Mazda        0    3  Japanese  2020    auto
             1    2  Japanese  2016  manual
             2    3  Japanese  2020    auto
Toyota Camry 3  glx  Japanese  2019  manual
             4  gli  Japanese  2018  manual
  • Related