Skip to content

Instantly share code, notes, and snippets.

@thekensta
Last active January 19, 2016 15:39
Show Gist options
  • Save thekensta/3a3a3573da90b8b89e72 to your computer and use it in GitHub Desktop.
Save thekensta/3a3a3573da90b8b89e72 to your computer and use it in GitHub Desktop.
Pandas Pivot Multi Index
x = pd.DataFrame({"Date": np.repeat(pd.date_range("2015-01-01", "2015-01-04"), 2),
"Class": [1, 2, 1, 2, 1, 2, 1, 2],
"Value": np.random.random(size=8)})
p1 = pd.pivot_table(x, index="Date", columns="Class")
p2 = pd.pivot_table(x, index="Date", columns="Class", values="Value")
p1
# Value
# Class 1 2
# Date
# 2015-01-01 0.440874 0.692439
# 2015-01-02 0.263481 0.246643
# 2015-01-03 0.583451 0.764958
# 2015-01-04 0.344014 0.781684
# Where we have not Explicitly set the values="<some column name>"
# there is a MultiIndex on the columns
p1.columns
# MultiIndex(levels=[[u'Value'], [1, 2]],
# labels=[[0, 0], [0, 1]],
# names=[None, u'Class'])
p2
# Class 1 2
# Date
# 2015-01-01 0.440874 0.692439
# 2015-01-02 0.263481 0.246643
# 2015-01-03 0.583451 0.764958
# 2015-01-04 0.344014 0.781684
# Where we do Explicitly set the values="<some column name>"
# we have a single level index
p2.columns
#Int64Index([1, 2], dtype='int64', name=u'Class')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment