Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I transpose dataframe in pandas without index?

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?

like image 277
user2237511 Avatar asked Feb 22 '17 02:02

user2237511


People also ask

Can I have a DataFrame without index?

To Print DataFrame Without Index By Making Index emptyYou can set the index as empty for each row, you can do this by creating an array with the empty string (one for each row in the DataFrame). and assign this to the DataFrame.

How do you transpose columns in a data frame?

Pandas DataFrame: transpose() functionThe transpose() function is used to transpose index and columns. Reflect the DataFrame over its main diagonal by writing rows as columns and vice-versa. If True, the underlying data is copied. Otherwise (default), no copy is made if possible.

How do I export a Pandas DataFrame to CSV without index?

pandas DataFrame to CSV with no index can be done by using index=False param of to_csv() method. With this, you can specify ignore index while writing/exporting DataFrame to CSV file.


2 Answers

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
like image 81
dimab0 Avatar answered Oct 07 '22 18:10

dimab0


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
like image 30
Tom Lynch Avatar answered Oct 07 '22 17:10

Tom Lynch