Skip to content

Instantly share code, notes, and snippets.

@Ruhshan
Last active June 8, 2018 15:41
Show Gist options
  • Save Ruhshan/4196374f0ba7932aac09653018ee5a4f to your computer and use it in GitHub Desktop.
Save Ruhshan/4196374f0ba7932aac09653018ee5a4f to your computer and use it in GitHub Desktop.
I have a dataframe like this:
name vals
0 foo X
1 foo Y
2 foo Z
3 bar Y
4 bar Z
5 bar Q
How can I transform it to like this:
Q X Y Z
name
foo 0 1 1 1
bar 1 0 1 1
----------------------------------------------
Code for first df:
df = pd.DataFrame({"name": ["foo", "foo", "foo",
"bar", "bar", "bar"],
"vals": ["X", "Y", "Z",
"Y", "Z", "Q"],
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment