Skip to content
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

ENH melt uses column name if available #4144

Merged
merged 3 commits into from Jul 6, 2013

Conversation

@hayd
Copy link
Contributor

commented Jul 6, 2013

Currently melt allows you to manually specify the val_name and var_name, this changes the default behaviour to grab the column name if one is available.

In [1]: df = pd.DataFrame({'a': {0: 1.0676829999999999, 1: -1.3214049999999999, 2: -0.80733299999999997}, 'b': {0: -1.110463, 1: 0.36891499999999999, 2: 0.082979999999999998}, 'c': {0: 0.20866999999999999, 1: -1.055342, 2: -0.87336100000000005}})

In [2]: df.columns.name = 'foo'

In [3]: df
Out[3]:
foo         a         b         c
0    1.067683 -1.110463  0.208670
1   -1.321405  0.368915 -1.055342
2   -0.807333  0.082980 -0.873361

In [4]: pd.melt(df)  # before this would have columns ['variable', 'value']
Out[4]:
  foo     value
0   a  1.067683
1   a -1.321405
2   a -0.807333
3   b -1.110463
4   b  0.368915
5   b  0.082980
6   c  0.208670
7   c -1.055342
8   c -0.873361

Also, cleaned up all the tests (and added some more) in test_reshape.
@jreback

This comment has been minimized.

Copy link
Contributor

commented Jul 6, 2013

isn't this already in with #3649, or this is just a cleanup of that?

@jreback

This comment has been minimized.

Copy link
Contributor

commented Jul 6, 2013

oh i c, you name is more 'automatic'...nice!

@jreback

This comment has been minimized.

Copy link
Contributor

commented Jul 6, 2013

@hayd there is a release notes/whatsnew associated with 3649, why don't you add on to it with this change?

@hayd

This comment has been minimized.

Copy link
Contributor Author

commented Jul 6, 2013

I was just pondering whether I needed to bother :) will do

@hayd

This comment has been minimized.

Copy link
Contributor Author

commented Jul 6, 2013

As an aside, wonder if melt should work with multiindexes...

@@ -651,6 +652,9 @@ def melt(frame, id_vars=None, value_vars=None,
else:
frame = frame.copy()

if var_name is None:
var_name = frame.columns.name if frame.columns.name is not None else 'variable'

This comment has been minimized.

Copy link
@cpcloud

cpcloud Jul 6, 2013

Member

u could do frame.columns.name or 'variable' too :)

This comment has been minimized.

Copy link
@hayd

hayd Jul 6, 2013

Author Contributor

...but just in case they had named their column False(y) :)

@hayd

This comment has been minimized.

Copy link
Contributor Author

commented Jul 6, 2013

merge? (Have another WIP for MultiIndexes)

@jreback

This comment has been minimized.

Copy link
Contributor

commented Jul 6, 2013

look good

hayd added a commit that referenced this pull request Jul 6, 2013
Merge pull request #4144 from hayd/melt_name
ENH melt uses column name if available

@hayd hayd merged commit 0378de5 into pandas-dev:master Jul 6, 2013

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
3 participants
You can’t perform that action at this time.