Can try like this.
I think there most be a better way than duplicate which make i kind of messy.
I think there most be a better way than duplicate which make i kind of messy.
import pandas as pd pd.set_option('display.expand_frame_repr', False) import re def expand_duplicates(df): expanded_rows = [] pattern = re.compile(r'^(.*?)(\d+)(.*?)$') for key_value in df.iloc[:, 0].unique(): group = df[df.iloc[:, 0] == key_value] expanded_row = {df.columns[0]: key_value} group_counter = 1 for _, row in group.iterrows(): for col in df.columns[1:]: match = pattern.match(col) if match: col_prefix, _, col_suffix = match.groups() col_key = f"{col_prefix}{group_counter}{col_suffix}" expanded_row[col_key] = row[col] group_counter += 1 expanded_rows.append(expanded_row) return pd.DataFrame(expanded_rows) input_path = 'input.csv' output_path = 'output.csv' input_df = pd.read_csv(input_path, sep='\t') expanded_df = expand_duplicates(input_df) #print(expanded_df) expanded_df.to_csv(output_path, index=False, sep='\t')