Transpose

Transpose#

.transpose() is another useful wrangling function, transposing rows/columns in a DataFrame.

We can also access the .transpose() function using the T property.

We’ll start with a small DataFrame.

import numpy as np, pandas as pd # import statements

# create df
df = pd.DataFrame(data={'c1': [2, 3], 'c2': [4, 5]})

df # show output

Example #1#

df.transpose() # transpose example
df.T # alternate syntax
dfTransposed = df.transpose() # create new df
dfTransposed # show new df

Example #2#

Another .transpose() example, using a DataFrame with different data types.

import numpy as np, pandas as pd # import statements

# create df
df = pd.DataFrame(data={'name': ['Jimmy', 'Monty'],
      'score': [10.5, 9],
      'employed': [False, True],
      'kids': [0, 0]})

df # show output
# transpose syntax
df.transpose()

# alternate syntax
# df.T

# create new df
# dfTransposed2 = df.transpose()