0
0
Data Analysis Pythondata~5 mins

Reshaping and transposing in Data Analysis Python

Choose your learning style9 modes available
Introduction

Reshaping and transposing help you change the shape or layout of your data. This makes it easier to analyze or visualize.

You want to switch rows and columns to better compare data.
You need to convert a long list of data into a table format.
You want to flatten a table into a single list for simple calculations.
You need to prepare data for a chart that requires a specific shape.
You want to organize data to match the input format of a machine learning model.
Syntax
Data Analysis Python
import pandas as pd

# Transpose a DataFrame
df.T

# Reshape using pivot
pivoted_df = df.pivot(index='row_label', columns='column_label', values='value')

# Reshape using melt
melted_df = pd.melt(df, id_vars=['id_vars'], value_vars=['value_vars'])

Transpose flips rows and columns.

Pivot and melt change data between wide and long formats.

Examples
This transposes the DataFrame, swapping rows and columns.
Data Analysis Python
import pandas as pd

df = pd.DataFrame({
    'A': [1, 2],
    'B': [3, 4]
})

print(df.T)
This reshapes data to show temperatures by date and city.
Data Analysis Python
import pandas as pd

df = pd.DataFrame({
    'Date': ['2023-01-01', '2023-01-01', '2023-01-02'],
    'City': ['NY', 'LA', 'NY'],
    'Temp': [30, 40, 35]
})

pivoted = df.pivot(index='Date', columns='City', values='Temp')
print(pivoted)
This converts wide data back to long format.
Data Analysis Python
import pandas as pd

df = pd.DataFrame({
    'Date': ['2023-01-01', '2023-01-02'],
    'NY': [30, 35],
    'LA': [40, 45]
})

melted = pd.melt(df, id_vars=['Date'], value_vars=['NY', 'LA'], var_name='City', value_name='Temp')
print(melted)
Sample Program

This program shows how to transpose a table, then reshape it from wide to long format and back to wide format.

Data Analysis Python
import pandas as pd

# Create a simple DataFrame
df = pd.DataFrame({
    'Name': ['Alice', 'Bob', 'Charlie'],
    'Math': [85, 90, 95],
    'Science': [88, 92, 85]
})

print('Original DataFrame:')
print(df)

# Transpose the DataFrame
transposed = df.T
print('\nTransposed DataFrame:')
print(transposed)

# Reshape using melt to long format
melted = pd.melt(df, id_vars=['Name'], value_vars=['Math', 'Science'], var_name='Subject', value_name='Score')
print('\nMelted DataFrame (long format):')
print(melted)

# Reshape back to wide format using pivot
pivoted = melted.pivot(index='Name', columns='Subject', values='Score')
print('\nPivoted DataFrame (wide format):')
print(pivoted)
OutputSuccess
Important Notes

Transposing works well for small tables but can be confusing with large data.

Pivot requires unique index/column pairs; duplicates cause errors.

Melt is useful to prepare data for plotting or analysis that needs long format.

Summary

Reshaping changes how data is organized to fit your analysis needs.

Transposing swaps rows and columns easily.

Pivot and melt convert data between wide and long formats.