KeyError: 'cross' when trying to do cartesian product with merge()

Viewed 705

I have this line of code:

all_task_food_rescuer_combinations = task_data.merge(food_rescuer_data, how='cross')

It is giving me this error message:

KeyError: 'cross'

I don't have any columns named 'cross'. Does anyone know what could be causing this error?

Update: I swapped that line of code with these lines:

task_data['key'] = 1
food_rescuer_data['key'] = 1
all_task_food_rescuer_combinations = pd.merge(task_data, food_rescuer_data, on ='key').drop("key", 1)

I don't have the error now, but I'm still curious why my first approach didn't work. That line of code is straight from the documentation.

1 Answers

You are passing 'cross' to how= but it is New in version 1.2.0 so consider updating your pandas to a newer version.

Otherwise you can create an artificial column so that every row would have the same value in both dataframes:

left = task_data.assign(key=1)
right = food_rescuer_data(key=1)
left.merge(right, on='key').drop('key', 1)
Related