pandas transpose dataframe code example

Example 1: dataframe index column invert

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

Example 2: python pandas read csv transpose example

import pandas as pd

df = pd.read_csv('data/src/sample_pandas_normal.csv').head(3)
print(df)
#       name  age state  point
# 0    Alice   24    NY     64
# 1      Bob   42    CA     92
# 2  Charlie   18    CA     70

Example 3: pandas dataframe to change data to horizontally

| id | name | country01 | sort01 | country02 | sort02 | country03 | sort03 |
| 1 | Foo | USA | 1 | Japan | 2 | China | 3 |
| 2 | Bar | USA | 1| UK | 3 | France | 4 |
| 3 | Zap | Japan | 2 | UK | 3 | Russia | 5 |