Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

method df.merge is resetting the index #51796

Copy link
Copy link
Open
@aviadr1

Description

@aviadr1
Issue body actions

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      Morty Proxy This is a proxified and sanitized view of the page, visit original site.