Columns to row in python
<p>Do we have sas equivalent of proc transpose. i have the following variables</p>

<pre><code> a b c d e f g
 1 3 4 5 3 2 2
 2 3 3 3 2 4 2
</code></pre>

<p>i want to transpose, across columns a and b , dummy out put below:</p>

<pre><code> a b tranposed_columns transposed_value
 1 3 c                 4
 1 3 d                 5
 1 3 e                 3
 1 3 f                 2
 1 3 g                 2
 2 3 c                 3
 2 3 d                 3
 2 3 e                 2
 2 3 f                 4
 2 3 g                 2
</code></pre>

<p>How can i do this in python, in sas i would have written</p>

<pre><code> proc transpose data=Dummy_data 
 out=ouput_data (rename=(col1=transposed_value _name_=tranposed_columns));
 by a b;
 run;
</code></pre>

<p>i tried dummy_data.T , but it tranposes all the data, also tried hands with pivot data, but its not repeating the rows.</p>

<pre><code>dummy_data.pivot_table(index=['a','b'],values=['c','d','e','f','g'], aggfunc='sum', fill_value=0)
</code></pre>

<p>Use <a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.melt.html" rel="nofollow"><code>pd.melt</code></a> to coalesce columns into a single column. Use the <code>id_vars</code> parameter to "protect" certain columns from participating in the melt:</p>

<pre><code>In [11]: pd.melt(df, id_vars=['a','b'], var_name='transposed_columns', value_name='transposed_value')
Out[11]: 
   a  b transposed_columns  transposed_value
0  1  3                  c                 4
1  2  3                  c                 3
2  1  3                  d                 5
3  2  3                  d                 3
4  1  3                  e                 3
5  2  3                  e                 2
6  1  3                  f                 2
7  2  3                  f                 4
8  1  3                  g                 2
9  2  3                  g                 2
</code></pre>

python pandas python-3.4 transpose