Merging csv files with different headers with Pandas in Python
From your example, it looks like you need to do some column renaming in addition to the merge
. This is easiest done before the merge
itself.
# Read the csv files
dfA = pd.read_csv("a.csv")
dfB = pd.read_csv("b.csv")
# Rename the columns of b.csv that should match the ones in a.csv
dfB = dfB.rename(columns={'MEASUREMENT': 'HEIGHT', 'COUNTRY': 'LOCATION'})
# Merge on all common columns
df = pd.merge(dfA, dfB, on=list(set(dfA.columns) & set(dfB.columns)), how='outer')
# Only keep the columns that exists in a.csv
df = df[dfA.columns]
# Save to a new csv
df.to_csv("output.csv", index=False)
This should give you what you are after.