Skip to content Skip to sidebar Skip to footer

Duplicate Row For Every Different Entry In One Specific Column

I want to create a duplicate for every row, but only if the row has a new entry in a specific column and on top want to keep some columns empty then. So the dataframe looks like: n

Solution 1:

You can drop duplicate and concat:

pd.concat((df.drop_duplicates(['area','typ']).assign(number='',value=''), df)
         ).sort_index(kind='mergesort')

Output:

  number value area typ
0BA0110BA1220BA2BB2310BB3420BB4530BB

Update: For several columns that needs to be emptied:

cols = ['area','typ']
new_df = df.drop_duplicates(cols)

for col in new_df.columns:
    if col not in cols: new_df[col] = ''

pd.concat((new_df, df)).sort_index(kind='mergesort')

Post a Comment for "Duplicate Row For Every Different Entry In One Specific Column"