I have a pandas DataFrame with a single row:
10 20 30 70
data1: 2.3 5 6 7
I want to reindex the frame so that the column values (10, 20, 30, 70) become index values and the data becomes the column:
data1:
10 2.3
20 5.0
30 6.0
70 7.0
How do I achieve this?
Pandas DataFrame. transpose() is a library function that transpose index and columns. The transpose reflects the DataFrame over its main diagonal by writing rows as columns and vice-versa. Use the T attribute or the transpose() method to swap (= transpose) the rows and columns of DataFrame.
Using reindex() function to Reverse Row Reverse rows of the data frame using reindex() Function. The pandas dataframe. reindex() function concatenates the dataframe to a new index with optional filling logic, placing NA/NaN at locations that have no value in the previous index.
In order to set index to column in pandas DataFrame use reset_index() method. By using this you can also set single, multiple indexes to a column. If you are not aware by default, pandas adds an index to each row of the pandas DataFrame.
You're looking for the transpose (T
) method:
In [11]: df
Out[11]:
10 20 30 70
data1: 2.3 5 6 7
In [12]: df.T
Out[12]:
data1:
10 2.3
20 5.0
30 6.0
70 7.0
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With