Skip to content

Instantly share code, notes, and snippets.

@gyli
Last active May 29, 2017 05:47
Show Gist options
  • Save gyli/41247d3605d87145f2502f10a8513cad to your computer and use it in GitHub Desktop.
Save gyli/41247d3605d87145f2502f10a8513cad to your computer and use it in GitHub Desktop.
Convert long table to wide table in Python Pandas
# Convert long table like
# key long_column other_column_1
# ------------------------------------
# 0 Ann 1 A
# 1 Ann 3 A
# 2 Ann 5 A
# 3 John 3 B
# 4 John 5 B
# 5 George 1 A
# 6 George 3 A
#
# to
#
# wide_column_names wide_value1 wide_value3 wide_value5 other_column_1
# --------------------------------------------------------------------
# key
# Ann 1 3 5 A
# George 1 3 None A
# John None 3 5 B
import pandas as pd
data = pd.read_csv('data.csv')
# Group the long column values
data['key_group'] = data.groupby('key').cumcount()
# Use the long column values to build wide table column names
data['wide_column_names'] = 'wide_value_' + data['long_column']
# Generate wide table
wide_table = data.pivot(index='key', columns='wide_column_names', values='long_column')
# Attach other columns to wide table
data = data.set_index('key')
for other_column in ['other_column_1']:
wide_table[other_column] = data.groupby(data.index).first()[other_column]
wide_table.to_csv('wide_table.csv', index=None)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment