How to pivot a dataframe with two columns with no index

Use set_index to move column 'one' into the index, then use T to transpose.

a.set_index('one').T

Output:

one   a   b   c
two  12  32  12

Info:

<class 'pandas.core.frame.DataFrame'>
Index: 1 entries, two to two
Data columns (total 3 columns):
a    1 non-null int64
b    1 non-null int64
c    1 non-null int64
dtypes: int64(3)
memory usage: 28.0+ bytes
None

If this is your input:

a = pd.DataFrame([("a", 12), ("b", 32), ("c", 12)], columns=["one", "two"])
  one  two
0   a   12
1   b   32
2   c   12

Then a.transpose() results in this:

      0   1   2
one   a   b   c
two  12  32  12

Is this what you were looking for?

Tags:

Python

Pandas