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
In [2]:
import pandas as pd
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]:
In [4]:
df.pivot(index='patient', columns='obs', values='score')
Out[4]: