-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
Index gets lost when DataFrame melt method is used #17440
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
Comments
Thanks @NiklasKeck. I thought about proposing this a while back, but never wrote up an issue. I never worked out the correct way to handle the interaction between the existing index and the In [34]: df.reset_index().melt(id_vars=['Letters']).set_index(['Letters', 'variable'])
Out[34]:
value
Letters variable
A Numbers_1 0
B Numbers_1 1
C Numbers_1 2
A Numbers_2 3
B Numbers_2 4
C Numbers_2 5 but that may not be true in general. Anyway, I think this would be a useful addition (as an option keyword, to preserve backwards compatibility) |
Thanks @TomAugspurger for the rapid and nice response. Your idea to use the original column names (variable) for the additional level is more intuitive than the arbitrary melt_id. I think the result should be unique if the original index and column names were unique. I will think about how to implement that. |
Setting keep_index to True will reuse the original DataFrame index + names of melted columns as additional level. closes issue pandas-dev#17440
Setting keep_index to True will reuse the original DataFrame index + names of melted columns as additional level. closes issue pandas-dev#17440
Setting keep_index to True will reuse the original DataFrame index + names of melted columns as additional level. closes issue pandas-dev#17440
I just met this issue too and felt it's such a pity for losing valuable index information. What happened to this feature addition and what can i do to make it happen? |
Looks like #17459. Feel free to
revive it if you're interested in this.
…On Tue, Dec 11, 2018 at 11:39 AM gitgithan ***@***.***> wrote:
I just met this issue too and felt it's such a pity for losing valuable
index information. What happened to this feature addition and what can i do
to make it happen?
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#17440 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/ABQHIl5u5oNC65Kq9JI8Q15M1yV122-Uks5u3-3DgaJpZM4PM6m->
.
|
I'm very interested in this! |
Index gets lost when DataFrame melt method is used
Problem description
When melting a dataframe, I expected the original index to be reused. However, the original index is lost in the melt method. This is probably meant by wesm's comment (# TODO: what about the existing index?)
pandas/pandas/core/reshape/reshape.py
Line 715 in 133a208
Expected Output
I would expect something like
Where Letters and melt_id are two multiindex levels and variable and value are actual columns.
Output of
pd.show_versions()
[paste the output of
pd.show_versions()
here below this line]INSTALLED VERSIONS
commit: d0f62c2816ada96a991f5a624a52c9a4f09617f7
python: 3.6.2.final.0
python-bits: 64
OS: Windows
OS-release: 7
machine: AMD64
processor: Intel64 Family 6 Model 60 Stepping 3, GenuineIntel
byteorder: little
LC_ALL: None
LANG: en
LOCALE: None.None
pandas: 0.21.0.dev+420.gd0f62c2
pytest: 3.2.1
pip: 9.0.1
setuptools: 36.2.2.post20170724
Cython: 0.26
numpy: 1.13.1
scipy: None
pyarrow: None
xarray: None
IPython: 6.1.0
sphinx: 1.6.3
patsy: None
dateutil: 2.6.1
pytz: 2017.2
blosc: None
bottleneck: None
tables: None
numexpr: None
feather: None
matplotlib: None
openpyxl: None
xlrd: None
xlwt: None
xlsxwriter: None
lxml: None
bs4: None
html5lib: 0.9999999
sqlalchemy: None
pymysql: None
psycopg2: None
jinja2: 2.9.6
s3fs: None
fastparquet: None
pandas_gbq: None
pandas_datareader: None
The text was updated successfully, but these errors were encountered: