Skip to content

Instantly share code, notes, and snippets.

@adumont
Created August 26, 2019 22:28
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 adumont/bc2bac1b6cf7ba547e7ba6a19c01adb6 to your computer and use it in GitHub Desktop.
Save adumont/bc2bac1b6cf7ba547e7ba6a19c01adb6 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": [],
"source": [
"import pandas as pd\n",
"import numpy as np"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [],
"source": [
"df = pd.DataFrame({'i':range(20),\n",
" 'L':[chr(97+i) for i in range(20)]\n",
" })\n",
"\n",
"df['L2'] = df['L']"
]
},
{
"cell_type": "code",
"execution_count": 17,
"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>0</th>\n",
" <th>1</th>\n",
" <th>2</th>\n",
" <th>3</th>\n",
" <th>4</th>\n",
" <th>5</th>\n",
" <th>6</th>\n",
" <th>7</th>\n",
" <th>8</th>\n",
" <th>9</th>\n",
" <th>10</th>\n",
" <th>11</th>\n",
" <th>12</th>\n",
" <th>13</th>\n",
" <th>14</th>\n",
" <th>15</th>\n",
" <th>16</th>\n",
" <th>17</th>\n",
" <th>18</th>\n",
" <th>19</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>i</th>\n",
" <td>0</td>\n",
" <td>1</td>\n",
" <td>2</td>\n",
" <td>3</td>\n",
" <td>4</td>\n",
" <td>5</td>\n",
" <td>6</td>\n",
" <td>7</td>\n",
" <td>8</td>\n",
" <td>9</td>\n",
" <td>10</td>\n",
" <td>11</td>\n",
" <td>12</td>\n",
" <td>13</td>\n",
" <td>14</td>\n",
" <td>15</td>\n",
" <td>16</td>\n",
" <td>17</td>\n",
" <td>18</td>\n",
" <td>19</td>\n",
" </tr>\n",
" <tr>\n",
" <th>L</th>\n",
" <td>a</td>\n",
" <td>b</td>\n",
" <td>c</td>\n",
" <td>d</td>\n",
" <td>e</td>\n",
" <td>f</td>\n",
" <td>g</td>\n",
" <td>h</td>\n",
" <td>i</td>\n",
" <td>j</td>\n",
" <td>k</td>\n",
" <td>l</td>\n",
" <td>m</td>\n",
" <td>n</td>\n",
" <td>o</td>\n",
" <td>p</td>\n",
" <td>q</td>\n",
" <td>r</td>\n",
" <td>s</td>\n",
" <td>t</td>\n",
" </tr>\n",
" <tr>\n",
" <th>L2</th>\n",
" <td>a</td>\n",
" <td>b</td>\n",
" <td>c</td>\n",
" <td>d</td>\n",
" <td>e</td>\n",
" <td>f</td>\n",
" <td>g</td>\n",
" <td>h</td>\n",
" <td>i</td>\n",
" <td>j</td>\n",
" <td>k</td>\n",
" <td>l</td>\n",
" <td>m</td>\n",
" <td>n</td>\n",
" <td>o</td>\n",
" <td>p</td>\n",
" <td>q</td>\n",
" <td>r</td>\n",
" <td>s</td>\n",
" <td>t</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19\n",
"i 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19\n",
"L a b c d e f g h i j k l m n o p q r s t\n",
"L2 a b c d e f g h i j k l m n o p q r s t"
]
},
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.T"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(20, 8)"
]
},
"execution_count": 20,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"n_rows=len(df)\n",
"n_shuffle=int(n_rows*0.4)\n",
"n_rows, n_shuffle"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([ 3, 0, 11, 16, 14, 4, 8, 12])"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"pick_rows=np.random.permutation(list(range(n_rows)))[0:n_shuffle]\n",
"pick_rows"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array(['l', 'e', 'd', 'q', 'o', 'i', 'm', 'a'], dtype=object)"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"shuffled_values=np.random.permutation(df['L2'][pick_rows])\n",
"shuffled_values"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"C:\\Users\\adumont\\.conda\\envs\\fastai-cpu\\lib\\site-packages\\ipykernel_launcher.py:1: SettingWithCopyWarning: \n",
"A value is trying to be set on a copy of a slice from a DataFrame\n",
"\n",
"See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
" \"\"\"Entry point for launching an IPython kernel.\n"
]
}
],
"source": [
"df['L2'][pick_rows]=shuffled_values"
]
},
{
"cell_type": "code",
"execution_count": 15,
"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>0</th>\n",
" <th>1</th>\n",
" <th>2</th>\n",
" <th>3</th>\n",
" <th>4</th>\n",
" <th>5</th>\n",
" <th>6</th>\n",
" <th>7</th>\n",
" <th>8</th>\n",
" <th>9</th>\n",
" <th>10</th>\n",
" <th>11</th>\n",
" <th>12</th>\n",
" <th>13</th>\n",
" <th>14</th>\n",
" <th>15</th>\n",
" <th>16</th>\n",
" <th>17</th>\n",
" <th>18</th>\n",
" <th>19</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>i</th>\n",
" <td>0</td>\n",
" <td>1</td>\n",
" <td>2</td>\n",
" <td>3</td>\n",
" <td>4</td>\n",
" <td>5</td>\n",
" <td>6</td>\n",
" <td>7</td>\n",
" <td>8</td>\n",
" <td>9</td>\n",
" <td>10</td>\n",
" <td>11</td>\n",
" <td>12</td>\n",
" <td>13</td>\n",
" <td>14</td>\n",
" <td>15</td>\n",
" <td>16</td>\n",
" <td>17</td>\n",
" <td>18</td>\n",
" <td>19</td>\n",
" </tr>\n",
" <tr>\n",
" <th>L</th>\n",
" <td>a</td>\n",
" <td>b</td>\n",
" <td>c</td>\n",
" <td>d</td>\n",
" <td>e</td>\n",
" <td>f</td>\n",
" <td>g</td>\n",
" <td>h</td>\n",
" <td>i</td>\n",
" <td>j</td>\n",
" <td>k</td>\n",
" <td>l</td>\n",
" <td>m</td>\n",
" <td>n</td>\n",
" <td>o</td>\n",
" <td>p</td>\n",
" <td>q</td>\n",
" <td>r</td>\n",
" <td>s</td>\n",
" <td>t</td>\n",
" </tr>\n",
" <tr>\n",
" <th>L2</th>\n",
" <td>e</td>\n",
" <td>b</td>\n",
" <td>c</td>\n",
" <td>l</td>\n",
" <td>i</td>\n",
" <td>f</td>\n",
" <td>g</td>\n",
" <td>h</td>\n",
" <td>m</td>\n",
" <td>j</td>\n",
" <td>k</td>\n",
" <td>d</td>\n",
" <td>a</td>\n",
" <td>n</td>\n",
" <td>o</td>\n",
" <td>p</td>\n",
" <td>q</td>\n",
" <td>r</td>\n",
" <td>s</td>\n",
" <td>t</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19\n",
"i 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19\n",
"L a b c d e f g h i j k l m n o p q r s t\n",
"L2 e b c l i f g h m j k d a n o p q r s t"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.T"
]
}
],
"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.9"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment