How do I transpose dataframe in pandas without index?

PythonPandasDataframe

Python Problem Overview


Pretty sure this is very simple.

I am reading a csv file and have the dataframe:

Attribute    A   B   C
a            1   4   7
b            2   5   8
c            3   6   9

I want to do a transpose to get

Attribute    a   b   c
A            1   2   3
B            4   5   6
C            7   8   9

However, when I do df.T, it results in

             0   1   2 
Attribute    a   b   c
A            1   2   3
B            4   5   6
C            7   8   9`

How do I get rid of the indexes on top?

Python Solutions


Solution 1 - Python

You can set the index to your first column (or in general, the column you want to use as as index) in your dataframe first, then transpose the dataframe. For example if the column you want to use as index is 'Attribute', you can do:

df.set_index('Attribute',inplace=True)
df.transpose()

Or

df.set_index('Attribute').T

Solution 2 - Python

It works for me:

>>> data = {'A': [1, 2, 3], 'B': [4, 5, 6], 'C': [7, 8, 9]}
>>> df = pd.DataFrame(data, index=['a', 'b', 'c'])
>>> df.T
   a  b  c
A  1  2  3
B  4  5  6
C  7  8  9

Solution 3 - Python

If your index column 'Attribute' is really set to index before the transpose, then the top row after the transpose is not the first row, but a title row. if you don't like it, I would first drop the index, then rename them as columns after the transpose.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser2237511View Question on Stackoverflow
Solution 1 - Pythondimab0View Answer on Stackoverflow
Solution 2 - PythonTom LynchView Answer on Stackoverflow
Solution 3 - PythonSam_KimView Answer on Stackoverflow