|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "# Manipulating DataFrams with pandas" |
| 8 | + ] |
| 9 | + }, |
| 10 | + { |
| 11 | + "cell_type": "markdown", |
| 12 | + "metadata": {}, |
| 13 | + "source": [ |
| 14 | + "## Extracting and transforming data" |
| 15 | + ] |
| 16 | + }, |
| 17 | + { |
| 18 | + "cell_type": "code", |
| 19 | + "execution_count": 15, |
| 20 | + "metadata": {}, |
| 21 | + "outputs": [ |
| 22 | + { |
| 23 | + "data": { |
| 24 | + "text/html": [ |
| 25 | + "<div>\n", |
| 26 | + "<style scoped>\n", |
| 27 | + " .dataframe tbody tr th:only-of-type {\n", |
| 28 | + " vertical-align: middle;\n", |
| 29 | + " }\n", |
| 30 | + "\n", |
| 31 | + " .dataframe tbody tr th {\n", |
| 32 | + " vertical-align: top;\n", |
| 33 | + " }\n", |
| 34 | + "\n", |
| 35 | + " .dataframe thead th {\n", |
| 36 | + " text-align: right;\n", |
| 37 | + " }\n", |
| 38 | + "</style>\n", |
| 39 | + "<table border=\"1\" class=\"dataframe\">\n", |
| 40 | + " <thead>\n", |
| 41 | + " <tr style=\"text-align: right;\">\n", |
| 42 | + " <th></th>\n", |
| 43 | + " <th>City</th>\n", |
| 44 | + " <th>Edition</th>\n", |
| 45 | + " <th>Sport</th>\n", |
| 46 | + " <th>Discipline</th>\n", |
| 47 | + " <th>NOC</th>\n", |
| 48 | + " <th>Gender</th>\n", |
| 49 | + " <th>Event</th>\n", |
| 50 | + " <th>Event_gender</th>\n", |
| 51 | + " <th>Medal</th>\n", |
| 52 | + " </tr>\n", |
| 53 | + " <tr>\n", |
| 54 | + " <th>Athlete</th>\n", |
| 55 | + " <th></th>\n", |
| 56 | + " <th></th>\n", |
| 57 | + " <th></th>\n", |
| 58 | + " <th></th>\n", |
| 59 | + " <th></th>\n", |
| 60 | + " <th></th>\n", |
| 61 | + " <th></th>\n", |
| 62 | + " <th></th>\n", |
| 63 | + " <th></th>\n", |
| 64 | + " </tr>\n", |
| 65 | + " </thead>\n", |
| 66 | + " <tbody>\n", |
| 67 | + " <tr>\n", |
| 68 | + " <th>CHOROPHAS, Efstathios</th>\n", |
| 69 | + " <td>Athens</td>\n", |
| 70 | + " <td>1896</td>\n", |
| 71 | + " <td>Aquatics</td>\n", |
| 72 | + " <td>Swimming</td>\n", |
| 73 | + " <td>GRE</td>\n", |
| 74 | + " <td>Men</td>\n", |
| 75 | + " <td>1200m freestyle</td>\n", |
| 76 | + " <td>M</td>\n", |
| 77 | + " <td>Bronze</td>\n", |
| 78 | + " </tr>\n", |
| 79 | + " <tr>\n", |
| 80 | + " <th>HAJOS, Alfred</th>\n", |
| 81 | + " <td>Athens</td>\n", |
| 82 | + " <td>1896</td>\n", |
| 83 | + " <td>Aquatics</td>\n", |
| 84 | + " <td>Swimming</td>\n", |
| 85 | + " <td>HUN</td>\n", |
| 86 | + " <td>Men</td>\n", |
| 87 | + " <td>1200m freestyle</td>\n", |
| 88 | + " <td>M</td>\n", |
| 89 | + " <td>Gold</td>\n", |
| 90 | + " </tr>\n", |
| 91 | + " <tr>\n", |
| 92 | + " <th>ANDREOU, Joannis</th>\n", |
| 93 | + " <td>Athens</td>\n", |
| 94 | + " <td>1896</td>\n", |
| 95 | + " <td>Aquatics</td>\n", |
| 96 | + " <td>Swimming</td>\n", |
| 97 | + " <td>GRE</td>\n", |
| 98 | + " <td>Men</td>\n", |
| 99 | + " <td>1200m freestyle</td>\n", |
| 100 | + " <td>M</td>\n", |
| 101 | + " <td>Silver</td>\n", |
| 102 | + " </tr>\n", |
| 103 | + " <tr>\n", |
| 104 | + " <th>CHOROPHAS, Efstathios</th>\n", |
| 105 | + " <td>Athens</td>\n", |
| 106 | + " <td>1896</td>\n", |
| 107 | + " <td>Aquatics</td>\n", |
| 108 | + " <td>Swimming</td>\n", |
| 109 | + " <td>GRE</td>\n", |
| 110 | + " <td>Men</td>\n", |
| 111 | + " <td>400m freestyle</td>\n", |
| 112 | + " <td>M</td>\n", |
| 113 | + " <td>Bronze</td>\n", |
| 114 | + " </tr>\n", |
| 115 | + " <tr>\n", |
| 116 | + " <th>NEUMANN, Paul</th>\n", |
| 117 | + " <td>Athens</td>\n", |
| 118 | + " <td>1896</td>\n", |
| 119 | + " <td>Aquatics</td>\n", |
| 120 | + " <td>Swimming</td>\n", |
| 121 | + " <td>AUT</td>\n", |
| 122 | + " <td>Men</td>\n", |
| 123 | + " <td>400m freestyle</td>\n", |
| 124 | + " <td>M</td>\n", |
| 125 | + " <td>Gold</td>\n", |
| 126 | + " </tr>\n", |
| 127 | + " <tr>\n", |
| 128 | + " <th>PEPANOS, Antonios</th>\n", |
| 129 | + " <td>Athens</td>\n", |
| 130 | + " <td>1896</td>\n", |
| 131 | + " <td>Aquatics</td>\n", |
| 132 | + " <td>Swimming</td>\n", |
| 133 | + " <td>GRE</td>\n", |
| 134 | + " <td>Men</td>\n", |
| 135 | + " <td>400m freestyle</td>\n", |
| 136 | + " <td>M</td>\n", |
| 137 | + " <td>Silver</td>\n", |
| 138 | + " </tr>\n", |
| 139 | + " <tr>\n", |
| 140 | + " <th>LANE, Francis</th>\n", |
| 141 | + " <td>Athens</td>\n", |
| 142 | + " <td>1896</td>\n", |
| 143 | + " <td>Athletics</td>\n", |
| 144 | + " <td>Athletics</td>\n", |
| 145 | + " <td>USA</td>\n", |
| 146 | + " <td>Men</td>\n", |
| 147 | + " <td>100m</td>\n", |
| 148 | + " <td>M</td>\n", |
| 149 | + " <td>Bronze</td>\n", |
| 150 | + " </tr>\n", |
| 151 | + " <tr>\n", |
| 152 | + " <th>SZOKOLYI, Alajos</th>\n", |
| 153 | + " <td>Athens</td>\n", |
| 154 | + " <td>1896</td>\n", |
| 155 | + " <td>Athletics</td>\n", |
| 156 | + " <td>Athletics</td>\n", |
| 157 | + " <td>HUN</td>\n", |
| 158 | + " <td>Men</td>\n", |
| 159 | + " <td>100m</td>\n", |
| 160 | + " <td>M</td>\n", |
| 161 | + " <td>Bronze</td>\n", |
| 162 | + " </tr>\n", |
| 163 | + " <tr>\n", |
| 164 | + " <th>BURKE, Thomas</th>\n", |
| 165 | + " <td>Athens</td>\n", |
| 166 | + " <td>1896</td>\n", |
| 167 | + " <td>Athletics</td>\n", |
| 168 | + " <td>Athletics</td>\n", |
| 169 | + " <td>USA</td>\n", |
| 170 | + " <td>Men</td>\n", |
| 171 | + " <td>100m</td>\n", |
| 172 | + " <td>M</td>\n", |
| 173 | + " <td>Gold</td>\n", |
| 174 | + " </tr>\n", |
| 175 | + " <tr>\n", |
| 176 | + " <th>HOFMANN, Fritz</th>\n", |
| 177 | + " <td>Athens</td>\n", |
| 178 | + " <td>1896</td>\n", |
| 179 | + " <td>Athletics</td>\n", |
| 180 | + " <td>Athletics</td>\n", |
| 181 | + " <td>GER</td>\n", |
| 182 | + " <td>Men</td>\n", |
| 183 | + " <td>100m</td>\n", |
| 184 | + " <td>M</td>\n", |
| 185 | + " <td>Silver</td>\n", |
| 186 | + " </tr>\n", |
| 187 | + " </tbody>\n", |
| 188 | + "</table>\n", |
| 189 | + "</div>" |
| 190 | + ], |
| 191 | + "text/plain": [ |
| 192 | + " City Edition Sport Discipline NOC Gender Event Event_gender Medal\n", |
| 193 | + "Athlete \n", |
| 194 | + "CHOROPHAS, Efstathios Athens 1896 Aquatics Swimming GRE Men 1200m freestyle M Bronze\n", |
| 195 | + "HAJOS, Alfred Athens 1896 Aquatics Swimming HUN Men 1200m freestyle M Gold\n", |
| 196 | + "ANDREOU, Joannis Athens 1896 Aquatics Swimming GRE Men 1200m freestyle M Silver\n", |
| 197 | + "CHOROPHAS, Efstathios Athens 1896 Aquatics Swimming GRE Men 400m freestyle M Bronze\n", |
| 198 | + "NEUMANN, Paul Athens 1896 Aquatics Swimming AUT Men 400m freestyle M Gold\n", |
| 199 | + "PEPANOS, Antonios Athens 1896 Aquatics Swimming GRE Men 400m freestyle M Silver\n", |
| 200 | + "LANE, Francis Athens 1896 Athletics Athletics USA Men 100m M Bronze\n", |
| 201 | + "SZOKOLYI, Alajos Athens 1896 Athletics Athletics HUN Men 100m M Bronze\n", |
| 202 | + "BURKE, Thomas Athens 1896 Athletics Athletics USA Men 100m M Gold\n", |
| 203 | + "HOFMANN, Fritz Athens 1896 Athletics Athletics GER Men 100m M Silver" |
| 204 | + ] |
| 205 | + }, |
| 206 | + "execution_count": 15, |
| 207 | + "metadata": {}, |
| 208 | + "output_type": "execute_result" |
| 209 | + } |
| 210 | + ], |
| 211 | + "source": [ |
| 212 | + "import pandas as pd\n", |
| 213 | + "df = pd.read_csv('https://assets.datacamp.com/production/repositories/502/datasets/bf22326ecc9171f68796ad805a7c1135288120b6/all_medalists.csv', index_col='Athlete')\n", |
| 214 | + "df[5:15]" |
| 215 | + ] |
| 216 | + }, |
| 217 | + { |
| 218 | + "cell_type": "markdown", |
| 219 | + "metadata": {}, |
| 220 | + "source": [ |
| 221 | + "### Indexing using square brackets" |
| 222 | + ] |
| 223 | + }, |
| 224 | + { |
| 225 | + "cell_type": "code", |
| 226 | + "execution_count": 12, |
| 227 | + "metadata": {}, |
| 228 | + "outputs": [ |
| 229 | + { |
| 230 | + "data": { |
| 231 | + "text/plain": [ |
| 232 | + "HERSCHMANN, Otto Athens\n", |
| 233 | + "HERSCHMANN, Otto Stockholm\n", |
| 234 | + "Name: City, dtype: object" |
| 235 | + ] |
| 236 | + }, |
| 237 | + "execution_count": 12, |
| 238 | + "metadata": {}, |
| 239 | + "output_type": "execute_result" |
| 240 | + } |
| 241 | + ], |
| 242 | + "source": [ |
| 243 | + "df['City']['HERSCHMANN, Otto']" |
| 244 | + ] |
| 245 | + }, |
| 246 | + { |
| 247 | + "cell_type": "markdown", |
| 248 | + "metadata": {}, |
| 249 | + "source": [ |
| 250 | + "### Using column attribute and row label" |
| 251 | + ] |
| 252 | + }, |
| 253 | + { |
| 254 | + "cell_type": "code", |
| 255 | + "execution_count": 18, |
| 256 | + "metadata": {}, |
| 257 | + "outputs": [ |
| 258 | + { |
| 259 | + "data": { |
| 260 | + "text/plain": [ |
| 261 | + "HAJOS, Alfred Gold\n", |
| 262 | + "HAJOS, Alfred Gold\n", |
| 263 | + "Name: Medal, dtype: object" |
| 264 | + ] |
| 265 | + }, |
| 266 | + "execution_count": 18, |
| 267 | + "metadata": {}, |
| 268 | + "output_type": "execute_result" |
| 269 | + } |
| 270 | + ], |
| 271 | + "source": [ |
| 272 | + "df.Medal['HAJOS, Alfred']" |
| 273 | + ] |
| 274 | + }, |
| 275 | + { |
| 276 | + "cell_type": "markdown", |
| 277 | + "metadata": {}, |
| 278 | + "source": [ |
| 279 | + "### Using the .loc and .iloc accessors" |
| 280 | + ] |
| 281 | + }, |
| 282 | + { |
| 283 | + "cell_type": "code", |
| 284 | + "execution_count": 21, |
| 285 | + "metadata": {}, |
| 286 | + "outputs": [ |
| 287 | + { |
| 288 | + "data": { |
| 289 | + "text/plain": [ |
| 290 | + "'Bronze'" |
| 291 | + ] |
| 292 | + }, |
| 293 | + "execution_count": 21, |
| 294 | + "metadata": {}, |
| 295 | + "output_type": "execute_result" |
| 296 | + } |
| 297 | + ], |
| 298 | + "source": [ |
| 299 | + "#df.loc[row specifier, column specifier]\n", |
| 300 | + "df.loc['LANE, Francis', 'Medal']" |
| 301 | + ] |
| 302 | + }, |
| 303 | + { |
| 304 | + "cell_type": "code", |
| 305 | + "execution_count": 26, |
| 306 | + "metadata": {}, |
| 307 | + "outputs": [ |
| 308 | + { |
| 309 | + "data": { |
| 310 | + "text/plain": [ |
| 311 | + "'100m freestyle for sailors'" |
| 312 | + ] |
| 313 | + }, |
| 314 | + "execution_count": 26, |
| 315 | + "metadata": {}, |
| 316 | + "output_type": "execute_result" |
| 317 | + } |
| 318 | + ], |
| 319 | + "source": [ |
| 320 | + "df.iloc[4, 6]" |
| 321 | + ] |
| 322 | + } |
| 323 | + ], |
| 324 | + "metadata": { |
| 325 | + "kernelspec": { |
| 326 | + "display_name": "Python 3", |
| 327 | + "language": "python", |
| 328 | + "name": "python3" |
| 329 | + }, |
| 330 | + "language_info": { |
| 331 | + "codemirror_mode": { |
| 332 | + "name": "ipython", |
| 333 | + "version": 3 |
| 334 | + }, |
| 335 | + "file_extension": ".py", |
| 336 | + "mimetype": "text/x-python", |
| 337 | + "name": "python", |
| 338 | + "nbconvert_exporter": "python", |
| 339 | + "pygments_lexer": "ipython3", |
| 340 | + "version": "3.7.3" |
| 341 | + }, |
| 342 | + "toc": { |
| 343 | + "base_numbering": 1, |
| 344 | + "nav_menu": {}, |
| 345 | + "number_sections": true, |
| 346 | + "sideBar": true, |
| 347 | + "skip_h1_title": false, |
| 348 | + "title_cell": "Table of Contents", |
| 349 | + "title_sidebar": "Contents", |
| 350 | + "toc_cell": false, |
| 351 | + "toc_position": {}, |
| 352 | + "toc_section_display": true, |
| 353 | + "toc_window_display": false |
| 354 | + } |
| 355 | + }, |
| 356 | + "nbformat": 4, |
| 357 | + "nbformat_minor": 2 |
| 358 | +} |
0 commit comments