Long and Wide Data Format Converter
Created:December 17, 2024
Last Updated:March 22, 2025
Tool
1. Upload Your Data
Not sure about data formats? Check out the examples in the Learn More section below. Save your data when you are done because this tool does not save your data anywhere.
Learn More
Understanding Long and Wide Data Formats
Long Format (Tidy)
Each row is an observation, with variables in columns
ID,Year,Measurement,Value 1,2020,Temperature,18.5 1,2020,Humidity,30 1,2021,Temperature,19.0 1,2021,Humidity,32 2,2020,Temperature,17.0 2,2020,Humidity,40 2,2021,Temperature,16.5 2,2021,Humidity,42
Wide Format
Data spread across multiple columns with unique identifiers
ID,Year,Temperature,Humidity 1,2020,18.5,30 1,2021,19.0,32 2,2020,17.0,40 2,2021,16.5,42
Long to Wide Transformation
Original Data (Long Format)
ID | Year | Measurement | Value |
---|---|---|---|
1 | 2020 | Temperature | 18.5 |
1 | 2020 | Humidity | 30 |
1 | 2021 | Temperature | 19 |
1 | 2021 | Humidity | 32 |
2 | 2020 | Temperature | 17 |
2 | 2020 | Humidity | 40 |
2 | 2021 | Temperature | 16.5 |
2 | 2021 | Humidity | 42 |
Column Selection for Long to Wide
Select these columns:
ID Columns:
- ID (identifies unique entries)
- Year (identifies unique entries)
Value Columns:
- Measurement (will become new column names)
- Value (will fill the new columns)
Result (Wide Format)
ID | Year | Temperature | Humidity |
---|---|---|---|
1 | 2020 | 18.5 | 30 |
1 | 2021 | 19 | 32 |
2 | 2020 | 17 | 40 |
2 | 2021 | 16.5 | 42 |
Wide to Long Transformation
Original Data (Wide Format)
ID | Year | Temperature | Humidity |
---|---|---|---|
1 | 2020 | 18.5 | 30 |
1 | 2021 | 19 | 32 |
2 | 2020 | 17 | 40 |
2 | 2021 | 16.5 | 42 |
Column Selection for Wide to Long
Select these columns:
ID Columns:
- ID (stays as is)
- Year (stays as is)
Value Columns:
- Temperature (will be stacked)
- Humidity (will be stacked)
This will create two new columns: "Measurement" (containing "Temperature" or "Humidity") and "Value" (containing the corresponding numbers)
Result (Long Format)
ID | Year | Measurement | Value |
---|---|---|---|
1 | 2020 | Temperature | 18.5 |
1 | 2020 | Humidity | 30 |
1 | 2021 | Temperature | 19 |
1 | 2021 | Humidity | 32 |
2 | 2020 | Temperature | 17 |
2 | 2020 | Humidity | 40 |
2 | 2021 | Temperature | 16.5 |
2 | 2021 | Humidity | 42 |
How to convert long and wide data formats in Python and R
Python Code Example
Wide to Long Format
Python
import pandas as pd
# create wide format data
wide_df = pd.DataFrame({
'ID': [1, 1, 2, 2],
'Year': [2020, 2021, 2020, 2021],
'Temperature': [18.5, 19.0, 17.0, 16.5],
'Humidity': [30, 32, 40, 42]
})
print(wide_df)
# Convert wide to long format
long_df = pd.melt(
wide_df,
id_vars=['ID', 'Year'],
value_vars=['Temperature', 'Humidity'],
var_name='Measurement',
value_name='Value'
)
print(long_df)
# Convert back to wide format
wide_df = long_df.pivot_table(
index=['ID', 'Year'],
columns='Measurement',
values='Value'
).reset_index()
print(wide_df)
R Code Example
Wide to Long Format
R
library(tidyverse)
# wide format data
wide_data <- data.frame(
ID = c(1, 1, 2, 2),
Year = c(2020, 2021, 2020, 2021),
Temperature = c(18.5, 19.0, 17.0, 16.5),
Humidity = c(30, 32, 40, 42)
)
print(wide_data)
# Convert wide to long format
long_data <- wide_data |>
pivot_longer(
cols = c(Temperature, Humidity),
names_to = "Measurement",
values_to = "Value"
)
print(long_data)
# Convert long back to wide format
wide_data <- long_data |>
pivot_wider(
id_cols = c(ID, Year),
names_from = Measurement,
values_from = Value
)
print(wide_data)