Skip to content

Instantly share code, notes, and snippets.

@ryan-williams
Last active December 3, 2019 00:56
Show Gist options
  • Save ryan-williams/3872a38c1d8670246f10829a004b9095 to your computer and use it in GitHub Desktop.
Save ryan-williams/3872a38c1d8670246f10829a004b9095 to your computer and use it in GitHub Desktop.
Pandas functions for a 2-D histogram of a dataframe: one column's values become the columns, and values become counts of given {row, column} pairs)
  • Col1 is the field that will be the "rows" index
  • Col2 is the column whose values will become the new columns
  • Col3 is any other column (assuming that other columns are always filled; .count() will only count cells where Col3 has a value)
df \
.groupby(['Col1', 'Col2']) \
[['Col3']] \
.count() \
.reset_index() \
.pivot('Col1', 'Col2', 'Col3')

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