Skip to content

Instantly share code, notes, and snippets.

@rainsunny
Created September 5, 2017 09:19
Show Gist options
  • Save rainsunny/c1c2a95ed62372e002b293cab028dd08 to your computer and use it in GitHub Desktop.
Save rainsunny/c1c2a95ed62372e002b293cab028dd08 to your computer and use it in GitHub Desktop.
Turn certain columns to key-value rows ( and the reverse )
"""
Turn this
location name Jan-2010 Feb-2010 March-2010
A "test" 12 20 30
B "foo" 18 20 25
into this
location name Date Value
A "test" Jan-2010 12
A "test" Feb-2010 20
A "test" March-2010 30
B "foo" Jan-2010 18
B "foo" Feb-2010 20
B "foo" March-2010 25
Reference: https://stackoverflow.com/questions/28654047/pandas-convert-some-columns-into-rows
"""
########## Using pandas.melt
import pandas as pd
df = pd.DataFrame({"location": ["A", "B"], "name": ["test", "foo"],
"Jan-2010": [12, 18], "Feb-2010": [20, 20], "Mar-2010": [30, 25]})
df2 = pd.melt(df, id_vars=["location", "name"], var_name="Date", value_name="Value")
df2 = df2.sort_values(["location", "name"])
##############
# TODO: how to do the reverse process
@rainsunny
Copy link
Author

The reverse process: pandas.DataFrame.pivot

From the doc string of DataFrame.pivot:

Reshape data (produce a "pivot" table) based on column values. Uses
unique values from index / columns to form axes of the resulting
DataFrame.

So the code is like this:

df3 = df2.pivot(index='location', columns='Date', values='Value')
df3 = df3.reset_index()
df3['name'] = ['test', 'foo']
df3

Not so good?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment