how to convert header row into new columns in python pandas?
<p>I am having following dataframe:</p>

<pre><code>A,B,C
1,2,3
</code></pre>

<p>I have to convert above dataframe like following format:</p>

<pre><code>cols,vals
A,1
B,2
c,3
</code></pre>

<p>How to create column names as a new column in pandas?</p>

<p>You can transpose by <a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.T.html" rel="nofollow"><code>T</code></a>:</p>

<pre><code>import pandas as pd

df = pd.DataFrame({'A': {0: 1}, 'C': {0: 3}, 'B': {0: 2}})

print (df)
   A  B  C
0  1  2  3

print (df.T)
   0
A  1
B  2
C  3

df1 = df.T.reset_index()
df1.columns = ['cols','vals']
print (df1)
  cols  vals
0    A     1
1    B     2
2    C     3
</code></pre>

<p>If <code>DataFrame</code> has more rows, you can use:</p>

<pre><code>import pandas as pd

df = pd.DataFrame({'A': {0: 1, 1: 9, 2: 1}, 
                  'C': {0: 3, 1: 6, 2: 7}, 
                  'B': {0: 2, 1: 4, 2: 8}})

print (df)
   A  B  C
0  1  2  3
1  9  4  6
2  1  8  7

df.index = 'vals' + df.index.astype(str)

print (df.T)
   vals0  vals1  vals2
A      1      9      1
B      2      4      8
C      3      6      7

df1 = df.T.reset_index().rename(columns={'index':'cols'})
print (df1)

  cols  vals0  vals1  vals2
0    A      1      9      1
1    B      2      4      8
2    C      3      6      7
</code></pre>

python python-2.7 pandas dataframe transpose