Title: Pandas: Long To Wide Format
Slug: pandas_long_to_wide
Summary: Pandas: Long To Wide Format
Date: 2016-05-01 12:00
Category: Python
Tags: Data Wrangling
Authors: Chris Albon

import modules


In [2]:
import pandas as pd

Create "long" dataframe


In [3]:
raw_data = {'patient': [1, 1, 1, 2, 2], 
        'obs': [1, 2, 3, 1, 2], 
        'treatment': [0, 1, 0, 1, 0],
        'score': [6252, 24243, 2345, 2342, 23525]} 
df = pd.DataFrame(raw_data, columns = ['patient', 'obs', 'treatment', 'score'])
df


Out[3]:
patient obs treatment score
0 1 1 0 6252
1 1 2 1 24243
2 1 3 0 2345
3 2 1 1 2342
4 2 2 0 23525

Make a "wide" data

Now we will create a "wide" dataframe with the rows by patient number, the columns being by observation number, and the cell values being the score values.


In [4]:
df.pivot(index='patient', columns='obs', values='score')


Out[4]:
obs 1 2 3
patient
1 6252.0 24243.0 2345.0
2 2342.0 23525.0 NaN