Skip to content

Allow to specify column names other than 'level_*' in reset_index() #22260

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
AKuederle opened this issue Aug 9, 2018 · 8 comments
Closed

Allow to specify column names other than 'level_*' in reset_index() #22260

AKuederle opened this issue Aug 9, 2018 · 8 comments
Labels
Enhancement Indexing Related to indexing on series/frames, not to indexes themselves MultiIndex

Comments

@AKuederle
Copy link

Code Sample, a copy-pastable example if possible

# Instead of
new_df = old_df.reset_index()
new_df = new_df.rename({'level_0': 'my_name', 'level_1': 'my_other_name'})

# it should be possible to do
new_df = old_df.reset_index(index_cols = ['my_name', 'my_other_name'])

Problem description

I often find myself renaming columns after resting the index. I think this could be easily done in one step (see above).

If there is interest in that, I could try to implement it for Series and DataFrame

@WillAyd
Copy link
Member

WillAyd commented Aug 9, 2018

reset_index is just going to take whatever names you have for your MultiIndex levels. For example:

In [12]: mi = pd.MultiIndex.from_tuples((('a', 1), ('b', 2)), names=['foo', 'bar'])
In [12]: df = pd.DataFrame(index=mi)   
In [12]: df.reset_index()  
Out[12]: 
  foo  bar
0   a    1
1   b    2

You would be much better served having those names in place prior to the reset.

@gfyoung gfyoung added MultiIndex Indexing Related to indexing on series/frames, not to indexes themselves Usage Question Enhancement and removed Usage Question labels Aug 9, 2018
@gfyoung
Copy link
Member

gfyoung commented Aug 9, 2018

I'm ambivalent about this. It's not a super huge ask for this function, but whether it fits semantically into the scope of this function is unclear.

cc @jreback

@AKuederle
Copy link
Author

I personally find it strange, that the function forces defaults name on you without allowing you to change them. I think if we find a proper name for the additional parameter, it would make a nice small improvement

@jreback
Copy link
Contributor

jreback commented Aug 10, 2018

@AKuederle
Copy link
Author

No col_fill only allows to specify the names of the other levels, if your colums are a Multiindex. However, the actual name of the new index column is not influenced and is set to the default values, if the original index did not have names.

@WillAyd
Copy link
Member

WillAyd commented Aug 10, 2018

I am -1 on a new keyword. Names can easily be set before so I don't see this as adding a lot of value

@WillAyd
Copy link
Member

WillAyd commented Feb 14, 2019

Thanks for the suggestion! However based on all previous core dev responses I think we can close this one

@WillAyd WillAyd closed this as completed Feb 14, 2019
@jorisvandenbossche
Copy link
Member

I personally find this a regular annoyance with reset_index that you have to a rename afterwards. I have actually an old open issue about this: #6878 (so this issue is also kind of a duplicate)

@jorisvandenbossche jorisvandenbossche added this to the No action milestone Feb 14, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Enhancement Indexing Related to indexing on series/frames, not to indexes themselves MultiIndex
Projects
None yet
Development

No branches or pull requests

5 participants