Pandas merge DataFrames based on index / column combination

I have two DataFrames that I want to merge . I read about merging multiple columns and keeping the index on merge . My problem should satisfy both, and it’s hard for me to figure out how to do this.

The first DataFrame looks like this:

enter image description here

and the second looks like

enter image description here

I want to combine them based on Date and ID . In the first, a DataFrame Date is an index, and ID is a column; in the second DataFrame, both Date and ID are part of MultiIndex .

Essentially, as a result, I want the DataFrame to look like a DataFrame 2 with an extra column for Events from DataFrame 1.

+5
source share
1 answer

I suggest restarting the index ( reset_index ) and then merging the DataFrame as you read. Then you can set the index ( set_index ) to play the required MultiIndex.

+1
source

All Articles