How to "melt" `pandas.DataFrame` objects in 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 without 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="https://i.stack.imgur.com/cDNbN.png" rel="nofollow noreferrer"><img src="https://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="https://i.stack.imgur.com/JJbep.png" rel="nofollow noreferrer"><img src="https://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="https://i.stack.imgur.com/l5rEY.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/l5rEY.png" alt="enter image description here"></a></p>

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