Check if duplicate columns are created and then remove if any

PHOTO EMBED

Sun Sep 18 2022 12:43:00 GMT+0000 (Coordinated Universal Time)

Saved by @hquizzagan #python

# Raise warning if duplicate is detected
df_combined = pd.concat([df1, df2], axis=1) \
								.set_flags(allow_duplicate_labels=False)

# Check if duplicates exist then remove
df_combined = df.loc[:. ~df_combined.columns.duplicated()].copy()
content_copyCOPY

Usually, duplicate columns are results of un-monitored merging or concatenation of dataframes. They can be a headache during debugging.