ENH: Melt with MultiIndex columns
State: closed

No idea if there's actually a market for this, but anyhow:

```
In [1]: df = pd.DataFrame([[ 1.067683, -1.110463,  0.20867 ], [-1.321405,  0.368915, -1.055342], [-0.807333,  0.08298 , -0.873361]])

In [2]: df.columns = [list('ABC'), list('abc')]

In [3]: df.columns.names = ['CAP', 'low']

In [4]: df
Out[4]:
CAP         A         B         C
low         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 [5]: pd.melt(df, col_level=0)
Out[5]:
  CAP     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

In [6]: pd.melt(df,)
Out[6]:
  CAP low     value
0   A   a  1.067683
1   A   a -1.321405
2   A   a -0.807333
3   B   b -1.110463
4   B   b  0.368915
5   B   b  0.082980
6   C   c  0.208670
7   C   c -1.055342
8   C   c -0.873361

In [7]: df.columns.names = [None, None]

In [8]: pd.melt(df,)
Out[8]:
  variable_0 variable_1     value
0          A          a  1.067683
1          A          a -1.321405
2          A          a -0.807333
3          B          b -1.110463
4          B          b  0.368915
5          B          b  0.082980
6          C          c  0.208670
7          C          c -1.055342
8          C          c -0.873361
```

Also includes fix for `get_level_values` and name attribute.

cc #4144


Labels:

Comments:
tbh I'm not 100% if this is the right behaviour... thoughts?

slightly OT: is there a corresponding `freeze` or `unmelt` method?

using `df.set_index` might work i guess, but might be useful to have a wrapper around it? not sure

i guess pivot tables are really unmelting...so maybe not that useful

@cpcloud docstring says:

> "Unpivots" a DataFrame from wide format to long format...

So I guess pivot is the inverse (why it's not just called unpivot I don't know)... weirdly docs create an unpivot function [here](http://pandas.pydata.org/pandas-docs/stable/reshaping.html#reshaping-by-pivoting-dataframe-objects).

No idea at all why it's called melt...

not that I know anything about R, but that's where it comes from......

@jreback that's one step back in it's history, would be nice to know where it originated from... maybe I'll ask on SO, no doubt I'll be downvoted into oblivion.

@hayd I may be in the minority, but I think `melt` is a nicely descriptive name, kinda like an `unstack` that's _so_ intense it melted the whole frame away :tongue:  

or 0.12?

@jreback What do you reckon to this _behaviour_ though? Does it make sense?

@jtratner but where does the name come from? ;(

@hayd i can ask on SO if you want i only have 331 rep so oblivion is a jump, skip and a hop away.

@cpcloud Up to you, I had penned a partial question (but I'd need to research more before posting, so may leave for another day)... I'il upvote if you beat me to it :p

I'm thinking it called it because it's the opposite of "cast" (though no idea if there is a difference between cast and pivot) http://www.statmethods.net/management/reshape.html :s

@hayd it looks ok to me....I don't really use this stuff... @wesm ?

i like how this is in the R docs for cast.

```
Along with ‘melt’ and recast, this is the only function you should
ever need to use.
```

Everything in R can be done with exactly 3 functions.

@hayd I think you're right there's all sorts of talk about "molten" `data.frame`s in the R docs.

@cpcloud personally think it's a rather confusing/strange analogy...

@hayd Well, you know, R has to keep the barrier to entry very high by using obscure analogies

@hayd are you suggesting that we change the name for `melt`?

personally i think `melt` should stay for back compat. could alias `unpivot` to it

I think melt is pretty descriptive...

how are you thinking about it? or do i detect sarcasm over github? i think of it like a "generalized-for-labels" `ravel` which i think is a much better name..

i actually don't use this much but now i'm starting to see where this might be useful in my own work...

I wouldn't be unhappy to see an unpivot alias for melt (or vice versa), but then should cast be an alias for pivot (or pivot_table)? *shudders*

I am -1 on `cast` I think its _very_ confusing, but +1 on `unpivot`

cast is a horrible name

what was `R` thinking?

as confusing as possible, but no more

Well written documentation

```
Basically, you "melt" data so that each row is a unique id-variable combination. 
Then you "cast" the melted data into any shape you would like. Here is a very simple example.

There is much more that you can do with the melt( ) and cast( ) functions. 
See the documentation for more details.
```

"shape-casting" should not be called casting, it should be called reshaping (strangely enough, that's the name of the package containing `cast`) and type casting should be called `cast` much more consistent with the history of every other programming language.

The documentation refers to itself :expressionless: 

Let's deprecate melt (in 0.13 but keep it around) and have unpivot immediately (in 0.12).