Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas merge with MultiIndex for repeated columns

I've got two DataFrames, containing the same information (length, width) about different aspects (left foot, right foot) of the same objects (people).

import pandas as pd

left_feet = pd.DataFrame(
    data={
        "Length": [20, 30, 25],
        "Width": [8, 10, 9]},
    index=[0, 1, 2])

right_feet = pd.DataFrame(
    data={
        "Length": [24, 30],
        "Width": [8, 10]},
    index=[2, 1])

print(left_feet)
   Length  Width
0      20      8
1      30     10
2      25      9

print(right_feet)
   Length  Width
2      24      8
1      30     10

I want to merge these into a single DataFrame, so I do this:

feet = pd.merge(left_feet, right_feet,
         left_index=True, right_index=True,
         suffixes=["_left", "_right"])

print(feet)
   Length_left  Width_left  Length_right  Width_right
1           30          10            30           10
2           25           9            24            8

Working with suffixes is cumbersome however. I would instead like the columns to be a MultiIndex, where the first level contains "left" and "right" and the second level contains "length" and "width".

What's the best way to do this?

Note: similar questions have been asked about concatenating aligned DataFrames, but this question is about a join (i.e. "merge") operation; the rows are not necessarily aligned and there may not always be corresponding rows.

like image 834
Denziloe Avatar asked Aug 10 '20 12:08

Denziloe


People also ask

How avoid duplicates in pandas merge?

merge() function to join the left dataframe with the unique column dataframe using 'inner' join. This will ensure that no columns are duplicated in the merged dataset.

Can I merge DataFrame with series?

merge() can be used for all database join operations between DataFrame or named series objects. You have to pass an extra parameter “name” to the series in this case. For instance, pd. merge(S1, S2, right_index=True, left_index=True) .

Can you merge multiple DFS in pandas?

We can use either pandas. merge() or DataFrame. merge() to merge multiple Dataframes. Merging multiple Dataframes is similar to SQL join and supports different types of join inner , left , right , outer , cross .


Video Answer


1 Answers

Try concat, with keys parameter and join='inner':

print(pd.concat([left_feet, right_feet], axis=1, keys=['Left','Right'], join='inner'))
    Left        Right      
  Length Width Length Width
1     30    10     30    10
2     25     9     24     8
like image 184
Ben.T Avatar answered Sep 18 '22 01:09

Ben.T