Skip to content

Instantly share code, notes, and snippets.

@spidezad
Last active April 3, 2019 09:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save spidezad/7a6d4231c0da67e8510b7cb182ecfd76 to your computer and use it in GitHub Desktop.
Save spidezad/7a6d4231c0da67e8510b7cb182ecfd76 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "# Convert to ratio of each row sum\np_cnt_ratio_df = df.div(df.sum(1), axis =0)\np_cnt_ratio_df",
"execution_count": 3,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 3,
"data": {
"text/plain": " b g r\n0 0.260417 0.250000 0.489583\n1 0.248227 0.283688 0.468085\n2 0.388186 0.303797 0.308017\n3 0.402878 0.316547 0.280576\n4 0.418079 0.175141 0.406780",
"text/html": "<div>\n<style scoped>\n .dataframe tbody tr th:only-of-type {\n vertical-align: middle;\n }\n\n .dataframe tbody tr th {\n vertical-align: top;\n }\n\n .dataframe thead th {\n text-align: right;\n }\n</style>\n<table border=\"1\" class=\"dataframe\">\n <thead>\n <tr style=\"text-align: right;\">\n <th></th>\n <th>b</th>\n <th>g</th>\n <th>r</th>\n </tr>\n </thead>\n <tbody>\n <tr>\n <th>0</th>\n <td>0.260417</td>\n <td>0.250000</td>\n <td>0.489583</td>\n </tr>\n <tr>\n <th>1</th>\n <td>0.248227</td>\n <td>0.283688</td>\n <td>0.468085</td>\n </tr>\n <tr>\n <th>2</th>\n <td>0.388186</td>\n <td>0.303797</td>\n <td>0.308017</td>\n </tr>\n <tr>\n <th>3</th>\n <td>0.402878</td>\n <td>0.316547</td>\n <td>0.280576</td>\n </tr>\n <tr>\n <th>4</th>\n <td>0.418079</td>\n <td>0.175141</td>\n <td>0.406780</td>\n </tr>\n </tbody>\n</table>\n</div>"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "# Do a cum sum along the row\nratio_cum_df = p_cnt_ratio_df.cumsum(axis=1)\nratio_cum_df",
"execution_count": 4,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 4,
"data": {
"text/plain": " b g r\n0 0.260417 0.510417 1.0\n1 0.248227 0.531915 1.0\n2 0.388186 0.691983 1.0\n3 0.402878 0.719424 1.0\n4 0.418079 0.593220 1.0",
"text/html": "<div>\n<style scoped>\n .dataframe tbody tr th:only-of-type {\n vertical-align: middle;\n }\n\n .dataframe tbody tr th {\n vertical-align: top;\n }\n\n .dataframe thead th {\n text-align: right;\n }\n</style>\n<table border=\"1\" class=\"dataframe\">\n <thead>\n <tr style=\"text-align: right;\">\n <th></th>\n <th>b</th>\n <th>g</th>\n <th>r</th>\n </tr>\n </thead>\n <tbody>\n <tr>\n <th>0</th>\n <td>0.260417</td>\n <td>0.510417</td>\n <td>1.0</td>\n </tr>\n <tr>\n <th>1</th>\n <td>0.248227</td>\n <td>0.531915</td>\n <td>1.0</td>\n </tr>\n <tr>\n <th>2</th>\n <td>0.388186</td>\n <td>0.691983</td>\n <td>1.0</td>\n </tr>\n <tr>\n <th>3</th>\n <td>0.402878</td>\n <td>0.719424</td>\n <td>1.0</td>\n </tr>\n <tr>\n <th>4</th>\n <td>0.418079</td>\n <td>0.593220</td>\n <td>1.0</td>\n </tr>\n </tbody>\n</table>\n</div>"
},
"metadata": {}
}
]
}
],
"metadata": {
"kernelspec": {
"name": "python3",
"display_name": "Python 3",
"language": "python"
},
"language_info": {
"name": "python",
"version": "3.6.4",
"mimetype": "text/x-python",
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"pygments_lexer": "ipython3",
"nbconvert_exporter": "python",
"file_extension": ".py"
},
"gist": {
"id": "7a6d4231c0da67e8510b7cb182ecfd76",
"data": {
"description": "",
"public": true
}
},
"_draft": {
"nbviewer_url": "https://gist.github.com/7a6d4231c0da67e8510b7cb182ecfd76"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment