How to "melt" `pandas.DataFrame` objects? (Python 3)
<p>I'm trying to <code>melt</code> certain columns of a <code>pd.DataFrame</code> while preserving columns of the other.  In this case, I want to <code>melt</code> <code>sine</code> and <code>cosine</code> columns into <code>values</code> and then which column they came from (i.e. <code>sine</code> or <code>cosine</code>) into a new columns entitled <code>data_type</code> then preserving the original <code>desc</code> column.</p>

<p><strong>How can I use <code>pd.melt</code> to achieve this w/o melting and concatenating each component manually?</strong> </p>

<pre><code># Data
a = np.linspace(0,2*np.pi,100)
DF_data = pd.DataFrame([a, np.sin(np.pi*a), np.cos(np.pi*a)], index=["t", "sine", "cosine"], columns=["t_%d"%_ for _ in range(100)]).T
DF_data["desc"] = ["info about this" for _ in DF_data.index]
</code></pre>

<p><a href="http://i.stack.imgur.com/cDNbN.png" rel="nofollow"><img src="http://i.stack.imgur.com/cDNbN.png" alt="enter image description here"></a></p>

<p>The round about way I did it:</p>

<pre><code># Melt each part
DF_melt_A = pd.DataFrame([DF_data["t"],
                          DF_data["sine"],
                          pd.Series(DF_data.shape[0]*["sine"], index=DF_data.index, name="data_type"), 
                          DF_data["desc"]]).T.reset_index()
DF_melt_A.columns = ["idx","t","values","data_type","desc"]
DF_melt_B = pd.DataFrame([DF_data["t"],
                          DF_data["cosine"],
                          pd.Series(DF_data.shape[0]*["cosine"], index=DF_data.index, name="data_type"),
                          DF_data["desc"]]).T.reset_index()
DF_melt_B.columns = ["idx","t","values","data_type","desc"]

# Merge
pd.concat([DF_melt_A, DF_melt_B], axis=0, ignore_index=True)
</code></pre>

<p><a href="http://i.stack.imgur.com/JJbep.png" rel="nofollow"><img src="http://i.stack.imgur.com/JJbep.png" alt="enter image description here"></a></p>

<p>If I do <code>pd.melt(DF_data</code> I get a complete meltdown</p>

<p><a href="http://i.stack.imgur.com/l5rEY.png" rel="nofollow"><img src="http://i.stack.imgur.com/l5rEY.png" alt="enter image description here"></a></p>

<p>In response to the comments:
<a href="http://i.stack.imgur.com/y1YCp.png" rel="nofollow"><img src="http://i.stack.imgur.com/y1YCp.png" alt="enter image description here"></a></p>

<p>allright so I had to create a similar df because I did not have access to your <code>a</code> variable. I change your <code>a</code> variable for a list from 0 to 99... so t will be 0 to 99</p>

<p>you could do this :</p>

<pre><code>a = range(0, 100)
DF_data = pd.DataFrame([a, [np.sin(x)for x in a], [np.cos(x)for x in a]], index=["t", "sine", "cosine"], columns=["t_%d"%_ for _ in range(100)]).T
DF_data["desc"] = ["info about this" for _ in DF_data.index]

df = pd.melt(DF_data, id_vars=['t','desc'])
df.head(5)
</code></pre>

<p>this should return what you are looking for.</p>

<pre><code>     t             desc variable     value
0  0.0  info about this     sine  0.000000
1  1.0  info about this     sine  0.841471
2  2.0  info about this     sine  0.909297
3  3.0  info about this     sine  0.141120
4  4.0  info about this     sine -0.756802
</code></pre>

python pandas matrix dataframe melt