Transpose in Python


To transpose is to rotate information from one row or column to another to change the data layout, for the purpose of making observations from a new perspective. A transposition turns the rows to columns and the columns to rows.

See Planets_DataFrame project in Anaconda

import numpy as np
import pandas as pd
from IPython.display import display, HTML

# Define df
data = {'planet': ['Mercury', 'Venus', 'Earth', 'Mars', 'Jupiter', 'Saturn', 'Uranus', 'Neptune'],
        'radius_km': [2440, 6052, 6371, 3390, 69911, 58232, 25362, 24622],
        'moons': [0, 0, 1, 2, 80, 83, 27, 14],
        'distance_AU': [0.39,0.72,1.0,1.52,5.2,9.54,19.8,30.06]
         }
df = pd.DataFrame(data)
df

# Transpose
df.head(20).T

In Excel you can copy paste and transpose.

What about SQL?

Leave a comment

Your email address will not be published. Required fields are marked *