Skip to content

method df.merge is resetting the indexΒ #51796

@aviadr1

Description

@aviadr1

method df.merge will throw away / reset the index of df. I dont think this is desired result.

here's an example:

df = pd.DataFrame({'key' : [1,2,3]}, index=['very','important','index'])
df2 = pd.DataFrame({'key' : [3,2,1], 'data':['c','b','a']}, index=[100,200,300])
df.merge(df2, on='key')

the result will lose the very important index in df and instead will have a generic 0 1 2 index.

   key data
0    1    a
1    2    b
2    3    c

here is a method based on merge that will preseve the original index (which should be the intended result IMHO)

def merge_with_perfect_index(df, df2, on, how):
    original_index_name = df.index.name
    TEMP_INDEX = 'temp_index_aviad'
    return df.rename_axis(TEMP_INDEX).reset_index().merge(df2, on=on, how=how).set_index(TEMP_INDEX).rename_axis(original_index_name)

merge_with_perfect_index(df, df2, on='key', how='left')

which outputs

                    key  data
very           1      a
important      2      b
index          3      c

tested on 1.5.3

Metadata

Metadata

Assignees

No one assigned

    Labels

    ReshapingConcat, Merge/Join, Stack/Unstack, Explode

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions