Home > Software engineering >  Function to merge on specific columns and specific dataframes
Function to merge on specific columns and specific dataframes

Time:10-12

I am trying to create a function to merge two dataframes on specific columns as follows:

list_to_be_merged_on = ['code', 'name', 'section']

def merge_on_specific_columns(table1, table2, list_to_be_merged_on):
     merged_table = pd.merge(table1, table2, on=[list_to_be_merged_on])
     return merged_table


 merged_df = 
 merge_on_specific_columns(file,car,list_to_be_merged_on)

I get the following error:

KeyError: ('code', 'name', 'section')

any suggestions to what might go wrong?

CodePudding user response:

You don't need to put list_to_be_meged_on in a list as it is already a list.
Try "on=list_to_be_merged_on" instead of "on=[list_to_be_merged_on]"

  • Related