Skip to content

Instantly share code, notes, and snippets.

@daxiongshu
Last active July 13, 2019 01:06
Show Gist options
  • Save daxiongshu/e3ec2548a80b19b1c1d24873e2e72671 to your computer and use it in GitHub Desktop.
Save daxiongshu/e3ec2548a80b19b1c1d24873e2e72671 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"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>ID_code</th>\n",
" <th>target</th>\n",
" <th>var_0</th>\n",
" <th>var_0_count</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>-82542400</td>\n",
" <td>0</td>\n",
" <td>11.796200</td>\n",
" <td>5</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>-82542399</td>\n",
" <td>0</td>\n",
" <td>5.279500</td>\n",
" <td>1</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>-82542398</td>\n",
" <td>0</td>\n",
" <td>8.472099</td>\n",
" <td>2</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>-82542397</td>\n",
" <td>1</td>\n",
" <td>12.271500</td>\n",
" <td>3</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>-82542396</td>\n",
" <td>0</td>\n",
" <td>11.644400</td>\n",
" <td>4</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" ID_code target var_0 var_0_count\n",
"0 -82542400 0 11.796200 5\n",
"1 -82542399 0 5.279500 1\n",
"2 -82542398 0 8.472099 2\n",
"3 -82542397 1 12.271500 3\n",
"4 -82542396 0 11.644400 4"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"var_0_count = train_gd.groupby('var_0').agg({'var_0':'count'})\n",
"var_0_count.columns = ['var_0_count']\n",
"var_0_count = var_0_count.reset_index()\n",
"train_gd = train_gd.merge(var_0_count,on='var_0',how='left')\n",
"\n",
"train_gd.head()[['ID_code','target','var_0','var_0_count']].to_pandas()"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.8"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment