├── Essential Basic Functionality.ipynb ├── Generating Dates between Start and End Dates.ipynb ├── Group By- split-apply-combine.ipynb ├── Indexing and Selecting Data.ipynb ├── Merge, join, and concatenate.ipynb ├── PandasCheatSheet.html ├── PandasCheatSheet.ipynb ├── Reshaping and Pivot Tables.ipynb ├── Time Series and Date functionality.ipynb ├── Working with Text Data.ipynb └── data └── baseball.csv /Generating Dates between Start and End Dates.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": { 7 | "collapsed": true 8 | }, 9 | "outputs": [], 10 | "source": [ 11 | "from IPython.core.display import display, HTML\n", 12 | "\n", 13 | "import pandas as pd\n", 14 | "import numpy as np" 15 | ] 16 | }, 17 | { 18 | "cell_type": "markdown", 19 | "metadata": {}, 20 | "source": [ 21 | "https://stackoverflow.com/questions/34304649/iterate-date-range-using-python-pandas" 22 | ] 23 | }, 24 | { 25 | "cell_type": "markdown", 26 | "metadata": {}, 27 | "source": [ 28 | "#### COPY TABLE BELOW TO CLIPBOARD\n", 29 | "\n", 30 | "
ID Arrival Departure EndDate StartDate
0 1922 ParisFrance NewYorkUnitedState 3/10/08 2/2/01
1 1002 LosAngelesUnitedState ForidaUnitedState 3/10/08 12/1/08
2 1901 ParisFrance LagosNigeria 3/5/01 2/2/01
3 1922 ParisFrance NewYorkUnitedState 2/3/11 12/1/08
4 1002 ParisFrance CaliforniaUnitedState 3/4/03 3/4/02
5 1099 ParisFrance BeijingChina 2/3/11 2/4/09
6 1901 LosAngelesUnitedState ParisFrance 3/5/01 2/2/01
" 31 | ] 32 | }, 33 | { 34 | "cell_type": "code", 35 | "execution_count": 2, 36 | "metadata": { 37 | "collapsed": false 38 | }, 39 | "outputs": [], 40 | "source": [ 41 | "df = pd.read_clipboard()" 42 | ] 43 | }, 44 | { 45 | "cell_type": "code", 46 | "execution_count": 3, 47 | "metadata": { 48 | "collapsed": false 49 | }, 50 | "outputs": [ 51 | { 52 | "data": { 53 | "text/plain": [ 54 | "ID int64\n", 55 | "Arrival object\n", 56 | "Departure object\n", 57 | "EndDate object\n", 58 | "StartDate object\n", 59 | "dtype: object" 60 | ] 61 | }, 62 | "execution_count": 3, 63 | "metadata": {}, 64 | "output_type": "execute_result" 65 | } 66 | ], 67 | "source": [ 68 | "df.dtypes" 69 | ] 70 | }, 71 | { 72 | "cell_type": "code", 73 | "execution_count": 4, 74 | "metadata": { 75 | "collapsed": false 76 | }, 77 | "outputs": [], 78 | "source": [ 79 | "# Convert dates to datetime dytpes\n", 80 | "df.EndDate = pd.to_datetime(df.EndDate)\n", 81 | "df.StartDate = pd.to_datetime(df.StartDate)" 82 | ] 83 | }, 84 | { 85 | "cell_type": "code", 86 | "execution_count": 5, 87 | "metadata": { 88 | "collapsed": false 89 | }, 90 | "outputs": [], 91 | "source": [ 92 | "# Set Index to StartDate\n", 93 | "df = df.set_index('StartDate', drop = False)" 94 | ] 95 | }, 96 | { 97 | "cell_type": "code", 98 | "execution_count": 6, 99 | "metadata": { 100 | "collapsed": true 101 | }, 102 | "outputs": [], 103 | "source": [ 104 | "# Create Empty DataFrame\n", 105 | "new_df = pd.DataFrame()" 106 | ] 107 | }, 108 | { 109 | "cell_type": "code", 110 | "execution_count": 7, 111 | "metadata": { 112 | "collapsed": false 113 | }, 114 | "outputs": [], 115 | "source": [ 116 | "# Iteration through each ID and Generate Dates between Start and End Dates\n", 117 | "for i, data in df.iterrows():\n", 118 | " data = data.to_frame().transpose()\n", 119 | " data = data.reindex(pd.date_range(start=data.index[0], end=data.EndDate[0])).fillna(method='ffill').reset_index().rename(columns={'index': 'Date'})\n", 120 | " new_df = pd.concat([new_df, data])" 121 | ] 122 | }, 123 | { 124 | "cell_type": "code", 125 | "execution_count": 8, 126 | "metadata": { 127 | "collapsed": false 128 | }, 129 | "outputs": [ 130 | { 131 | "data": { 132 | "text/html": [ 133 | "
\n", 134 | "\n", 135 | " \n", 136 | " \n", 137 | " \n", 138 | " \n", 139 | " \n", 140 | " \n", 141 | " \n", 142 | " \n", 143 | " \n", 144 | " \n", 145 | " \n", 146 | " \n", 147 | " \n", 148 | " \n", 149 | " \n", 150 | " \n", 151 | " \n", 152 | " \n", 153 | " \n", 154 | " \n", 155 | " \n", 156 | " \n", 157 | " \n", 158 | " \n", 159 | " \n", 160 | " \n", 161 | " \n", 162 | " \n", 163 | " \n", 164 | " \n", 165 | " \n", 166 | " \n", 167 | " \n", 168 | " \n", 169 | " \n", 170 | " \n", 171 | " \n", 172 | " \n", 173 | " \n", 174 | " \n", 175 | " \n", 176 | " \n", 177 | " \n", 178 | " \n", 179 | " \n", 180 | " \n", 181 | " \n", 182 | " \n", 183 | " \n", 184 | " \n", 185 | " \n", 186 | " \n", 187 | " \n", 188 | " \n", 189 | " \n", 190 | " \n", 191 | " \n", 192 | " \n", 193 | " \n", 194 | " \n", 195 | " \n", 196 | " \n", 197 | " \n", 198 | " \n", 199 | " \n", 200 | " \n", 201 | " \n", 202 | " \n", 203 | " \n", 204 | " \n", 205 | " \n", 206 | " \n", 207 | " \n", 208 | " \n", 209 | " \n", 210 | " \n", 211 | " \n", 212 | " \n", 213 | " \n", 214 | " \n", 215 | " \n", 216 | " \n", 217 | " \n", 218 | " \n", 219 | " \n", 220 | " \n", 221 | " \n", 222 | " \n", 223 | " \n", 224 | " \n", 225 | " \n", 226 | " \n", 227 | " \n", 228 | " \n", 229 | " \n", 230 | " \n", 231 | " \n", 232 | " \n", 233 | " \n", 234 | " \n", 235 | " \n", 236 | " \n", 237 | " \n", 238 | " \n", 239 | " \n", 240 | " \n", 241 | " \n", 242 | " \n", 243 | " \n", 244 | " \n", 245 | " \n", 246 | " \n", 247 | " \n", 248 | " \n", 249 | " \n", 250 | " \n", 251 | " \n", 252 | " \n", 253 | " \n", 254 | " \n", 255 | " \n", 256 | " \n", 257 | " \n", 258 | " \n", 259 | " \n", 260 | " \n", 261 | " \n", 262 | " \n", 263 | " \n", 264 | " \n", 265 | " \n", 266 | " \n", 267 | " \n", 268 | " \n", 269 | " \n", 270 | " \n", 271 | " \n", 272 | " \n", 273 | " \n", 274 | " \n", 275 | " \n", 276 | " \n", 277 | " \n", 278 | " \n", 279 | " \n", 280 | " \n", 281 | " \n", 282 | " \n", 283 | "
DateIDArrivalDepartureEndDateStartDate
02001-02-021922.0ParisFranceNewYorkUnitedState2008-03-102001-02-02
12001-02-031922.0ParisFranceNewYorkUnitedState2008-03-102001-02-02
22001-02-041922.0ParisFranceNewYorkUnitedState2008-03-102001-02-02
32001-02-051922.0ParisFranceNewYorkUnitedState2008-03-102001-02-02
42001-02-061922.0ParisFranceNewYorkUnitedState2008-03-102001-02-02
52001-02-071922.0ParisFranceNewYorkUnitedState2008-03-102001-02-02
62001-02-081922.0ParisFranceNewYorkUnitedState2008-03-102001-02-02
72001-02-091922.0ParisFranceNewYorkUnitedState2008-03-102001-02-02
82001-02-101922.0ParisFranceNewYorkUnitedState2008-03-102001-02-02
92001-02-111922.0ParisFranceNewYorkUnitedState2008-03-102001-02-02
102001-02-121922.0ParisFranceNewYorkUnitedState2008-03-102001-02-02
112001-02-131922.0ParisFranceNewYorkUnitedState2008-03-102001-02-02
122001-02-141922.0ParisFranceNewYorkUnitedState2008-03-102001-02-02
132001-02-151922.0ParisFranceNewYorkUnitedState2008-03-102001-02-02
142001-02-161922.0ParisFranceNewYorkUnitedState2008-03-102001-02-02
\n", 284 | "
" 285 | ], 286 | "text/plain": [ 287 | " Date ID Arrival Departure EndDate StartDate\n", 288 | "0 2001-02-02 1922.0 ParisFrance NewYorkUnitedState 2008-03-10 2001-02-02\n", 289 | "1 2001-02-03 1922.0 ParisFrance NewYorkUnitedState 2008-03-10 2001-02-02\n", 290 | "2 2001-02-04 1922.0 ParisFrance NewYorkUnitedState 2008-03-10 2001-02-02\n", 291 | "3 2001-02-05 1922.0 ParisFrance NewYorkUnitedState 2008-03-10 2001-02-02\n", 292 | "4 2001-02-06 1922.0 ParisFrance NewYorkUnitedState 2008-03-10 2001-02-02\n", 293 | "5 2001-02-07 1922.0 ParisFrance NewYorkUnitedState 2008-03-10 2001-02-02\n", 294 | "6 2001-02-08 1922.0 ParisFrance NewYorkUnitedState 2008-03-10 2001-02-02\n", 295 | "7 2001-02-09 1922.0 ParisFrance NewYorkUnitedState 2008-03-10 2001-02-02\n", 296 | "8 2001-02-10 1922.0 ParisFrance NewYorkUnitedState 2008-03-10 2001-02-02\n", 297 | "9 2001-02-11 1922.0 ParisFrance NewYorkUnitedState 2008-03-10 2001-02-02\n", 298 | "10 2001-02-12 1922.0 ParisFrance NewYorkUnitedState 2008-03-10 2001-02-02\n", 299 | "11 2001-02-13 1922.0 ParisFrance NewYorkUnitedState 2008-03-10 2001-02-02\n", 300 | "12 2001-02-14 1922.0 ParisFrance NewYorkUnitedState 2008-03-10 2001-02-02\n", 301 | "13 2001-02-15 1922.0 ParisFrance NewYorkUnitedState 2008-03-10 2001-02-02\n", 302 | "14 2001-02-16 1922.0 ParisFrance NewYorkUnitedState 2008-03-10 2001-02-02" 303 | ] 304 | }, 305 | "execution_count": 8, 306 | "metadata": {}, 307 | "output_type": "execute_result" 308 | } 309 | ], 310 | "source": [ 311 | "new_df.head(15)" 312 | ] 313 | }, 314 | { 315 | "cell_type": "code", 316 | "execution_count": 9, 317 | "metadata": { 318 | "collapsed": false 319 | }, 320 | "outputs": [], 321 | "source": [ 322 | "# ALTERNATIVE GROUP METHODS\n", 323 | "\n", 324 | "#grouped = df.groupby([lambda x: x.year,'ArCityArCountry'])\n", 325 | "#grouped = new_df.groupby([pd.Grouper(key = 'Date', freq = 'A')])" 326 | ] 327 | }, 328 | { 329 | "cell_type": "code", 330 | "execution_count": 10, 331 | "metadata": { 332 | "collapsed": false 333 | }, 334 | "outputs": [], 335 | "source": [ 336 | "new_df['Year'] = new_df.Date.dt.year\n", 337 | "new_df['Month'] = new_df.Date.dt.month" 338 | ] 339 | }, 340 | { 341 | "cell_type": "code", 342 | "execution_count": 11, 343 | "metadata": { 344 | "collapsed": false 345 | }, 346 | "outputs": [ 347 | { 348 | "data": { 349 | "text/html": [ 350 | "
\n", 351 | "\n", 352 | " \n", 353 | " \n", 354 | " \n", 355 | " \n", 356 | " \n", 357 | " \n", 358 | " \n", 359 | " \n", 360 | " \n", 361 | " \n", 362 | " \n", 363 | " \n", 364 | " \n", 365 | " \n", 366 | " \n", 367 | " \n", 368 | " \n", 369 | " \n", 370 | " \n", 371 | " \n", 372 | " \n", 373 | " \n", 374 | " \n", 375 | " \n", 376 | " \n", 377 | " \n", 378 | " \n", 379 | " \n", 380 | " \n", 381 | " \n", 382 | " \n", 383 | " \n", 384 | " \n", 385 | " \n", 386 | " \n", 387 | " \n", 388 | " \n", 389 | " \n", 390 | " \n", 391 | " \n", 392 | " \n", 393 | " \n", 394 | " \n", 395 | " \n", 396 | " \n", 397 | " \n", 398 | " \n", 399 | " \n", 400 | " \n", 401 | " \n", 402 | " \n", 403 | " \n", 404 | " \n", 405 | " \n", 406 | " \n", 407 | " \n", 408 | " \n", 409 | " \n", 410 | " \n", 411 | " \n", 412 | " \n", 413 | " \n", 414 | " \n", 415 | " \n", 416 | " \n", 417 | " \n", 418 | " \n", 419 | " \n", 420 | " \n", 421 | " \n", 422 | " \n", 423 | " \n", 424 | " \n", 425 | " \n", 426 | " \n", 427 | " \n", 428 | " \n", 429 | " \n", 430 | " \n", 431 | " \n", 432 | " \n", 433 | " \n", 434 | " \n", 435 | " \n", 436 | " \n", 437 | " \n", 438 | " \n", 439 | " \n", 440 | " \n", 441 | " \n", 442 | " \n", 443 | " \n", 444 | " \n", 445 | " \n", 446 | " \n", 447 | " \n", 448 | " \n", 449 | " \n", 450 | " \n", 451 | " \n", 452 | " \n", 453 | " \n", 454 | " \n", 455 | " \n", 456 | " \n", 457 | " \n", 458 | " \n", 459 | " \n", 460 | " \n", 461 | " \n", 462 | " \n", 463 | " \n", 464 | " \n", 465 | " \n", 466 | " \n", 467 | " \n", 468 | " \n", 469 | " \n", 470 | " \n", 471 | " \n", 472 | " \n", 473 | " \n", 474 | " \n", 475 | " \n", 476 | " \n", 477 | " \n", 478 | " \n", 479 | " \n", 480 | " \n", 481 | " \n", 482 | " \n", 483 | " \n", 484 | " \n", 485 | " \n", 486 | " \n", 487 | " \n", 488 | " \n", 489 | " \n", 490 | " \n", 491 | " \n", 492 | " \n", 493 | " \n", 494 | " \n", 495 | " \n", 496 | " \n", 497 | " \n", 498 | " \n", 499 | " \n", 500 | " \n", 501 | " \n", 502 | " \n", 503 | " \n", 504 | " \n", 505 | " \n", 506 | " \n", 507 | " \n", 508 | " \n", 509 | " \n", 510 | " \n", 511 | " \n", 512 | " \n", 513 | " \n", 514 | " \n", 515 | " \n", 516 | " \n", 517 | " \n", 518 | " \n", 519 | " \n", 520 | " \n", 521 | " \n", 522 | " \n", 523 | " \n", 524 | " \n", 525 | " \n", 526 | " \n", 527 | " \n", 528 | " \n", 529 | " \n", 530 | " \n", 531 | " \n", 532 | " \n", 533 | " \n", 534 | " \n", 535 | " \n", 536 | " \n", 537 | " \n", 538 | " \n", 539 | " \n", 540 | " \n", 541 | " \n", 542 | " \n", 543 | " \n", 544 | " \n", 545 | " \n", 546 | " \n", 547 | " \n", 548 | " \n", 549 | " \n", 550 | " \n", 551 | " \n", 552 | " \n", 553 | " \n", 554 | " \n", 555 | " \n", 556 | " \n", 557 | " \n", 558 | " \n", 559 | " \n", 560 | " \n", 561 | " \n", 562 | " \n", 563 | " \n", 564 | " \n", 565 | " \n", 566 | " \n", 567 | " \n", 568 | " \n", 569 | " \n", 570 | " \n", 571 | " \n", 572 | " \n", 573 | " \n", 574 | " \n", 575 | " \n", 576 | " \n", 577 | " \n", 578 | " \n", 579 | " \n", 580 | " \n", 581 | " \n", 582 | " \n", 583 | " \n", 584 | " \n", 585 | " \n", 586 | " \n", 587 | "
DateIDArrivalDepartureEndDateStartDateYearMonth
02001-02-021922.0ParisFranceNewYorkUnitedState2008-03-102001-02-0220012
12001-02-031922.0ParisFranceNewYorkUnitedState2008-03-102001-02-0220012
22001-02-041922.0ParisFranceNewYorkUnitedState2008-03-102001-02-0220012
32001-02-051922.0ParisFranceNewYorkUnitedState2008-03-102001-02-0220012
42001-02-061922.0ParisFranceNewYorkUnitedState2008-03-102001-02-0220012
52001-02-071922.0ParisFranceNewYorkUnitedState2008-03-102001-02-0220012
62001-02-081922.0ParisFranceNewYorkUnitedState2008-03-102001-02-0220012
72001-02-091922.0ParisFranceNewYorkUnitedState2008-03-102001-02-0220012
82001-02-101922.0ParisFranceNewYorkUnitedState2008-03-102001-02-0220012
92001-02-111922.0ParisFranceNewYorkUnitedState2008-03-102001-02-0220012
102001-02-121922.0ParisFranceNewYorkUnitedState2008-03-102001-02-0220012
112001-02-131922.0ParisFranceNewYorkUnitedState2008-03-102001-02-0220012
122001-02-141922.0ParisFranceNewYorkUnitedState2008-03-102001-02-0220012
132001-02-151922.0ParisFranceNewYorkUnitedState2008-03-102001-02-0220012
142001-02-161922.0ParisFranceNewYorkUnitedState2008-03-102001-02-0220012
152001-02-171922.0ParisFranceNewYorkUnitedState2008-03-102001-02-0220012
162001-02-181922.0ParisFranceNewYorkUnitedState2008-03-102001-02-0220012
172001-02-191922.0ParisFranceNewYorkUnitedState2008-03-102001-02-0220012
182001-02-201922.0ParisFranceNewYorkUnitedState2008-03-102001-02-0220012
192001-02-211922.0ParisFranceNewYorkUnitedState2008-03-102001-02-0220012
\n", 588 | "
" 589 | ], 590 | "text/plain": [ 591 | " Date ID Arrival Departure EndDate StartDate \\\n", 592 | "0 2001-02-02 1922.0 ParisFrance NewYorkUnitedState 2008-03-10 2001-02-02 \n", 593 | "1 2001-02-03 1922.0 ParisFrance NewYorkUnitedState 2008-03-10 2001-02-02 \n", 594 | "2 2001-02-04 1922.0 ParisFrance NewYorkUnitedState 2008-03-10 2001-02-02 \n", 595 | "3 2001-02-05 1922.0 ParisFrance NewYorkUnitedState 2008-03-10 2001-02-02 \n", 596 | "4 2001-02-06 1922.0 ParisFrance NewYorkUnitedState 2008-03-10 2001-02-02 \n", 597 | "5 2001-02-07 1922.0 ParisFrance NewYorkUnitedState 2008-03-10 2001-02-02 \n", 598 | "6 2001-02-08 1922.0 ParisFrance NewYorkUnitedState 2008-03-10 2001-02-02 \n", 599 | "7 2001-02-09 1922.0 ParisFrance NewYorkUnitedState 2008-03-10 2001-02-02 \n", 600 | "8 2001-02-10 1922.0 ParisFrance NewYorkUnitedState 2008-03-10 2001-02-02 \n", 601 | "9 2001-02-11 1922.0 ParisFrance NewYorkUnitedState 2008-03-10 2001-02-02 \n", 602 | "10 2001-02-12 1922.0 ParisFrance NewYorkUnitedState 2008-03-10 2001-02-02 \n", 603 | "11 2001-02-13 1922.0 ParisFrance NewYorkUnitedState 2008-03-10 2001-02-02 \n", 604 | "12 2001-02-14 1922.0 ParisFrance NewYorkUnitedState 2008-03-10 2001-02-02 \n", 605 | "13 2001-02-15 1922.0 ParisFrance NewYorkUnitedState 2008-03-10 2001-02-02 \n", 606 | "14 2001-02-16 1922.0 ParisFrance NewYorkUnitedState 2008-03-10 2001-02-02 \n", 607 | "15 2001-02-17 1922.0 ParisFrance NewYorkUnitedState 2008-03-10 2001-02-02 \n", 608 | "16 2001-02-18 1922.0 ParisFrance NewYorkUnitedState 2008-03-10 2001-02-02 \n", 609 | "17 2001-02-19 1922.0 ParisFrance NewYorkUnitedState 2008-03-10 2001-02-02 \n", 610 | "18 2001-02-20 1922.0 ParisFrance NewYorkUnitedState 2008-03-10 2001-02-02 \n", 611 | "19 2001-02-21 1922.0 ParisFrance NewYorkUnitedState 2008-03-10 2001-02-02 \n", 612 | "\n", 613 | " Year Month \n", 614 | "0 2001 2 \n", 615 | "1 2001 2 \n", 616 | "2 2001 2 \n", 617 | "3 2001 2 \n", 618 | "4 2001 2 \n", 619 | "5 2001 2 \n", 620 | "6 2001 2 \n", 621 | "7 2001 2 \n", 622 | "8 2001 2 \n", 623 | "9 2001 2 \n", 624 | "10 2001 2 \n", 625 | "11 2001 2 \n", 626 | "12 2001 2 \n", 627 | "13 2001 2 \n", 628 | "14 2001 2 \n", 629 | "15 2001 2 \n", 630 | "16 2001 2 \n", 631 | "17 2001 2 \n", 632 | "18 2001 2 \n", 633 | "19 2001 2 " 634 | ] 635 | }, 636 | "execution_count": 11, 637 | "metadata": {}, 638 | "output_type": "execute_result" 639 | } 640 | ], 641 | "source": [ 642 | "new_df.head(20)" 643 | ] 644 | }, 645 | { 646 | "cell_type": "code", 647 | "execution_count": 12, 648 | "metadata": { 649 | "collapsed": false 650 | }, 651 | "outputs": [], 652 | "source": [ 653 | "grouped = new_df[['Departure','Year','ID']].groupby(['Year','Departure']).count()" 654 | ] 655 | }, 656 | { 657 | "cell_type": "code", 658 | "execution_count": 13, 659 | "metadata": { 660 | "collapsed": false 661 | }, 662 | "outputs": [ 663 | { 664 | "data": { 665 | "text/html": [ 666 | "
\n", 667 | "\n", 668 | " \n", 669 | " \n", 670 | " \n", 671 | " \n", 672 | " \n", 673 | " \n", 674 | " \n", 675 | " \n", 676 | " \n", 677 | " \n", 678 | " \n", 679 | " \n", 680 | " \n", 681 | " \n", 682 | " \n", 683 | " \n", 684 | " \n", 685 | " \n", 686 | " \n", 687 | " \n", 688 | " \n", 689 | " \n", 690 | " \n", 691 | " \n", 692 | " \n", 693 | " \n", 694 | " \n", 695 | " \n", 696 | " \n", 697 | " \n", 698 | " \n", 699 | " \n", 700 | " \n", 701 | " \n", 702 | " \n", 703 | " \n", 704 | " \n", 705 | " \n", 706 | " \n", 707 | " \n", 708 | " \n", 709 | " \n", 710 | " \n", 711 | " \n", 712 | " \n", 713 | " \n", 714 | " \n", 715 | " \n", 716 | " \n", 717 | " \n", 718 | " \n", 719 | " \n", 720 | " \n", 721 | " \n", 722 | " \n", 723 | " \n", 724 | " \n", 725 | " \n", 726 | " \n", 727 | " \n", 728 | " \n", 729 | " \n", 730 | " \n", 731 | " \n", 732 | " \n", 733 | " \n", 734 | " \n", 735 | " \n", 736 | " \n", 737 | " \n", 738 | " \n", 739 | " \n", 740 | " \n", 741 | " \n", 742 | " \n", 743 | " \n", 744 | " \n", 745 | " \n", 746 | " \n", 747 | " \n", 748 | " \n", 749 | " \n", 750 | " \n", 751 | " \n", 752 | " \n", 753 | " \n", 754 | " \n", 755 | " \n", 756 | " \n", 757 | " \n", 758 | " \n", 759 | " \n", 760 | " \n", 761 | " \n", 762 | " \n", 763 | " \n", 764 | " \n", 765 | " \n", 766 | " \n", 767 | " \n", 768 | " \n", 769 | " \n", 770 | " \n", 771 | " \n", 772 | " \n", 773 | " \n", 774 | "
ID
Year20012002200320042005200620072008200920102011
Departure
BeijingChinaNaNNaNNaNNaNNaNNaNNaNNaN331.0365.034.0
CaliforniaUnitedStateNaN303.063.0NaNNaNNaNNaNNaNNaNNaNNaN
LagosNigeria32.0NaNNaNNaNNaNNaNNaNNaNNaNNaNNaN
NewYorkUnitedState333.0365.0365.0366.0365.0365.0365.0101.0365.0365.034.0
ParisFrance32.0NaNNaNNaNNaNNaNNaNNaNNaNNaNNaN
\n", 775 | "
" 776 | ], 777 | "text/plain": [ 778 | " ID \\\n", 779 | "Year 2001 2002 2003 2004 2005 2006 2007 2008 \n", 780 | "Departure \n", 781 | "BeijingChina NaN NaN NaN NaN NaN NaN NaN NaN \n", 782 | "CaliforniaUnitedState NaN 303.0 63.0 NaN NaN NaN NaN NaN \n", 783 | "LagosNigeria 32.0 NaN NaN NaN NaN NaN NaN NaN \n", 784 | "NewYorkUnitedState 333.0 365.0 365.0 366.0 365.0 365.0 365.0 101.0 \n", 785 | "ParisFrance 32.0 NaN NaN NaN NaN NaN NaN NaN \n", 786 | "\n", 787 | " \n", 788 | "Year 2009 2010 2011 \n", 789 | "Departure \n", 790 | "BeijingChina 331.0 365.0 34.0 \n", 791 | "CaliforniaUnitedState NaN NaN NaN \n", 792 | "LagosNigeria NaN NaN NaN \n", 793 | "NewYorkUnitedState 365.0 365.0 34.0 \n", 794 | "ParisFrance NaN NaN NaN " 795 | ] 796 | }, 797 | "execution_count": 13, 798 | "metadata": {}, 799 | "output_type": "execute_result" 800 | } 801 | ], 802 | "source": [ 803 | "grouped.unstack(0)" 804 | ] 805 | } 806 | ], 807 | "metadata": { 808 | "kernelspec": { 809 | "display_name": "Python 2", 810 | "language": "python", 811 | "name": "python2" 812 | }, 813 | "language_info": { 814 | "codemirror_mode": { 815 | "name": "ipython", 816 | "version": 2 817 | }, 818 | "file_extension": ".py", 819 | "mimetype": "text/x-python", 820 | "name": "python", 821 | "nbconvert_exporter": "python", 822 | "pygments_lexer": "ipython2", 823 | "version": "2.7.11" 824 | } 825 | }, 826 | "nbformat": 4, 827 | "nbformat_minor": 0 828 | } 829 | -------------------------------------------------------------------------------- /Working with Text Data.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "![alt text](http://pandas.pydata.org/_static/pandas_logo.png)" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "

PANDAS TABLE OF CONTENTS

\n", 15 | "\n", 16 | "## [Working with Text Data](http://pandas.pydata.org/pandas-docs/stable/text.html) " 17 | ] 18 | }, 19 | { 20 | "cell_type": "markdown", 21 | "metadata": {}, 22 | "source": [ 23 | "- [Splitting-and-Replacing-Strings](#Splitting-and-Replacing-Strings)\n", 24 | "- [Indexing with .str](#Indexing-with-.str)\n", 25 | "- [Extracting Substrings](#Extracting-Substrings)\n", 26 | "- [Testing for Strings that Match or Contain a Pattern](#Testing-for-Strings-that-Match-or-Contain-a-Pattern)\n", 27 | "- [Creating Indicator Variables](#Creating-Indicator-Variables)\n", 28 | "- [Method Summary](#Method-Summary)\n" 29 | ] 30 | }, 31 | { 32 | "cell_type": "code", 33 | "execution_count": 1, 34 | "metadata": { 35 | "collapsed": true 36 | }, 37 | "outputs": [], 38 | "source": [ 39 | "import pandas as pd\n", 40 | "import numpy as np" 41 | ] 42 | }, 43 | { 44 | "cell_type": "markdown", 45 | "metadata": {}, 46 | "source": [ 47 | "Series and Index are equipped with a set of string processing methods that make it easy to operate on each element of the array. Perhaps most importantly, these methods exclude missing/NA values automatically. These are accessed via the str attribute and generally have names matching the equivalent (scalar) built-in string methods:" 48 | ] 49 | }, 50 | { 51 | "cell_type": "code", 52 | "execution_count": 2, 53 | "metadata": { 54 | "collapsed": true 55 | }, 56 | "outputs": [], 57 | "source": [ 58 | "s = pd.Series(['A', 'B', 'C', 'Aaba', 'Baca', np.nan, 'CABA', 'dog', 'cat'])" 59 | ] 60 | }, 61 | { 62 | "cell_type": "code", 63 | "execution_count": 3, 64 | "metadata": { 65 | "collapsed": false 66 | }, 67 | "outputs": [ 68 | { 69 | "data": { 70 | "text/plain": [ 71 | "0 A\n", 72 | "1 B\n", 73 | "2 C\n", 74 | "3 Aaba\n", 75 | "4 Baca\n", 76 | "5 NaN\n", 77 | "6 CABA\n", 78 | "7 dog\n", 79 | "8 cat\n", 80 | "dtype: object" 81 | ] 82 | }, 83 | "execution_count": 3, 84 | "metadata": {}, 85 | "output_type": "execute_result" 86 | } 87 | ], 88 | "source": [ 89 | "s" 90 | ] 91 | }, 92 | { 93 | "cell_type": "code", 94 | "execution_count": 4, 95 | "metadata": { 96 | "collapsed": false 97 | }, 98 | "outputs": [ 99 | { 100 | "data": { 101 | "text/plain": [ 102 | "0 a\n", 103 | "1 b\n", 104 | "2 c\n", 105 | "3 aaba\n", 106 | "4 baca\n", 107 | "5 NaN\n", 108 | "6 caba\n", 109 | "7 dog\n", 110 | "8 cat\n", 111 | "dtype: object" 112 | ] 113 | }, 114 | "execution_count": 4, 115 | "metadata": {}, 116 | "output_type": "execute_result" 117 | } 118 | ], 119 | "source": [ 120 | "s.str.lower()" 121 | ] 122 | }, 123 | { 124 | "cell_type": "code", 125 | "execution_count": 5, 126 | "metadata": { 127 | "collapsed": false 128 | }, 129 | "outputs": [ 130 | { 131 | "data": { 132 | "text/plain": [ 133 | "0 A\n", 134 | "1 B\n", 135 | "2 C\n", 136 | "3 AABA\n", 137 | "4 BACA\n", 138 | "5 NaN\n", 139 | "6 CABA\n", 140 | "7 DOG\n", 141 | "8 CAT\n", 142 | "dtype: object" 143 | ] 144 | }, 145 | "execution_count": 5, 146 | "metadata": {}, 147 | "output_type": "execute_result" 148 | } 149 | ], 150 | "source": [ 151 | "s.str.upper()" 152 | ] 153 | }, 154 | { 155 | "cell_type": "code", 156 | "execution_count": 6, 157 | "metadata": { 158 | "collapsed": false 159 | }, 160 | "outputs": [ 161 | { 162 | "data": { 163 | "text/plain": [ 164 | "0 1.0\n", 165 | "1 1.0\n", 166 | "2 1.0\n", 167 | "3 4.0\n", 168 | "4 4.0\n", 169 | "5 NaN\n", 170 | "6 4.0\n", 171 | "7 3.0\n", 172 | "8 3.0\n", 173 | "dtype: float64" 174 | ] 175 | }, 176 | "execution_count": 6, 177 | "metadata": {}, 178 | "output_type": "execute_result" 179 | } 180 | ], 181 | "source": [ 182 | "s.str.len()" 183 | ] 184 | }, 185 | { 186 | "cell_type": "code", 187 | "execution_count": 7, 188 | "metadata": { 189 | "collapsed": true 190 | }, 191 | "outputs": [], 192 | "source": [ 193 | "idx = pd.Index([' jack', 'jill ', ' jesse ', 'frank'])" 194 | ] 195 | }, 196 | { 197 | "cell_type": "code", 198 | "execution_count": 8, 199 | "metadata": { 200 | "collapsed": false 201 | }, 202 | "outputs": [ 203 | { 204 | "data": { 205 | "text/plain": [ 206 | "Index([u' jack', u'jill ', u' jesse ', u'frank'], dtype='object')" 207 | ] 208 | }, 209 | "execution_count": 8, 210 | "metadata": {}, 211 | "output_type": "execute_result" 212 | } 213 | ], 214 | "source": [ 215 | "idx" 216 | ] 217 | }, 218 | { 219 | "cell_type": "code", 220 | "execution_count": 9, 221 | "metadata": { 222 | "collapsed": false 223 | }, 224 | "outputs": [ 225 | { 226 | "data": { 227 | "text/plain": [ 228 | "Index([u'jack', u'jill', u'jesse', u'frank'], dtype='object')" 229 | ] 230 | }, 231 | "execution_count": 9, 232 | "metadata": {}, 233 | "output_type": "execute_result" 234 | } 235 | ], 236 | "source": [ 237 | "idx.str.strip()" 238 | ] 239 | }, 240 | { 241 | "cell_type": "code", 242 | "execution_count": 10, 243 | "metadata": { 244 | "collapsed": false 245 | }, 246 | "outputs": [ 247 | { 248 | "data": { 249 | "text/plain": [ 250 | "Index([u'jack', u'jill ', u'jesse ', u'frank'], dtype='object')" 251 | ] 252 | }, 253 | "execution_count": 10, 254 | "metadata": {}, 255 | "output_type": "execute_result" 256 | } 257 | ], 258 | "source": [ 259 | "idx.str.lstrip()" 260 | ] 261 | }, 262 | { 263 | "cell_type": "code", 264 | "execution_count": 11, 265 | "metadata": { 266 | "collapsed": false 267 | }, 268 | "outputs": [ 269 | { 270 | "data": { 271 | "text/plain": [ 272 | "Index([u' jack', u'jill', u' jesse', u'frank'], dtype='object')" 273 | ] 274 | }, 275 | "execution_count": 11, 276 | "metadata": {}, 277 | "output_type": "execute_result" 278 | } 279 | ], 280 | "source": [ 281 | "idx.str.rstrip()" 282 | ] 283 | }, 284 | { 285 | "cell_type": "markdown", 286 | "metadata": {}, 287 | "source": [ 288 | "The string methods on Index are especially useful for cleaning up or transforming DataFrame columns. For instance, you may have columns with leading or trailing whitespace:" 289 | ] 290 | }, 291 | { 292 | "cell_type": "code", 293 | "execution_count": 12, 294 | "metadata": { 295 | "collapsed": false 296 | }, 297 | "outputs": [], 298 | "source": [ 299 | "df = pd.DataFrame(np.random.randn(3, 2), columns=[' Column A ', ' Column B '],\n", 300 | " index=range(3))" 301 | ] 302 | }, 303 | { 304 | "cell_type": "code", 305 | "execution_count": 13, 306 | "metadata": { 307 | "collapsed": false 308 | }, 309 | "outputs": [ 310 | { 311 | "data": { 312 | "text/html": [ 313 | "
\n", 314 | "\n", 315 | " \n", 316 | " \n", 317 | " \n", 318 | " \n", 319 | " \n", 320 | " \n", 321 | " \n", 322 | " \n", 323 | " \n", 324 | " \n", 325 | " \n", 326 | " \n", 327 | " \n", 328 | " \n", 329 | " \n", 330 | " \n", 331 | " \n", 332 | " \n", 333 | " \n", 334 | " \n", 335 | " \n", 336 | " \n", 337 | " \n", 338 | " \n", 339 | "
Column AColumn B
0-0.343147-0.240406
1-1.727499-0.914029
2-0.245533-0.713243
\n", 340 | "
" 341 | ], 342 | "text/plain": [ 343 | " Column A Column B \n", 344 | "0 -0.343147 -0.240406\n", 345 | "1 -1.727499 -0.914029\n", 346 | "2 -0.245533 -0.713243" 347 | ] 348 | }, 349 | "execution_count": 13, 350 | "metadata": {}, 351 | "output_type": "execute_result" 352 | } 353 | ], 354 | "source": [ 355 | "df" 356 | ] 357 | }, 358 | { 359 | "cell_type": "markdown", 360 | "metadata": {}, 361 | "source": [ 362 | "Since df.columns is an Index object, we can use the .str accessor" 363 | ] 364 | }, 365 | { 366 | "cell_type": "code", 367 | "execution_count": 14, 368 | "metadata": { 369 | "collapsed": false 370 | }, 371 | "outputs": [ 372 | { 373 | "data": { 374 | "text/plain": [ 375 | "Index([u'Column A', u'Column B'], dtype='object')" 376 | ] 377 | }, 378 | "execution_count": 14, 379 | "metadata": {}, 380 | "output_type": "execute_result" 381 | } 382 | ], 383 | "source": [ 384 | "df.columns.str.strip()" 385 | ] 386 | }, 387 | { 388 | "cell_type": "code", 389 | "execution_count": 15, 390 | "metadata": { 391 | "collapsed": false 392 | }, 393 | "outputs": [ 394 | { 395 | "data": { 396 | "text/plain": [ 397 | "Index([u' column a ', u' column b '], dtype='object')" 398 | ] 399 | }, 400 | "execution_count": 15, 401 | "metadata": {}, 402 | "output_type": "execute_result" 403 | } 404 | ], 405 | "source": [ 406 | "df.columns.str.lower()" 407 | ] 408 | }, 409 | { 410 | "cell_type": "markdown", 411 | "metadata": {}, 412 | "source": [ 413 | "These string methods can then be used to clean up the columns as needed. Here we are removing leading and trailing whitespaces, lowercasing all names, and replacing any remaining whitespaces with underscores:" 414 | ] 415 | }, 416 | { 417 | "cell_type": "code", 418 | "execution_count": 16, 419 | "metadata": { 420 | "collapsed": true 421 | }, 422 | "outputs": [], 423 | "source": [ 424 | "df.columns = df.columns.str.strip().str.lower().str.replace(' ', '_')" 425 | ] 426 | }, 427 | { 428 | "cell_type": "code", 429 | "execution_count": 17, 430 | "metadata": { 431 | "collapsed": false 432 | }, 433 | "outputs": [ 434 | { 435 | "data": { 436 | "text/html": [ 437 | "
\n", 438 | "\n", 439 | " \n", 440 | " \n", 441 | " \n", 442 | " \n", 443 | " \n", 444 | " \n", 445 | " \n", 446 | " \n", 447 | " \n", 448 | " \n", 449 | " \n", 450 | " \n", 451 | " \n", 452 | " \n", 453 | " \n", 454 | " \n", 455 | " \n", 456 | " \n", 457 | " \n", 458 | " \n", 459 | " \n", 460 | " \n", 461 | " \n", 462 | " \n", 463 | "
column_acolumn_b
0-0.343147-0.240406
1-1.727499-0.914029
2-0.245533-0.713243
\n", 464 | "
" 465 | ], 466 | "text/plain": [ 467 | " column_a column_b\n", 468 | "0 -0.343147 -0.240406\n", 469 | "1 -1.727499 -0.914029\n", 470 | "2 -0.245533 -0.713243" 471 | ] 472 | }, 473 | "execution_count": 17, 474 | "metadata": {}, 475 | "output_type": "execute_result" 476 | } 477 | ], 478 | "source": [ 479 | "df" 480 | ] 481 | }, 482 | { 483 | "cell_type": "markdown", 484 | "metadata": {}, 485 | "source": [ 486 | "**Note:** If you have a Series where lots of elements are repeated (i.e. the number of unique elements in the Series is a lot smaller than the length of the Series), it can be faster to convert the original Series to one of type category and then use .str. or .dt. on that. The performance difference comes from the fact that, for Series of type category, the string operations are done on the .categories and not on each element of the Series.\n", 487 | "Please note that a Series of type category with string .categories has some limitations in comparison of Series of type string (e.g. you can’t add strings to each other: s + \" \" + s won’t work if s is a Series of type category). Also, .str methods which operate on elements of type list are not available on such a Series." 488 | ] 489 | }, 490 | { 491 | "cell_type": "markdown", 492 | "metadata": {}, 493 | "source": [ 494 | "## Splitting and Replacing Strings" 495 | ] 496 | }, 497 | { 498 | "cell_type": "markdown", 499 | "metadata": {}, 500 | "source": [ 501 | "Methods like split return a Series of lists:" 502 | ] 503 | }, 504 | { 505 | "cell_type": "code", 506 | "execution_count": 18, 507 | "metadata": { 508 | "collapsed": true 509 | }, 510 | "outputs": [], 511 | "source": [ 512 | "s2 = pd.Series(['a_b_c', 'c_d_e', np.nan, 'f_g_h'])" 513 | ] 514 | }, 515 | { 516 | "cell_type": "code", 517 | "execution_count": 19, 518 | "metadata": { 519 | "collapsed": false 520 | }, 521 | "outputs": [ 522 | { 523 | "data": { 524 | "text/plain": [ 525 | "0 a_b_c\n", 526 | "1 c_d_e\n", 527 | "2 NaN\n", 528 | "3 f_g_h\n", 529 | "dtype: object" 530 | ] 531 | }, 532 | "execution_count": 19, 533 | "metadata": {}, 534 | "output_type": "execute_result" 535 | } 536 | ], 537 | "source": [ 538 | "s2" 539 | ] 540 | }, 541 | { 542 | "cell_type": "code", 543 | "execution_count": 20, 544 | "metadata": { 545 | "collapsed": false 546 | }, 547 | "outputs": [ 548 | { 549 | "data": { 550 | "text/plain": [ 551 | "0 [a, b, c]\n", 552 | "1 [c, d, e]\n", 553 | "2 NaN\n", 554 | "3 [f, g, h]\n", 555 | "dtype: object" 556 | ] 557 | }, 558 | "execution_count": 20, 559 | "metadata": {}, 560 | "output_type": "execute_result" 561 | } 562 | ], 563 | "source": [ 564 | "s2.str.split('_')" 565 | ] 566 | }, 567 | { 568 | "cell_type": "markdown", 569 | "metadata": {}, 570 | "source": [ 571 | "Elements in the split lists can be accessed using get or [] notation:" 572 | ] 573 | }, 574 | { 575 | "cell_type": "code", 576 | "execution_count": 21, 577 | "metadata": { 578 | "collapsed": false 579 | }, 580 | "outputs": [ 581 | { 582 | "data": { 583 | "text/plain": [ 584 | "0 b\n", 585 | "1 d\n", 586 | "2 NaN\n", 587 | "3 g\n", 588 | "dtype: object" 589 | ] 590 | }, 591 | "execution_count": 21, 592 | "metadata": {}, 593 | "output_type": "execute_result" 594 | } 595 | ], 596 | "source": [ 597 | "s2.str.split('_').str.get(1)" 598 | ] 599 | }, 600 | { 601 | "cell_type": "code", 602 | "execution_count": 22, 603 | "metadata": { 604 | "collapsed": false 605 | }, 606 | "outputs": [ 607 | { 608 | "data": { 609 | "text/plain": [ 610 | "0 b\n", 611 | "1 d\n", 612 | "2 NaN\n", 613 | "3 g\n", 614 | "dtype: object" 615 | ] 616 | }, 617 | "execution_count": 22, 618 | "metadata": {}, 619 | "output_type": "execute_result" 620 | } 621 | ], 622 | "source": [ 623 | "s2.str.split('_').str[1]" 624 | ] 625 | }, 626 | { 627 | "cell_type": "markdown", 628 | "metadata": {}, 629 | "source": [ 630 | "Easy to expand this to return a DataFrame using expand." 631 | ] 632 | }, 633 | { 634 | "cell_type": "code", 635 | "execution_count": 23, 636 | "metadata": { 637 | "collapsed": false 638 | }, 639 | "outputs": [ 640 | { 641 | "data": { 642 | "text/html": [ 643 | "
\n", 644 | "\n", 645 | " \n", 646 | " \n", 647 | " \n", 648 | " \n", 649 | " \n", 650 | " \n", 651 | " \n", 652 | " \n", 653 | " \n", 654 | " \n", 655 | " \n", 656 | " \n", 657 | " \n", 658 | " \n", 659 | " \n", 660 | " \n", 661 | " \n", 662 | " \n", 663 | " \n", 664 | " \n", 665 | " \n", 666 | " \n", 667 | " \n", 668 | " \n", 669 | " \n", 670 | " \n", 671 | " \n", 672 | " \n", 673 | " \n", 674 | " \n", 675 | " \n", 676 | " \n", 677 | " \n", 678 | " \n", 679 | "
012
0abc
1cde
2NaNNoneNone
3fgh
\n", 680 | "
" 681 | ], 682 | "text/plain": [ 683 | " 0 1 2\n", 684 | "0 a b c\n", 685 | "1 c d e\n", 686 | "2 NaN None None\n", 687 | "3 f g h" 688 | ] 689 | }, 690 | "execution_count": 23, 691 | "metadata": {}, 692 | "output_type": "execute_result" 693 | } 694 | ], 695 | "source": [ 696 | "s2.str.split('_', expand=True)" 697 | ] 698 | }, 699 | { 700 | "cell_type": "markdown", 701 | "metadata": {}, 702 | "source": [ 703 | "It is also possible to limit the number of splits:" 704 | ] 705 | }, 706 | { 707 | "cell_type": "code", 708 | "execution_count": 24, 709 | "metadata": { 710 | "collapsed": false 711 | }, 712 | "outputs": [ 713 | { 714 | "data": { 715 | "text/html": [ 716 | "
\n", 717 | "\n", 718 | " \n", 719 | " \n", 720 | " \n", 721 | " \n", 722 | " \n", 723 | " \n", 724 | " \n", 725 | " \n", 726 | " \n", 727 | " \n", 728 | " \n", 729 | " \n", 730 | " \n", 731 | " \n", 732 | " \n", 733 | " \n", 734 | " \n", 735 | " \n", 736 | " \n", 737 | " \n", 738 | " \n", 739 | " \n", 740 | " \n", 741 | " \n", 742 | " \n", 743 | " \n", 744 | " \n", 745 | " \n", 746 | " \n", 747 | "
01
0ab_c
1cd_e
2NaNNone
3fg_h
\n", 748 | "
" 749 | ], 750 | "text/plain": [ 751 | " 0 1\n", 752 | "0 a b_c\n", 753 | "1 c d_e\n", 754 | "2 NaN None\n", 755 | "3 f g_h" 756 | ] 757 | }, 758 | "execution_count": 24, 759 | "metadata": {}, 760 | "output_type": "execute_result" 761 | } 762 | ], 763 | "source": [ 764 | "s2.str.split('_', expand=True, n=1)" 765 | ] 766 | }, 767 | { 768 | "cell_type": "markdown", 769 | "metadata": {}, 770 | "source": [ 771 | "rsplit is similar to split except it works in the reverse direction, i.e., from the end of the string to the beginning of the string:" 772 | ] 773 | }, 774 | { 775 | "cell_type": "code", 776 | "execution_count": 25, 777 | "metadata": { 778 | "collapsed": false 779 | }, 780 | "outputs": [ 781 | { 782 | "data": { 783 | "text/html": [ 784 | "
\n", 785 | "\n", 786 | " \n", 787 | " \n", 788 | " \n", 789 | " \n", 790 | " \n", 791 | " \n", 792 | " \n", 793 | " \n", 794 | " \n", 795 | " \n", 796 | " \n", 797 | " \n", 798 | " \n", 799 | " \n", 800 | " \n", 801 | " \n", 802 | " \n", 803 | " \n", 804 | " \n", 805 | " \n", 806 | " \n", 807 | " \n", 808 | " \n", 809 | " \n", 810 | " \n", 811 | " \n", 812 | " \n", 813 | " \n", 814 | " \n", 815 | "
01
0a_bc
1c_de
2NaNNone
3f_gh
\n", 816 | "
" 817 | ], 818 | "text/plain": [ 819 | " 0 1\n", 820 | "0 a_b c\n", 821 | "1 c_d e\n", 822 | "2 NaN None\n", 823 | "3 f_g h" 824 | ] 825 | }, 826 | "execution_count": 25, 827 | "metadata": {}, 828 | "output_type": "execute_result" 829 | } 830 | ], 831 | "source": [ 832 | "s2.str.rsplit('_', expand=True, n=1)" 833 | ] 834 | }, 835 | { 836 | "cell_type": "markdown", 837 | "metadata": {}, 838 | "source": [ 839 | "Methods like replace and findall take [regular expressions](https://docs.python.org/2/library/re.html), too:" 840 | ] 841 | }, 842 | { 843 | "cell_type": "code", 844 | "execution_count": 26, 845 | "metadata": { 846 | "collapsed": true 847 | }, 848 | "outputs": [], 849 | "source": [ 850 | "s3 = pd.Series(['A', 'B', 'C', 'Aaba', 'Baca',\n", 851 | " '', np.nan, 'CABA', 'dog', 'cat'])" 852 | ] 853 | }, 854 | { 855 | "cell_type": "code", 856 | "execution_count": 27, 857 | "metadata": { 858 | "collapsed": false 859 | }, 860 | "outputs": [ 861 | { 862 | "data": { 863 | "text/plain": [ 864 | "0 A\n", 865 | "1 B\n", 866 | "2 C\n", 867 | "3 Aaba\n", 868 | "4 Baca\n", 869 | "5 \n", 870 | "6 NaN\n", 871 | "7 CABA\n", 872 | "8 dog\n", 873 | "9 cat\n", 874 | "dtype: object" 875 | ] 876 | }, 877 | "execution_count": 27, 878 | "metadata": {}, 879 | "output_type": "execute_result" 880 | } 881 | ], 882 | "source": [ 883 | "s3" 884 | ] 885 | }, 886 | { 887 | "cell_type": "code", 888 | "execution_count": 28, 889 | "metadata": { 890 | "collapsed": false 891 | }, 892 | "outputs": [ 893 | { 894 | "data": { 895 | "text/plain": [ 896 | "0 A\n", 897 | "1 B\n", 898 | "2 C\n", 899 | "3 XX-XX ba\n", 900 | "4 XX-XX ca\n", 901 | "5 \n", 902 | "6 NaN\n", 903 | "7 XX-XX BA\n", 904 | "8 XX-XX \n", 905 | "9 XX-XX t\n", 906 | "dtype: object" 907 | ] 908 | }, 909 | "execution_count": 28, 910 | "metadata": {}, 911 | "output_type": "execute_result" 912 | } 913 | ], 914 | "source": [ 915 | " s3.str.replace('^.a|dog', 'XX-XX ', case=False)" 916 | ] 917 | }, 918 | { 919 | "cell_type": "markdown", 920 | "metadata": {}, 921 | "source": [ 922 | "Some caution must be taken to keep regular expressions in mind! For example, the following code will cause trouble because of the regular expression meaning of $:" 923 | ] 924 | }, 925 | { 926 | "cell_type": "code", 927 | "execution_count": 29, 928 | "metadata": { 929 | "collapsed": true 930 | }, 931 | "outputs": [], 932 | "source": [ 933 | "# Consider the following badly formatted financial data\n", 934 | "dollars = pd.Series(['12', '-$10', '$10,000'])" 935 | ] 936 | }, 937 | { 938 | "cell_type": "code", 939 | "execution_count": 30, 940 | "metadata": { 941 | "collapsed": false 942 | }, 943 | "outputs": [ 944 | { 945 | "data": { 946 | "text/plain": [ 947 | "0 12\n", 948 | "1 -$10\n", 949 | "2 $10,000\n", 950 | "dtype: object" 951 | ] 952 | }, 953 | "execution_count": 30, 954 | "metadata": {}, 955 | "output_type": "execute_result" 956 | } 957 | ], 958 | "source": [ 959 | "dollars" 960 | ] 961 | }, 962 | { 963 | "cell_type": "code", 964 | "execution_count": 31, 965 | "metadata": { 966 | "collapsed": false 967 | }, 968 | "outputs": [ 969 | { 970 | "data": { 971 | "text/plain": [ 972 | "0 12\n", 973 | "1 -10\n", 974 | "2 10,000\n", 975 | "dtype: object" 976 | ] 977 | }, 978 | "execution_count": 31, 979 | "metadata": {}, 980 | "output_type": "execute_result" 981 | } 982 | ], 983 | "source": [ 984 | "dollars.str.replace('$', '')" 985 | ] 986 | }, 987 | { 988 | "cell_type": "code", 989 | "execution_count": 32, 990 | "metadata": { 991 | "collapsed": false 992 | }, 993 | "outputs": [ 994 | { 995 | "data": { 996 | "text/plain": [ 997 | "0 12\n", 998 | "1 -$10\n", 999 | "2 $10,000\n", 1000 | "dtype: object" 1001 | ] 1002 | }, 1003 | "execution_count": 32, 1004 | "metadata": {}, 1005 | "output_type": "execute_result" 1006 | } 1007 | ], 1008 | "source": [ 1009 | "# But this doesn't:\n", 1010 | "dollars.str.replace('-$', '-')" 1011 | ] 1012 | }, 1013 | { 1014 | "cell_type": "code", 1015 | "execution_count": 33, 1016 | "metadata": { 1017 | "collapsed": false 1018 | }, 1019 | "outputs": [ 1020 | { 1021 | "data": { 1022 | "text/plain": [ 1023 | "0 12\n", 1024 | "1 -10\n", 1025 | "2 $10,000\n", 1026 | "dtype: object" 1027 | ] 1028 | }, 1029 | "execution_count": 33, 1030 | "metadata": {}, 1031 | "output_type": "execute_result" 1032 | } 1033 | ], 1034 | "source": [ 1035 | "# We need to escape the special character (for >1 len patterns)\n", 1036 | "dollars.str.replace(r'-\\$', '-')" 1037 | ] 1038 | }, 1039 | { 1040 | "cell_type": "markdown", 1041 | "metadata": {}, 1042 | "source": [ 1043 | "## Indexing with .str" 1044 | ] 1045 | }, 1046 | { 1047 | "cell_type": "markdown", 1048 | "metadata": {}, 1049 | "source": [ 1050 | "You can use [] notation to directly index by position locations. If you index past the end of the string, the result will be a NaN." 1051 | ] 1052 | }, 1053 | { 1054 | "cell_type": "code", 1055 | "execution_count": 34, 1056 | "metadata": { 1057 | "collapsed": true 1058 | }, 1059 | "outputs": [], 1060 | "source": [ 1061 | "s = pd.Series(['A', 'B', 'C', 'Aaba', 'Baca', np.nan,\n", 1062 | " 'CABA', 'dog', 'cat'])" 1063 | ] 1064 | }, 1065 | { 1066 | "cell_type": "code", 1067 | "execution_count": 35, 1068 | "metadata": { 1069 | "collapsed": false 1070 | }, 1071 | "outputs": [ 1072 | { 1073 | "data": { 1074 | "text/plain": [ 1075 | "0 A\n", 1076 | "1 B\n", 1077 | "2 C\n", 1078 | "3 Aaba\n", 1079 | "4 Baca\n", 1080 | "5 NaN\n", 1081 | "6 CABA\n", 1082 | "7 dog\n", 1083 | "8 cat\n", 1084 | "dtype: object" 1085 | ] 1086 | }, 1087 | "execution_count": 35, 1088 | "metadata": {}, 1089 | "output_type": "execute_result" 1090 | } 1091 | ], 1092 | "source": [ 1093 | "s" 1094 | ] 1095 | }, 1096 | { 1097 | "cell_type": "code", 1098 | "execution_count": 36, 1099 | "metadata": { 1100 | "collapsed": false 1101 | }, 1102 | "outputs": [ 1103 | { 1104 | "data": { 1105 | "text/plain": [ 1106 | "0 A\n", 1107 | "1 B\n", 1108 | "2 C\n", 1109 | "3 Aa\n", 1110 | "4 Ba\n", 1111 | "5 NaN\n", 1112 | "6 CA\n", 1113 | "7 do\n", 1114 | "8 ca\n", 1115 | "dtype: object" 1116 | ] 1117 | }, 1118 | "execution_count": 36, 1119 | "metadata": {}, 1120 | "output_type": "execute_result" 1121 | } 1122 | ], 1123 | "source": [ 1124 | "s.str[0:2]" 1125 | ] 1126 | }, 1127 | { 1128 | "cell_type": "code", 1129 | "execution_count": 37, 1130 | "metadata": { 1131 | "collapsed": false 1132 | }, 1133 | "outputs": [ 1134 | { 1135 | "data": { 1136 | "text/plain": [ 1137 | "0 NaN\n", 1138 | "1 NaN\n", 1139 | "2 NaN\n", 1140 | "3 a\n", 1141 | "4 a\n", 1142 | "5 NaN\n", 1143 | "6 A\n", 1144 | "7 o\n", 1145 | "8 a\n", 1146 | "dtype: object" 1147 | ] 1148 | }, 1149 | "execution_count": 37, 1150 | "metadata": {}, 1151 | "output_type": "execute_result" 1152 | } 1153 | ], 1154 | "source": [ 1155 | "s.str[1]" 1156 | ] 1157 | }, 1158 | { 1159 | "cell_type": "markdown", 1160 | "metadata": {}, 1161 | "source": [ 1162 | "## Extracting Substrings" 1163 | ] 1164 | }, 1165 | { 1166 | "cell_type": "markdown", 1167 | "metadata": {}, 1168 | "source": [ 1169 | "### Extract first match in each subject (extract)" 1170 | ] 1171 | }, 1172 | { 1173 | "cell_type": "markdown", 1174 | "metadata": {}, 1175 | "source": [ 1176 | "New in version 0.13.0.\n", 1177 | "\n", 1178 | "**Warning:** In version 0.18.0, extract gained the expand argument. When expand=False it returns a Series, Index, or DataFrame, depending on the subject and regular expression pattern (same behavior as pre-0.18.0). When expand=True it always returns a DataFrame, which is more consistent and less confusing from the perspective of a user.\n", 1179 | "\n", 1180 | "The extract method accepts a [regular expression](https://docs.python.org/2/library/re.html) with at least one capture group.\n", 1181 | "\n", 1182 | "Extracting a regular expression with more than one group returns a DataFrame with one column per group." 1183 | ] 1184 | }, 1185 | { 1186 | "cell_type": "code", 1187 | "execution_count": 38, 1188 | "metadata": { 1189 | "collapsed": false 1190 | }, 1191 | "outputs": [ 1192 | { 1193 | "data": { 1194 | "text/plain": [ 1195 | "0 a1\n", 1196 | "1 b2\n", 1197 | "2 c3\n", 1198 | "dtype: object" 1199 | ] 1200 | }, 1201 | "execution_count": 38, 1202 | "metadata": {}, 1203 | "output_type": "execute_result" 1204 | } 1205 | ], 1206 | "source": [ 1207 | " pd.Series(['a1', 'b2', 'c3'])" 1208 | ] 1209 | }, 1210 | { 1211 | "cell_type": "code", 1212 | "execution_count": 39, 1213 | "metadata": { 1214 | "collapsed": false 1215 | }, 1216 | "outputs": [ 1217 | { 1218 | "data": { 1219 | "text/html": [ 1220 | "
\n", 1221 | "\n", 1222 | " \n", 1223 | " \n", 1224 | " \n", 1225 | " \n", 1226 | " \n", 1227 | " \n", 1228 | " \n", 1229 | " \n", 1230 | " \n", 1231 | " \n", 1232 | " \n", 1233 | " \n", 1234 | " \n", 1235 | " \n", 1236 | " \n", 1237 | " \n", 1238 | " \n", 1239 | " \n", 1240 | " \n", 1241 | " \n", 1242 | " \n", 1243 | " \n", 1244 | " \n", 1245 | " \n", 1246 | "
01
0a1
1b2
2NaNNaN
\n", 1247 | "
" 1248 | ], 1249 | "text/plain": [ 1250 | " 0 1\n", 1251 | "0 a 1\n", 1252 | "1 b 2\n", 1253 | "2 NaN NaN" 1254 | ] 1255 | }, 1256 | "execution_count": 39, 1257 | "metadata": {}, 1258 | "output_type": "execute_result" 1259 | } 1260 | ], 1261 | "source": [ 1262 | "pd.Series(['a1', 'b2', 'c3']).str.extract('([ab])(\\d)', expand=False)" 1263 | ] 1264 | }, 1265 | { 1266 | "cell_type": "markdown", 1267 | "metadata": {}, 1268 | "source": [ 1269 | "Elements that do not match return a row filled with NaN. Thus, a Series of messy strings can be “converted” into a like-indexed Series or DataFrame of cleaned-up or more useful strings, without necessitating get() to access tuples or re.match objects. The dtype of the result is always object, even if no match is found and the result only contains NaN.\n", 1270 | "\n", 1271 | "Named groups like" 1272 | ] 1273 | }, 1274 | { 1275 | "cell_type": "code", 1276 | "execution_count": 40, 1277 | "metadata": { 1278 | "collapsed": false 1279 | }, 1280 | "outputs": [ 1281 | { 1282 | "data": { 1283 | "text/html": [ 1284 | "
\n", 1285 | "\n", 1286 | " \n", 1287 | " \n", 1288 | " \n", 1289 | " \n", 1290 | " \n", 1291 | " \n", 1292 | " \n", 1293 | " \n", 1294 | " \n", 1295 | " \n", 1296 | " \n", 1297 | " \n", 1298 | " \n", 1299 | " \n", 1300 | " \n", 1301 | " \n", 1302 | " \n", 1303 | " \n", 1304 | " \n", 1305 | " \n", 1306 | " \n", 1307 | " \n", 1308 | " \n", 1309 | " \n", 1310 | "
letterdigit
0a1
1b2
2NaNNaN
\n", 1311 | "
" 1312 | ], 1313 | "text/plain": [ 1314 | " letter digit\n", 1315 | "0 a 1\n", 1316 | "1 b 2\n", 1317 | "2 NaN NaN" 1318 | ] 1319 | }, 1320 | "execution_count": 40, 1321 | "metadata": {}, 1322 | "output_type": "execute_result" 1323 | } 1324 | ], 1325 | "source": [ 1326 | "pd.Series(['a1', 'b2', 'c3']).str.extract('(?P[ab])(?P\\d)', expand=False)" 1327 | ] 1328 | }, 1329 | { 1330 | "cell_type": "markdown", 1331 | "metadata": {}, 1332 | "source": [ 1333 | "and optional groups like" 1334 | ] 1335 | }, 1336 | { 1337 | "cell_type": "code", 1338 | "execution_count": 41, 1339 | "metadata": { 1340 | "collapsed": false 1341 | }, 1342 | "outputs": [ 1343 | { 1344 | "data": { 1345 | "text/html": [ 1346 | "
\n", 1347 | "\n", 1348 | " \n", 1349 | " \n", 1350 | " \n", 1351 | " \n", 1352 | " \n", 1353 | " \n", 1354 | " \n", 1355 | " \n", 1356 | " \n", 1357 | " \n", 1358 | " \n", 1359 | " \n", 1360 | " \n", 1361 | " \n", 1362 | " \n", 1363 | " \n", 1364 | " \n", 1365 | " \n", 1366 | " \n", 1367 | " \n", 1368 | " \n", 1369 | " \n", 1370 | " \n", 1371 | " \n", 1372 | "
01
0a1
1b2
2NaN3
\n", 1373 | "
" 1374 | ], 1375 | "text/plain": [ 1376 | " 0 1\n", 1377 | "0 a 1\n", 1378 | "1 b 2\n", 1379 | "2 NaN 3" 1380 | ] 1381 | }, 1382 | "execution_count": 41, 1383 | "metadata": {}, 1384 | "output_type": "execute_result" 1385 | } 1386 | ], 1387 | "source": [ 1388 | "pd.Series(['a1', 'b2', '3']).str.extract('([ab])?(\\d)', expand=False)" 1389 | ] 1390 | }, 1391 | { 1392 | "cell_type": "markdown", 1393 | "metadata": {}, 1394 | "source": [ 1395 | "can also be used. Note that any capture group names in the regular expression will be used for column names; otherwise capture group numbers will be used.\n", 1396 | "\n", 1397 | "Extracting a regular expression with one group returns a DataFrame with one column if expand=True." 1398 | ] 1399 | }, 1400 | { 1401 | "cell_type": "code", 1402 | "execution_count": 42, 1403 | "metadata": { 1404 | "collapsed": false 1405 | }, 1406 | "outputs": [ 1407 | { 1408 | "data": { 1409 | "text/html": [ 1410 | "
\n", 1411 | "\n", 1412 | " \n", 1413 | " \n", 1414 | " \n", 1415 | " \n", 1416 | " \n", 1417 | " \n", 1418 | " \n", 1419 | " \n", 1420 | " \n", 1421 | " \n", 1422 | " \n", 1423 | " \n", 1424 | " \n", 1425 | " \n", 1426 | " \n", 1427 | " \n", 1428 | " \n", 1429 | " \n", 1430 | " \n", 1431 | " \n", 1432 | "
0
01
12
2NaN
\n", 1433 | "
" 1434 | ], 1435 | "text/plain": [ 1436 | " 0\n", 1437 | "0 1\n", 1438 | "1 2\n", 1439 | "2 NaN" 1440 | ] 1441 | }, 1442 | "execution_count": 42, 1443 | "metadata": {}, 1444 | "output_type": "execute_result" 1445 | } 1446 | ], 1447 | "source": [ 1448 | "pd.Series(['a1', 'b2', 'c3']).str.extract('[ab](\\d)', expand=True)" 1449 | ] 1450 | }, 1451 | { 1452 | "cell_type": "markdown", 1453 | "metadata": {}, 1454 | "source": [ 1455 | "It returns a Series if expand=False." 1456 | ] 1457 | }, 1458 | { 1459 | "cell_type": "code", 1460 | "execution_count": 43, 1461 | "metadata": { 1462 | "collapsed": false 1463 | }, 1464 | "outputs": [ 1465 | { 1466 | "data": { 1467 | "text/plain": [ 1468 | "0 1\n", 1469 | "1 2\n", 1470 | "2 NaN\n", 1471 | "dtype: object" 1472 | ] 1473 | }, 1474 | "execution_count": 43, 1475 | "metadata": {}, 1476 | "output_type": "execute_result" 1477 | } 1478 | ], 1479 | "source": [ 1480 | "pd.Series(['a1', 'b2', 'c3']).str.extract('[ab](\\d)', expand=False)" 1481 | ] 1482 | }, 1483 | { 1484 | "cell_type": "markdown", 1485 | "metadata": {}, 1486 | "source": [ 1487 | "Calling on an Index with a regex with exactly one capture group returns a DataFrame with one column if expand=True," 1488 | ] 1489 | }, 1490 | { 1491 | "cell_type": "code", 1492 | "execution_count": 44, 1493 | "metadata": { 1494 | "collapsed": true 1495 | }, 1496 | "outputs": [], 1497 | "source": [ 1498 | "s = pd.Series([\"a1\", \"b2\", \"c3\"], [\"A11\", \"B22\", \"C33\"])" 1499 | ] 1500 | }, 1501 | { 1502 | "cell_type": "code", 1503 | "execution_count": 45, 1504 | "metadata": { 1505 | "collapsed": false 1506 | }, 1507 | "outputs": [ 1508 | { 1509 | "data": { 1510 | "text/plain": [ 1511 | "A11 a1\n", 1512 | "B22 b2\n", 1513 | "C33 c3\n", 1514 | "dtype: object" 1515 | ] 1516 | }, 1517 | "execution_count": 45, 1518 | "metadata": {}, 1519 | "output_type": "execute_result" 1520 | } 1521 | ], 1522 | "source": [ 1523 | "s" 1524 | ] 1525 | }, 1526 | { 1527 | "cell_type": "code", 1528 | "execution_count": 46, 1529 | "metadata": { 1530 | "collapsed": false 1531 | }, 1532 | "outputs": [ 1533 | { 1534 | "data": { 1535 | "text/html": [ 1536 | "
\n", 1537 | "\n", 1538 | " \n", 1539 | " \n", 1540 | " \n", 1541 | " \n", 1542 | " \n", 1543 | " \n", 1544 | " \n", 1545 | " \n", 1546 | " \n", 1547 | " \n", 1548 | " \n", 1549 | " \n", 1550 | " \n", 1551 | " \n", 1552 | " \n", 1553 | " \n", 1554 | " \n", 1555 | " \n", 1556 | " \n", 1557 | " \n", 1558 | "
letter
0A
1B
2C
\n", 1559 | "
" 1560 | ], 1561 | "text/plain": [ 1562 | " letter\n", 1563 | "0 A\n", 1564 | "1 B\n", 1565 | "2 C" 1566 | ] 1567 | }, 1568 | "execution_count": 46, 1569 | "metadata": {}, 1570 | "output_type": "execute_result" 1571 | } 1572 | ], 1573 | "source": [ 1574 | "s.index.str.extract(\"(?P[a-zA-Z])\", expand=True)" 1575 | ] 1576 | }, 1577 | { 1578 | "cell_type": "markdown", 1579 | "metadata": {}, 1580 | "source": [ 1581 | "It returns an Index if expand=False." 1582 | ] 1583 | }, 1584 | { 1585 | "cell_type": "code", 1586 | "execution_count": 47, 1587 | "metadata": { 1588 | "collapsed": false 1589 | }, 1590 | "outputs": [ 1591 | { 1592 | "data": { 1593 | "text/plain": [ 1594 | "Index([u'A', u'B', u'C'], dtype='object', name=u'letter')" 1595 | ] 1596 | }, 1597 | "execution_count": 47, 1598 | "metadata": {}, 1599 | "output_type": "execute_result" 1600 | } 1601 | ], 1602 | "source": [ 1603 | "s.index.str.extract(\"(?P[a-zA-Z])\", expand=False)" 1604 | ] 1605 | }, 1606 | { 1607 | "cell_type": "markdown", 1608 | "metadata": {}, 1609 | "source": [ 1610 | "Calling on an Index with a regex with more than one capture group returns a DataFrame if expand=True." 1611 | ] 1612 | }, 1613 | { 1614 | "cell_type": "code", 1615 | "execution_count": 48, 1616 | "metadata": { 1617 | "collapsed": false 1618 | }, 1619 | "outputs": [ 1620 | { 1621 | "data": { 1622 | "text/html": [ 1623 | "
\n", 1624 | "\n", 1625 | " \n", 1626 | " \n", 1627 | " \n", 1628 | " \n", 1629 | " \n", 1630 | " \n", 1631 | " \n", 1632 | " \n", 1633 | " \n", 1634 | " \n", 1635 | " \n", 1636 | " \n", 1637 | " \n", 1638 | " \n", 1639 | " \n", 1640 | " \n", 1641 | " \n", 1642 | " \n", 1643 | " \n", 1644 | " \n", 1645 | " \n", 1646 | " \n", 1647 | " \n", 1648 | " \n", 1649 | "
letter1
0A11
1B22
2C33
\n", 1650 | "
" 1651 | ], 1652 | "text/plain": [ 1653 | " letter 1\n", 1654 | "0 A 11\n", 1655 | "1 B 22\n", 1656 | "2 C 33" 1657 | ] 1658 | }, 1659 | "execution_count": 48, 1660 | "metadata": {}, 1661 | "output_type": "execute_result" 1662 | } 1663 | ], 1664 | "source": [ 1665 | "s.index.str.extract(\"(?P[a-zA-Z])([0-9]+)\", expand=True)" 1666 | ] 1667 | }, 1668 | { 1669 | "cell_type": "markdown", 1670 | "metadata": {}, 1671 | "source": [ 1672 | "It raises ValueError if expand=False." 1673 | ] 1674 | }, 1675 | { 1676 | "cell_type": "markdown", 1677 | "metadata": {}, 1678 | "source": [ 1679 | " s.index.str.extract(\"(?P[a-zA-Z])([0-9]+)\", expand=False)\n", 1680 | " \n", 1681 | " ValueError: only one regex group is supported with Index" 1682 | ] 1683 | }, 1684 | { 1685 | "cell_type": "markdown", 1686 | "metadata": {}, 1687 | "source": [ 1688 | "The table below summarizes the behavior of extract(expand=False) (input subject in first column, number of groups in regex in first row)" 1689 | ] 1690 | }, 1691 | { 1692 | "cell_type": "markdown", 1693 | "metadata": {}, 1694 | "source": [ 1695 | "\n", 1696 | "\n", 1697 | "\n", 1698 | "\n", 1699 | "\n", 1700 | "\n", 1701 | "\n", 1702 | "\n", 1703 | "\n", 1704 | "\n", 1705 | "\n", 1706 | "\n", 1707 | "\n", 1708 | "\n", 1709 | "\n", 1710 | "\n", 1711 | "\n", 1712 | "\n", 1713 | "\n", 1714 | "\n", 1715 | "
 1 group>1 group
IndexIndexValueError
SeriesSeriesDataFrame
" 1716 | ] 1717 | }, 1718 | { 1719 | "cell_type": "markdown", 1720 | "metadata": {}, 1721 | "source": [ 1722 | "### Extract all matches in each subject (extractall)" 1723 | ] 1724 | }, 1725 | { 1726 | "cell_type": "markdown", 1727 | "metadata": {}, 1728 | "source": [ 1729 | "New in version 0.18.0.\n", 1730 | "\n", 1731 | "Unlike extract (which returns only the first match)," 1732 | ] 1733 | }, 1734 | { 1735 | "cell_type": "code", 1736 | "execution_count": 49, 1737 | "metadata": { 1738 | "collapsed": true 1739 | }, 1740 | "outputs": [], 1741 | "source": [ 1742 | "s = pd.Series([\"a1a2\", \"b1\", \"c1\"], [\"A\", \"B\", \"C\"])" 1743 | ] 1744 | }, 1745 | { 1746 | "cell_type": "code", 1747 | "execution_count": 50, 1748 | "metadata": { 1749 | "collapsed": false 1750 | }, 1751 | "outputs": [ 1752 | { 1753 | "data": { 1754 | "text/plain": [ 1755 | "A a1a2\n", 1756 | "B b1\n", 1757 | "C c1\n", 1758 | "dtype: object" 1759 | ] 1760 | }, 1761 | "execution_count": 50, 1762 | "metadata": {}, 1763 | "output_type": "execute_result" 1764 | } 1765 | ], 1766 | "source": [ 1767 | "s" 1768 | ] 1769 | }, 1770 | { 1771 | "cell_type": "code", 1772 | "execution_count": 51, 1773 | "metadata": { 1774 | "collapsed": true 1775 | }, 1776 | "outputs": [], 1777 | "source": [ 1778 | "two_groups = '(?P[a-z])(?P[0-9])'" 1779 | ] 1780 | }, 1781 | { 1782 | "cell_type": "code", 1783 | "execution_count": 52, 1784 | "metadata": { 1785 | "collapsed": false 1786 | }, 1787 | "outputs": [ 1788 | { 1789 | "data": { 1790 | "text/html": [ 1791 | "
\n", 1792 | "\n", 1793 | " \n", 1794 | " \n", 1795 | " \n", 1796 | " \n", 1797 | " \n", 1798 | " \n", 1799 | " \n", 1800 | " \n", 1801 | " \n", 1802 | " \n", 1803 | " \n", 1804 | " \n", 1805 | " \n", 1806 | " \n", 1807 | " \n", 1808 | " \n", 1809 | " \n", 1810 | " \n", 1811 | " \n", 1812 | " \n", 1813 | " \n", 1814 | " \n", 1815 | " \n", 1816 | " \n", 1817 | "
letterdigit
Aa1
Bb1
Cc1
\n", 1818 | "
" 1819 | ], 1820 | "text/plain": [ 1821 | " letter digit\n", 1822 | "A a 1\n", 1823 | "B b 1\n", 1824 | "C c 1" 1825 | ] 1826 | }, 1827 | "execution_count": 52, 1828 | "metadata": {}, 1829 | "output_type": "execute_result" 1830 | } 1831 | ], 1832 | "source": [ 1833 | "s.str.extract(two_groups, expand=True)" 1834 | ] 1835 | }, 1836 | { 1837 | "cell_type": "markdown", 1838 | "metadata": {}, 1839 | "source": [ 1840 | "the extractall method returns every match. The result of extractall is always a DataFrame with a MultiIndex on its rows. The last level of the MultiIndex is named match and indicates the order in the subject." 1841 | ] 1842 | }, 1843 | { 1844 | "cell_type": "code", 1845 | "execution_count": 53, 1846 | "metadata": { 1847 | "collapsed": false 1848 | }, 1849 | "outputs": [ 1850 | { 1851 | "data": { 1852 | "text/html": [ 1853 | "
\n", 1854 | "\n", 1855 | " \n", 1856 | " \n", 1857 | " \n", 1858 | " \n", 1859 | " \n", 1860 | " \n", 1861 | " \n", 1862 | " \n", 1863 | " \n", 1864 | " \n", 1865 | " \n", 1866 | " \n", 1867 | " \n", 1868 | " \n", 1869 | " \n", 1870 | " \n", 1871 | " \n", 1872 | " \n", 1873 | " \n", 1874 | " \n", 1875 | " \n", 1876 | " \n", 1877 | " \n", 1878 | " \n", 1879 | " \n", 1880 | " \n", 1881 | " \n", 1882 | " \n", 1883 | " \n", 1884 | " \n", 1885 | " \n", 1886 | " \n", 1887 | " \n", 1888 | " \n", 1889 | " \n", 1890 | " \n", 1891 | " \n", 1892 | " \n", 1893 | " \n", 1894 | "
letterdigit
match
A0a1
1a2
B0b1
C0c1
\n", 1895 | "
" 1896 | ], 1897 | "text/plain": [ 1898 | " letter digit\n", 1899 | " match \n", 1900 | "A 0 a 1\n", 1901 | " 1 a 2\n", 1902 | "B 0 b 1\n", 1903 | "C 0 c 1" 1904 | ] 1905 | }, 1906 | "execution_count": 53, 1907 | "metadata": {}, 1908 | "output_type": "execute_result" 1909 | } 1910 | ], 1911 | "source": [ 1912 | " s.str.extractall(two_groups)" 1913 | ] 1914 | }, 1915 | { 1916 | "cell_type": "markdown", 1917 | "metadata": {}, 1918 | "source": [ 1919 | "When each subject string in the Series has exactly one match," 1920 | ] 1921 | }, 1922 | { 1923 | "cell_type": "code", 1924 | "execution_count": 54, 1925 | "metadata": { 1926 | "collapsed": true 1927 | }, 1928 | "outputs": [], 1929 | "source": [ 1930 | "s = pd.Series(['a3', 'b3', 'c2'])" 1931 | ] 1932 | }, 1933 | { 1934 | "cell_type": "code", 1935 | "execution_count": 55, 1936 | "metadata": { 1937 | "collapsed": false 1938 | }, 1939 | "outputs": [ 1940 | { 1941 | "data": { 1942 | "text/plain": [ 1943 | "0 a3\n", 1944 | "1 b3\n", 1945 | "2 c2\n", 1946 | "dtype: object" 1947 | ] 1948 | }, 1949 | "execution_count": 55, 1950 | "metadata": {}, 1951 | "output_type": "execute_result" 1952 | } 1953 | ], 1954 | "source": [ 1955 | "s" 1956 | ] 1957 | }, 1958 | { 1959 | "cell_type": "markdown", 1960 | "metadata": {}, 1961 | "source": [ 1962 | "then extractall(pat).xs(0, level='match') gives the same result as extract(pat)." 1963 | ] 1964 | }, 1965 | { 1966 | "cell_type": "code", 1967 | "execution_count": 56, 1968 | "metadata": { 1969 | "collapsed": true 1970 | }, 1971 | "outputs": [], 1972 | "source": [ 1973 | "extract_result = s.str.extract(two_groups, expand=True)" 1974 | ] 1975 | }, 1976 | { 1977 | "cell_type": "code", 1978 | "execution_count": 57, 1979 | "metadata": { 1980 | "collapsed": false 1981 | }, 1982 | "outputs": [ 1983 | { 1984 | "data": { 1985 | "text/html": [ 1986 | "
\n", 1987 | "\n", 1988 | " \n", 1989 | " \n", 1990 | " \n", 1991 | " \n", 1992 | " \n", 1993 | " \n", 1994 | " \n", 1995 | " \n", 1996 | " \n", 1997 | " \n", 1998 | " \n", 1999 | " \n", 2000 | " \n", 2001 | " \n", 2002 | " \n", 2003 | " \n", 2004 | " \n", 2005 | " \n", 2006 | " \n", 2007 | " \n", 2008 | " \n", 2009 | " \n", 2010 | " \n", 2011 | " \n", 2012 | "
letterdigit
0a3
1b3
2c2
\n", 2013 | "
" 2014 | ], 2015 | "text/plain": [ 2016 | " letter digit\n", 2017 | "0 a 3\n", 2018 | "1 b 3\n", 2019 | "2 c 2" 2020 | ] 2021 | }, 2022 | "execution_count": 57, 2023 | "metadata": {}, 2024 | "output_type": "execute_result" 2025 | } 2026 | ], 2027 | "source": [ 2028 | "extract_result" 2029 | ] 2030 | }, 2031 | { 2032 | "cell_type": "code", 2033 | "execution_count": 58, 2034 | "metadata": { 2035 | "collapsed": true 2036 | }, 2037 | "outputs": [], 2038 | "source": [ 2039 | "extractall_result = s.str.extractall(two_groups)" 2040 | ] 2041 | }, 2042 | { 2043 | "cell_type": "code", 2044 | "execution_count": 59, 2045 | "metadata": { 2046 | "collapsed": false 2047 | }, 2048 | "outputs": [ 2049 | { 2050 | "data": { 2051 | "text/html": [ 2052 | "
\n", 2053 | "\n", 2054 | " \n", 2055 | " \n", 2056 | " \n", 2057 | " \n", 2058 | " \n", 2059 | " \n", 2060 | " \n", 2061 | " \n", 2062 | " \n", 2063 | " \n", 2064 | " \n", 2065 | " \n", 2066 | " \n", 2067 | " \n", 2068 | " \n", 2069 | " \n", 2070 | " \n", 2071 | " \n", 2072 | " \n", 2073 | " \n", 2074 | " \n", 2075 | " \n", 2076 | " \n", 2077 | " \n", 2078 | " \n", 2079 | " \n", 2080 | " \n", 2081 | " \n", 2082 | " \n", 2083 | " \n", 2084 | " \n", 2085 | " \n", 2086 | " \n", 2087 | " \n", 2088 | "
letterdigit
match
00a3
10b3
20c2
\n", 2089 | "
" 2090 | ], 2091 | "text/plain": [ 2092 | " letter digit\n", 2093 | " match \n", 2094 | "0 0 a 3\n", 2095 | "1 0 b 3\n", 2096 | "2 0 c 2" 2097 | ] 2098 | }, 2099 | "execution_count": 59, 2100 | "metadata": {}, 2101 | "output_type": "execute_result" 2102 | } 2103 | ], 2104 | "source": [ 2105 | "extractall_result" 2106 | ] 2107 | }, 2108 | { 2109 | "cell_type": "code", 2110 | "execution_count": 60, 2111 | "metadata": { 2112 | "collapsed": false 2113 | }, 2114 | "outputs": [ 2115 | { 2116 | "data": { 2117 | "text/html": [ 2118 | "
\n", 2119 | "\n", 2120 | " \n", 2121 | " \n", 2122 | " \n", 2123 | " \n", 2124 | " \n", 2125 | " \n", 2126 | " \n", 2127 | " \n", 2128 | " \n", 2129 | " \n", 2130 | " \n", 2131 | " \n", 2132 | " \n", 2133 | " \n", 2134 | " \n", 2135 | " \n", 2136 | " \n", 2137 | " \n", 2138 | " \n", 2139 | " \n", 2140 | " \n", 2141 | " \n", 2142 | " \n", 2143 | " \n", 2144 | "
letterdigit
0a3
1b3
2c2
\n", 2145 | "
" 2146 | ], 2147 | "text/plain": [ 2148 | " letter digit\n", 2149 | "0 a 3\n", 2150 | "1 b 3\n", 2151 | "2 c 2" 2152 | ] 2153 | }, 2154 | "execution_count": 60, 2155 | "metadata": {}, 2156 | "output_type": "execute_result" 2157 | } 2158 | ], 2159 | "source": [ 2160 | "extractall_result.xs(0, level=\"match\")" 2161 | ] 2162 | }, 2163 | { 2164 | "cell_type": "markdown", 2165 | "metadata": {}, 2166 | "source": [ 2167 | "## Testing for Strings that Match or Contain a Pattern" 2168 | ] 2169 | }, 2170 | { 2171 | "cell_type": "markdown", 2172 | "metadata": {}, 2173 | "source": [ 2174 | "You can check whether elements contain a pattern:" 2175 | ] 2176 | }, 2177 | { 2178 | "cell_type": "code", 2179 | "execution_count": 61, 2180 | "metadata": { 2181 | "collapsed": true 2182 | }, 2183 | "outputs": [], 2184 | "source": [ 2185 | "pattern = r'[a-z][0-9]'" 2186 | ] 2187 | }, 2188 | { 2189 | "cell_type": "code", 2190 | "execution_count": 62, 2191 | "metadata": { 2192 | "collapsed": false 2193 | }, 2194 | "outputs": [ 2195 | { 2196 | "data": { 2197 | "text/plain": [ 2198 | "0 False\n", 2199 | "1 False\n", 2200 | "2 False\n", 2201 | "3 False\n", 2202 | "4 False\n", 2203 | "dtype: bool" 2204 | ] 2205 | }, 2206 | "execution_count": 62, 2207 | "metadata": {}, 2208 | "output_type": "execute_result" 2209 | } 2210 | ], 2211 | "source": [ 2212 | "pd.Series(['1', '2', '3a', '3b', '03c']).str.contains(pattern)" 2213 | ] 2214 | }, 2215 | { 2216 | "cell_type": "markdown", 2217 | "metadata": {}, 2218 | "source": [ 2219 | "or match a pattern:" 2220 | ] 2221 | }, 2222 | { 2223 | "cell_type": "code", 2224 | "execution_count": 63, 2225 | "metadata": { 2226 | "collapsed": false 2227 | }, 2228 | "outputs": [ 2229 | { 2230 | "data": { 2231 | "text/plain": [ 2232 | "0 False\n", 2233 | "1 False\n", 2234 | "2 False\n", 2235 | "3 False\n", 2236 | "4 False\n", 2237 | "dtype: bool" 2238 | ] 2239 | }, 2240 | "execution_count": 63, 2241 | "metadata": {}, 2242 | "output_type": "execute_result" 2243 | } 2244 | ], 2245 | "source": [ 2246 | "pd.Series(['1', '2', '3a', '3b', '03c']).str.match(pattern)" 2247 | ] 2248 | }, 2249 | { 2250 | "cell_type": "code", 2251 | "execution_count": 64, 2252 | "metadata": { 2253 | "collapsed": false 2254 | }, 2255 | "outputs": [ 2256 | { 2257 | "data": { 2258 | "text/plain": [ 2259 | "0 False\n", 2260 | "1 False\n", 2261 | "2 False\n", 2262 | "3 False\n", 2263 | "4 False\n", 2264 | "dtype: bool" 2265 | ] 2266 | }, 2267 | "execution_count": 64, 2268 | "metadata": {}, 2269 | "output_type": "execute_result" 2270 | } 2271 | ], 2272 | "source": [ 2273 | "pd.Series(['1', '2', '3a', '3b', '03c']).str.match(pattern, as_indexer=True)" 2274 | ] 2275 | }, 2276 | { 2277 | "cell_type": "markdown", 2278 | "metadata": {}, 2279 | "source": [ 2280 | "The distinction between match and contains is strictness: match relies on strict re.match, while contains relies on re.search.\n", 2281 | "\n", 2282 | "**Warning:** In previous versions, match was for extracting groups, returning a not-so-convenient Series of tuples. The new method extract (described in the previous section) is now preferred.\n", 2283 | "\n", 2284 | "This old, deprecated behavior of match is still the default. As demonstrated above, use the new behavior by setting as_indexer=True. In this mode, match is analogous to contains, returning a boolean Series. The new behavior will become the default behavior in a future release.\n", 2285 | "\n", 2286 | "Methods like match, contains, startswith, and endswith take\n", 2287 | "an extra na argument so missing values can be considered True or False:" 2288 | ] 2289 | }, 2290 | { 2291 | "cell_type": "code", 2292 | "execution_count": 65, 2293 | "metadata": { 2294 | "collapsed": true 2295 | }, 2296 | "outputs": [], 2297 | "source": [ 2298 | "s4 = pd.Series(['A', 'B', 'C', 'Aaba', 'Baca', np.nan, 'CABA', 'dog', 'cat'])" 2299 | ] 2300 | }, 2301 | { 2302 | "cell_type": "code", 2303 | "execution_count": 66, 2304 | "metadata": { 2305 | "collapsed": false 2306 | }, 2307 | "outputs": [ 2308 | { 2309 | "data": { 2310 | "text/plain": [ 2311 | "0 True\n", 2312 | "1 False\n", 2313 | "2 False\n", 2314 | "3 True\n", 2315 | "4 False\n", 2316 | "5 NaN\n", 2317 | "6 True\n", 2318 | "7 False\n", 2319 | "8 False\n", 2320 | "dtype: object" 2321 | ] 2322 | }, 2323 | "execution_count": 66, 2324 | "metadata": {}, 2325 | "output_type": "execute_result" 2326 | } 2327 | ], 2328 | "source": [ 2329 | "s4.str.contains('A')" 2330 | ] 2331 | }, 2332 | { 2333 | "cell_type": "code", 2334 | "execution_count": 67, 2335 | "metadata": { 2336 | "collapsed": false 2337 | }, 2338 | "outputs": [ 2339 | { 2340 | "data": { 2341 | "text/plain": [ 2342 | "0 True\n", 2343 | "1 False\n", 2344 | "2 False\n", 2345 | "3 True\n", 2346 | "4 False\n", 2347 | "5 False\n", 2348 | "6 True\n", 2349 | "7 False\n", 2350 | "8 False\n", 2351 | "dtype: bool" 2352 | ] 2353 | }, 2354 | "execution_count": 67, 2355 | "metadata": {}, 2356 | "output_type": "execute_result" 2357 | } 2358 | ], 2359 | "source": [ 2360 | "s4.str.contains('A', na=False)" 2361 | ] 2362 | }, 2363 | { 2364 | "cell_type": "code", 2365 | "execution_count": 68, 2366 | "metadata": { 2367 | "collapsed": false 2368 | }, 2369 | "outputs": [ 2370 | { 2371 | "data": { 2372 | "text/plain": [ 2373 | "0 True\n", 2374 | "1 False\n", 2375 | "2 False\n", 2376 | "3 True\n", 2377 | "4 False\n", 2378 | "5 True\n", 2379 | "6 True\n", 2380 | "7 False\n", 2381 | "8 False\n", 2382 | "dtype: bool" 2383 | ] 2384 | }, 2385 | "execution_count": 68, 2386 | "metadata": {}, 2387 | "output_type": "execute_result" 2388 | } 2389 | ], 2390 | "source": [ 2391 | "s4.str.contains('A', na=True)" 2392 | ] 2393 | }, 2394 | { 2395 | "cell_type": "markdown", 2396 | "metadata": {}, 2397 | "source": [ 2398 | "## Creating Indicator Variables" 2399 | ] 2400 | }, 2401 | { 2402 | "cell_type": "markdown", 2403 | "metadata": {}, 2404 | "source": [ 2405 | "You can extract dummy variables from string columns. For example if they are separated by a '|':" 2406 | ] 2407 | }, 2408 | { 2409 | "cell_type": "code", 2410 | "execution_count": 69, 2411 | "metadata": { 2412 | "collapsed": true 2413 | }, 2414 | "outputs": [], 2415 | "source": [ 2416 | "s = pd.Series(['a', 'a|b', np.nan, 'a|c'])" 2417 | ] 2418 | }, 2419 | { 2420 | "cell_type": "code", 2421 | "execution_count": 70, 2422 | "metadata": { 2423 | "collapsed": false 2424 | }, 2425 | "outputs": [ 2426 | { 2427 | "data": { 2428 | "text/plain": [ 2429 | "0 a\n", 2430 | "1 a|b\n", 2431 | "2 NaN\n", 2432 | "3 a|c\n", 2433 | "dtype: object" 2434 | ] 2435 | }, 2436 | "execution_count": 70, 2437 | "metadata": {}, 2438 | "output_type": "execute_result" 2439 | } 2440 | ], 2441 | "source": [ 2442 | "s" 2443 | ] 2444 | }, 2445 | { 2446 | "cell_type": "code", 2447 | "execution_count": 71, 2448 | "metadata": { 2449 | "collapsed": false 2450 | }, 2451 | "outputs": [ 2452 | { 2453 | "data": { 2454 | "text/html": [ 2455 | "
\n", 2456 | "\n", 2457 | " \n", 2458 | " \n", 2459 | " \n", 2460 | " \n", 2461 | " \n", 2462 | " \n", 2463 | " \n", 2464 | " \n", 2465 | " \n", 2466 | " \n", 2467 | " \n", 2468 | " \n", 2469 | " \n", 2470 | " \n", 2471 | " \n", 2472 | " \n", 2473 | " \n", 2474 | " \n", 2475 | " \n", 2476 | " \n", 2477 | " \n", 2478 | " \n", 2479 | " \n", 2480 | " \n", 2481 | " \n", 2482 | " \n", 2483 | " \n", 2484 | " \n", 2485 | " \n", 2486 | " \n", 2487 | " \n", 2488 | " \n", 2489 | " \n", 2490 | " \n", 2491 | "
abc
0100
1110
2000
3101
\n", 2492 | "
" 2493 | ], 2494 | "text/plain": [ 2495 | " a b c\n", 2496 | "0 1 0 0\n", 2497 | "1 1 1 0\n", 2498 | "2 0 0 0\n", 2499 | "3 1 0 1" 2500 | ] 2501 | }, 2502 | "execution_count": 71, 2503 | "metadata": {}, 2504 | "output_type": "execute_result" 2505 | } 2506 | ], 2507 | "source": [ 2508 | "s.str.get_dummies(sep='|')" 2509 | ] 2510 | }, 2511 | { 2512 | "cell_type": "markdown", 2513 | "metadata": {}, 2514 | "source": [ 2515 | "String Index also supports get_dummies which returns a MultiIndex.\n", 2516 | "\n", 2517 | "New in version 0.18.1." 2518 | ] 2519 | }, 2520 | { 2521 | "cell_type": "code", 2522 | "execution_count": 72, 2523 | "metadata": { 2524 | "collapsed": true 2525 | }, 2526 | "outputs": [], 2527 | "source": [ 2528 | "idx = pd.Index(['a', 'a|b', np.nan, 'a|c'])" 2529 | ] 2530 | }, 2531 | { 2532 | "cell_type": "code", 2533 | "execution_count": 73, 2534 | "metadata": { 2535 | "collapsed": false 2536 | }, 2537 | "outputs": [ 2538 | { 2539 | "data": { 2540 | "text/plain": [ 2541 | "Index([u'a', u'a|b', nan, u'a|c'], dtype='object')" 2542 | ] 2543 | }, 2544 | "execution_count": 73, 2545 | "metadata": {}, 2546 | "output_type": "execute_result" 2547 | } 2548 | ], 2549 | "source": [ 2550 | "idx" 2551 | ] 2552 | }, 2553 | { 2554 | "cell_type": "code", 2555 | "execution_count": 74, 2556 | "metadata": { 2557 | "collapsed": false 2558 | }, 2559 | "outputs": [ 2560 | { 2561 | "data": { 2562 | "text/plain": [ 2563 | "MultiIndex(levels=[[0, 1], [0, 1], [0, 1]],\n", 2564 | " labels=[[1, 1, 0, 1], [0, 1, 0, 0], [0, 0, 0, 1]],\n", 2565 | " names=[u'a', u'b', u'c'])" 2566 | ] 2567 | }, 2568 | "execution_count": 74, 2569 | "metadata": {}, 2570 | "output_type": "execute_result" 2571 | } 2572 | ], 2573 | "source": [ 2574 | "idx.str.get_dummies(sep='|')" 2575 | ] 2576 | }, 2577 | { 2578 | "cell_type": "markdown", 2579 | "metadata": {}, 2580 | "source": [ 2581 | "See also **[get_dummies()](http://pandas.pydata.org/pandas-docs/stable/generated/pandas.get_dummies.html#pandas.get_dummies).**" 2582 | ] 2583 | }, 2584 | { 2585 | "cell_type": "markdown", 2586 | "metadata": {}, 2587 | "source": [ 2588 | "## Method Summary" 2589 | ] 2590 | }, 2591 | { 2592 | "cell_type": "markdown", 2593 | "metadata": {}, 2594 | "source": [ 2595 | "\n", 2596 | "\n", 2597 | "\n", 2598 | "\n", 2599 | "\n", 2600 | "\n", 2601 | "\n", 2602 | "\n", 2603 | "\n", 2604 | "\n", 2605 | "\n", 2606 | "\n", 2607 | "\n", 2608 | "\n", 2609 | "\n", 2610 | "\n", 2611 | "\n", 2612 | "\n", 2613 | "\n", 2614 | "\n", 2615 | "\n", 2616 | "\n", 2617 | "\n", 2618 | "\n", 2619 | "\n", 2620 | "\n", 2621 | "\n", 2622 | "\n", 2623 | "\n", 2624 | "\n", 2625 | "\n", 2626 | "\n", 2627 | "\n", 2628 | "\n", 2629 | "\n", 2630 | "\n", 2631 | "\n", 2632 | "\n", 2633 | "\n", 2634 | "\n", 2635 | "\n", 2636 | "\n", 2637 | "\n", 2638 | "\n", 2639 | "\n", 2640 | "\n", 2641 | "\n", 2642 | "\n", 2643 | "\n", 2644 | "\n", 2645 | "\n", 2646 | "\n", 2647 | "\n", 2648 | "\n", 2649 | "\n", 2650 | "\n", 2651 | "\n", 2652 | "\n", 2653 | "\n", 2654 | "\n", 2655 | "\n", 2656 | "\n", 2657 | "\n", 2658 | "\n", 2659 | "\n", 2660 | "\n", 2661 | "\n", 2662 | "\n", 2663 | "\n", 2664 | "\n", 2665 | "\n", 2666 | "\n", 2667 | "\n", 2668 | "\n", 2669 | "\n", 2670 | "\n", 2671 | "\n", 2672 | "\n", 2673 | "\n", 2674 | "\n", 2675 | "\n", 2676 | "\n", 2677 | "\n", 2678 | "\n", 2679 | "\n", 2680 | "\n", 2681 | "\n", 2682 | "\n", 2683 | "\n", 2684 | "\n", 2685 | "\n", 2686 | "\n", 2687 | "\n", 2688 | "\n", 2689 | "\n", 2690 | "\n", 2691 | "\n", 2692 | "\n", 2693 | "\n", 2694 | "\n", 2695 | "\n", 2696 | "\n", 2697 | "\n", 2698 | "\n", 2699 | "\n", 2700 | "\n", 2701 | "\n", 2702 | "\n", 2703 | "\n", 2704 | "\n", 2705 | "\n", 2706 | "\n", 2707 | "\n", 2708 | "\n", 2709 | "\n", 2710 | "\n", 2711 | "\n", 2712 | "\n", 2713 | "\n", 2714 | "\n", 2715 | "\n", 2716 | "\n", 2717 | "\n", 2718 | "\n", 2719 | "\n", 2720 | "\n", 2721 | "\n", 2722 | "\n", 2723 | "\n", 2724 | "\n", 2725 | "\n", 2726 | "\n", 2727 | "\n", 2728 | "\n", 2729 | "\n", 2730 | "\n", 2731 | "\n", 2732 | "\n", 2733 | "\n", 2734 | "\n", 2735 | "\n", 2736 | "\n", 2737 | "\n", 2738 | "\n", 2739 | "\n", 2740 | "\n", 2741 | "\n", 2742 | "\n", 2743 | "\n", 2744 | "\n", 2745 | "\n", 2746 | "\n", 2747 | "\n", 2748 | "\n", 2749 | "\n", 2750 | "\n", 2751 | "\n", 2752 | "\n", 2753 | "\n", 2754 | "
MethodDescription
cat()Concatenate strings
split()Split strings on delimiter
rsplit()Split strings on delimiter working from the end of the string
get()Index into each element (retrieve i-th element)
join()Join strings in each element of the Series with passed separator
get_dummies()Split strings on the delimiter returning DataFrame of dummy variables
contains()Return boolean array if each string contains pattern/regex
replace()Replace occurrences of pattern/regex with some other string
repeat()Duplicate values (s.str.repeat(3) equivalent to x * 3)
pad()Add whitespace to left, right, or both sides of strings
center()Equivalent to str.center
ljust()Equivalent to str.ljust
rjust()Equivalent to str.rjust
zfill()Equivalent to str.zfill
wrap()Split long strings into lines with length less than a given width
slice()Slice each string in the Series
slice_replace()Replace slice in each string with passed value
count()Count occurrences of pattern
startswith()Equivalent to str.startswith(pat) for each element
endswith()Equivalent to str.endswith(pat) for each element
findall()Compute list of all occurrences of pattern/regex for each string
match()Call re.match on each element, returning matched groups as list
extract()Call re.search on each element, returning DataFrame with one row for each element and one column for each regex capture group
extractall()Call re.findall on each element, returning DataFrame with one row for each match and one column for each regex capture group
len()Compute string lengths
strip()Equivalent to str.strip
rstrip()Equivalent to str.rstrip
lstrip()Equivalent to str.lstrip
partition()Equivalent to str.partition
rpartition()Equivalent to str.rpartition
lower()Equivalent to str.lower
upper()Equivalent to str.upper
find()Equivalent to str.find
rfind()Equivalent to str.rfind
index()Equivalent to str.index
rindex()Equivalent to str.rindex
capitalize()Equivalent to str.capitalize
swapcase()Equivalent to str.swapcase
normalize()Return Unicode normal form. Equivalent to unicodedata.normalize
translate()Equivalent to str.translate
isalnum()Equivalent to str.isalnum
isalpha()Equivalent to str.isalpha
isdigit()Equivalent to str.isdigit
isspace()Equivalent to str.isspace
islower()Equivalent to str.islower
isupper()Equivalent to str.isupper
istitle()Equivalent to str.istitle
isnumeric()Equivalent to str.isnumeric
isdecimal()Equivalent to str.isdecimal
" 2755 | ] 2756 | } 2757 | ], 2758 | "metadata": { 2759 | "kernelspec": { 2760 | "display_name": "Python 2", 2761 | "language": "python", 2762 | "name": "python2" 2763 | }, 2764 | "language_info": { 2765 | "codemirror_mode": { 2766 | "name": "ipython", 2767 | "version": 2 2768 | }, 2769 | "file_extension": ".py", 2770 | "mimetype": "text/x-python", 2771 | "name": "python", 2772 | "nbconvert_exporter": "python", 2773 | "pygments_lexer": "ipython2", 2774 | "version": "2.7.11" 2775 | } 2776 | }, 2777 | "nbformat": 4, 2778 | "nbformat_minor": 0 2779 | } 2780 | -------------------------------------------------------------------------------- /data/baseball.csv: -------------------------------------------------------------------------------- 1 | id,player,year,stint,team,lg,g,ab,r,h,X2b,X3b,hr,rbi,sb,cs,bb,so,ibb,hbp,sh,sf,gidp 2 | 88641,womacto01,2006,2,CHN,NL,19,50,6,14,1,0,1,2.0,1.0,1.0,4,4.0,0.0,0.0,3.0,0.0,0.0 3 | 88643,schilcu01,2006,1,BOS,AL,31,2,0,1,0,0,0,0.0,0.0,0.0,0,1.0,0.0,0.0,0.0,0.0,0.0 4 | 88645,myersmi01,2006,1,NYA,AL,62,0,0,0,0,0,0,0.0,0.0,0.0,0,0.0,0.0,0.0,0.0,0.0,0.0 5 | 88649,helliri01,2006,1,MIL,NL,20,3,0,0,0,0,0,0.0,0.0,0.0,0,2.0,0.0,0.0,0.0,0.0,0.0 6 | 88650,johnsra05,2006,1,NYA,AL,33,6,0,1,0,0,0,0.0,0.0,0.0,0,4.0,0.0,0.0,0.0,0.0,0.0 7 | 88652,finlest01,2006,1,SFN,NL,139,426,66,105,21,12,6,40.0,7.0,0.0,46,55.0,2.0,2.0,3.0,4.0,6.0 8 | 88653,gonzalu01,2006,1,ARI,NL,153,586,93,159,52,2,15,73.0,0.0,1.0,69,58.0,10.0,7.0,0.0,6.0,14.0 9 | 88662,seleaa01,2006,1,LAN,NL,28,26,2,5,1,0,0,0.0,0.0,0.0,1,7.0,0.0,0.0,6.0,0.0,1.0 10 | 89177,francju01,2007,2,ATL,NL,15,40,1,10,3,0,0,8.0,0.0,0.0,4,10.0,1.0,0.0,0.0,1.0,1.0 11 | 89178,francju01,2007,1,NYN,NL,40,50,7,10,0,0,1,8.0,2.0,1.0,10,13.0,0.0,0.0,0.0,1.0,1.0 12 | 89330,zaungr01,2007,1,TOR,AL,110,331,43,80,24,1,10,52.0,0.0,0.0,51,55.0,8.0,2.0,1.0,6.0,9.0 13 | 89333,witasja01,2007,1,TBA,AL,3,0,0,0,0,0,0,0.0,0.0,0.0,0,0.0,0.0,0.0,0.0,0.0,0.0 14 | 89334,williwo02,2007,1,HOU,NL,33,59,3,6,0,0,1,2.0,0.0,0.0,0,25.0,0.0,0.0,5.0,0.0,1.0 15 | 89335,wickmbo01,2007,2,ARI,NL,8,0,0,0,0,0,0,0.0,0.0,0.0,0,0.0,0.0,0.0,0.0,0.0,0.0 16 | 89336,wickmbo01,2007,1,ATL,NL,47,0,0,0,0,0,0,0.0,0.0,0.0,0,0.0,0.0,0.0,0.0,0.0,0.0 17 | 89337,whitero02,2007,1,MIN,AL,38,109,8,19,4,0,4,20.0,0.0,0.0,6,19.0,0.0,3.0,0.0,1.0,2.0 18 | 89338,whiteri01,2007,1,HOU,NL,20,1,0,0,0,0,0,0.0,0.0,0.0,0,1.0,0.0,0.0,0.0,0.0,0.0 19 | 89339,wellsda01,2007,2,LAN,NL,7,15,2,4,1,0,0,1.0,0.0,0.0,0,6.0,0.0,0.0,0.0,0.0,0.0 20 | 89340,wellsda01,2007,1,SDN,NL,22,38,1,4,0,0,0,0.0,0.0,0.0,0,12.0,0.0,0.0,4.0,0.0,0.0 21 | 89341,weathda01,2007,1,CIN,NL,67,0,0,0,0,0,0,0.0,0.0,0.0,0,0.0,0.0,0.0,0.0,0.0,0.0 22 | 89343,walketo04,2007,1,OAK,AL,18,48,5,13,1,0,0,4.0,0.0,0.0,2,4.0,0.0,0.0,0.0,2.0,2.0 23 | 89345,wakefti01,2007,1,BOS,AL,1,2,0,0,0,0,0,0.0,0.0,0.0,0,2.0,0.0,0.0,0.0,0.0,0.0 24 | 89347,vizquom01,2007,1,SFN,NL,145,513,54,126,18,3,4,51.0,14.0,6.0,44,48.0,6.0,1.0,14.0,3.0,14.0 25 | 89348,villoro01,2007,1,NYA,AL,6,0,0,0,0,0,0,0.0,0.0,0.0,0,0.0,0.0,0.0,0.0,0.0,0.0 26 | 89352,valenjo03,2007,1,NYN,NL,51,166,18,40,11,1,3,18.0,2.0,1.0,15,28.0,4.0,0.0,1.0,1.0,5.0 27 | 89354,trachst01,2007,2,CHN,NL,4,7,0,1,0,0,0,0.0,0.0,0.0,0,1.0,0.0,0.0,0.0,0.0,0.0 28 | 89355,trachst01,2007,1,BAL,AL,3,5,0,0,0,0,0,0.0,0.0,0.0,0,3.0,0.0,0.0,0.0,0.0,0.0 29 | 89359,timlimi01,2007,1,BOS,AL,4,0,0,0,0,0,0,0.0,0.0,0.0,0,0.0,0.0,0.0,0.0,0.0,0.0 30 | 89360,thomeji01,2007,1,CHA,AL,130,432,79,119,19,0,35,96.0,0.0,1.0,95,134.0,11.0,6.0,0.0,3.0,10.0 31 | 89361,thomafr04,2007,1,TOR,AL,155,531,63,147,30,0,26,95.0,0.0,0.0,81,94.0,3.0,7.0,0.0,5.0,14.0 32 | 89363,tavarju01,2007,1,BOS,AL,2,4,0,1,0,0,0,0.0,0.0,0.0,1,3.0,0.0,0.0,0.0,0.0,0.0 33 | 89365,sweenma01,2007,2,LAN,NL,30,33,2,9,1,0,0,3.0,0.0,0.0,1,11.0,0.0,0.0,0.0,0.0,0.0 34 | 89366,sweenma01,2007,1,SFN,NL,76,90,18,23,8,0,2,10.0,2.0,0.0,13,18.0,0.0,3.0,1.0,0.0,0.0 35 | 89367,suppaje01,2007,1,MIL,NL,33,61,4,8,0,0,0,2.0,0.0,0.0,3,16.0,0.0,0.0,11.0,0.0,2.0 36 | 89368,stinnke01,2007,1,SLN,NL,26,82,7,13,3,0,1,5.0,0.0,0.0,5,22.0,2.0,0.0,0.0,0.0,2.0 37 | 89370,stantmi02,2007,1,CIN,NL,67,2,0,0,0,0,0,0.0,0.0,0.0,0,0.0,0.0,0.0,0.0,0.0,0.0 38 | 89371,stairma01,2007,1,TOR,AL,125,357,58,103,28,1,21,64.0,2.0,1.0,44,66.0,5.0,2.0,0.0,2.0,7.0 39 | 89372,sprinru01,2007,1,SLN,NL,72,1,0,0,0,0,0,0.0,0.0,0.0,0,1.0,0.0,0.0,0.0,0.0,0.0 40 | 89374,sosasa01,2007,1,TEX,AL,114,412,53,104,24,1,21,92.0,0.0,0.0,34,112.0,3.0,3.0,0.0,5.0,11.0 41 | 89375,smoltjo01,2007,1,ATL,NL,30,54,1,5,1,0,0,2.0,0.0,0.0,1,19.0,0.0,0.0,13.0,0.0,0.0 42 | 89378,sheffga01,2007,1,DET,AL,133,494,107,131,20,1,25,75.0,22.0,5.0,84,71.0,2.0,9.0,0.0,6.0,10.0 43 | 89381,seleaa01,2007,1,NYN,NL,31,4,0,0,0,0,0,0.0,0.0,0.0,1,1.0,0.0,0.0,1.0,0.0,0.0 44 | 89382,seaneru01,2007,1,LAN,NL,68,1,0,0,0,0,0,0.0,0.0,0.0,0,1.0,0.0,0.0,0.0,0.0,0.0 45 | 89383,schmija01,2007,1,LAN,NL,6,7,1,1,0,0,1,1.0,0.0,0.0,0,4.0,0.0,0.0,1.0,0.0,0.0 46 | 89384,schilcu01,2007,1,BOS,AL,1,2,0,1,0,0,0,0.0,0.0,0.0,0,1.0,0.0,0.0,0.0,0.0,0.0 47 | 89385,sandere02,2007,1,KCA,AL,24,73,12,23,7,0,2,11.0,0.0,1.0,11,15.0,0.0,1.0,0.0,0.0,2.0 48 | 89388,rogerke01,2007,1,DET,AL,1,2,0,0,0,0,0,0.0,0.0,0.0,0,1.0,0.0,0.0,0.0,0.0,0.0 49 | 89389,rodriiv01,2007,1,DET,AL,129,502,50,141,31,3,11,63.0,2.0,2.0,9,96.0,1.0,1.0,1.0,2.0,16.0 50 | 89396,ramirma02,2007,1,BOS,AL,133,483,84,143,33,1,20,88.0,0.0,0.0,71,92.0,13.0,7.0,0.0,8.0,21.0 51 | 89398,piazzmi01,2007,1,OAK,AL,83,309,33,85,17,1,8,44.0,0.0,0.0,18,61.0,0.0,0.0,0.0,2.0,9.0 52 | 89400,perezne01,2007,1,DET,AL,33,64,5,11,3,0,1,6.0,0.0,0.0,4,8.0,0.0,0.0,3.0,0.0,2.0 53 | 89402,parkch01,2007,1,NYN,NL,1,1,0,0,0,0,0,0.0,0.0,0.0,0,1.0,0.0,0.0,0.0,0.0,0.0 54 | 89406,oliveda02,2007,1,LAA,AL,5,0,0,0,0,0,0,0.0,0.0,0.0,0,0.0,0.0,0.0,0.0,0.0,0.0 55 | 89410,myersmi01,2007,1,NYA,AL,6,1,0,0,0,0,0,0.0,0.0,0.0,0,0.0,0.0,0.0,0.0,0.0,0.0 56 | 89411,mussimi01,2007,1,NYA,AL,2,2,0,0,0,0,0,0.0,0.0,0.0,1,0.0,0.0,0.0,0.0,0.0,0.0 57 | 89412,moyerja01,2007,1,PHI,NL,33,73,4,9,2,0,0,2.0,0.0,0.0,2,26.0,0.0,0.0,8.0,0.0,1.0 58 | 89420,mesajo01,2007,1,PHI,NL,38,0,0,0,0,0,0,0.0,0.0,0.0,0,0.0,0.0,0.0,0.0,0.0,0.0 59 | 89421,martipe02,2007,1,NYN,NL,5,9,1,1,1,0,0,0.0,0.0,0.0,0,6.0,0.0,0.0,2.0,0.0,0.0 60 | 89425,maddugr01,2007,1,SDN,NL,33,62,2,9,2,0,0,0.0,1.0,0.0,1,19.0,0.0,0.0,9.0,0.0,2.0 61 | 89426,mabryjo01,2007,1,COL,NL,28,34,4,4,1,0,1,5.0,0.0,0.0,5,10.0,0.0,0.0,0.0,0.0,1.0 62 | 89429,loftoke01,2007,2,CLE,AL,52,173,24,49,9,3,0,15.0,2.0,3.0,17,23.0,0.0,0.0,4.0,2.0,1.0 63 | 89430,loftoke01,2007,1,TEX,AL,84,317,62,96,16,3,7,23.0,21.0,4.0,39,28.0,1.0,2.0,2.0,3.0,5.0 64 | 89431,loaizes01,2007,1,LAN,NL,5,7,0,1,0,0,0,2.0,0.0,0.0,0,2.0,0.0,0.0,2.0,0.0,1.0 65 | 89438,kleskry01,2007,1,SFN,NL,116,362,51,94,27,3,6,44.0,5.0,1.0,46,68.0,2.0,1.0,1.0,1.0,14.0 66 | 89439,kentje01,2007,1,LAN,NL,136,494,78,149,36,1,20,79.0,1.0,3.0,57,61.0,4.0,5.0,0.0,6.0,17.0 67 | 89442,jonesto02,2007,1,DET,AL,5,0,0,0,0,0,0,0.0,0.0,0.0,0,0.0,0.0,0.0,0.0,0.0,0.0 68 | 89445,johnsra05,2007,1,ARI,NL,10,15,0,1,0,0,0,0.0,0.0,0.0,1,7.0,0.0,0.0,2.0,0.0,0.0 69 | 89450,hoffmtr01,2007,1,SDN,NL,60,0,0,0,0,0,0,0.0,0.0,0.0,0,0.0,0.0,0.0,0.0,0.0,0.0 70 | 89451,hernaro01,2007,2,LAN,NL,22,0,0,0,0,0,0,0.0,0.0,0.0,0,0.0,0.0,0.0,0.0,0.0,0.0 71 | 89452,hernaro01,2007,1,CLE,AL,2,0,0,0,0,0,0,0.0,0.0,0.0,0,0.0,0.0,0.0,0.0,0.0,0.0 72 | 89460,guarded01,2007,1,CIN,NL,15,0,0,0,0,0,0,0.0,0.0,0.0,0,0.0,0.0,0.0,0.0,0.0,0.0 73 | 89462,griffke02,2007,1,CIN,NL,144,528,78,146,24,1,30,93.0,6.0,1.0,85,99.0,14.0,1.0,0.0,9.0,14.0 74 | 89463,greensh01,2007,1,NYN,NL,130,446,62,130,30,1,10,46.0,11.0,1.0,37,62.0,4.0,5.0,1.0,1.0,14.0 75 | 89464,graffto01,2007,1,MIL,NL,86,231,34,55,8,0,9,30.0,0.0,1.0,24,44.0,6.0,3.0,0.0,2.0,7.0 76 | 89465,gordoto01,2007,1,PHI,NL,44,0,0,0,0,0,0,0.0,0.0,0.0,0,0.0,0.0,0.0,0.0,0.0,0.0 77 | 89466,gonzalu01,2007,1,LAN,NL,139,464,70,129,23,2,15,68.0,6.0,2.0,56,56.0,4.0,4.0,0.0,2.0,11.0 78 | 89467,gomezch02,2007,2,CLE,AL,19,53,4,15,2,0,0,5.0,0.0,0.0,0,6.0,0.0,0.0,1.0,1.0,1.0 79 | 89468,gomezch02,2007,1,BAL,AL,73,169,17,51,10,1,1,16.0,1.0,2.0,10,20.0,1.0,0.0,5.0,1.0,5.0 80 | 89469,glavito02,2007,1,NYN,NL,33,56,3,12,1,0,0,4.0,0.0,0.0,6,5.0,0.0,0.0,12.0,1.0,0.0 81 | 89473,floydcl01,2007,1,CHN,NL,108,282,40,80,10,1,9,45.0,0.0,0.0,35,47.0,5.0,5.0,0.0,0.0,6.0 82 | 89474,finlest01,2007,1,COL,NL,43,94,9,17,3,0,1,2.0,0.0,0.0,8,4.0,1.0,0.0,0.0,0.0,2.0 83 | 89480,embreal01,2007,1,OAK,AL,4,0,0,0,0,0,0,0.0,0.0,0.0,0,0.0,0.0,0.0,0.0,0.0,0.0 84 | 89481,edmonji01,2007,1,SLN,NL,117,365,39,92,15,2,12,53.0,0.0,2.0,41,75.0,2.0,0.0,2.0,3.0,9.0 85 | 89482,easleda01,2007,1,NYN,NL,76,193,24,54,6,0,10,26.0,0.0,1.0,19,35.0,1.0,5.0,0.0,1.0,2.0 86 | 89489,delgaca01,2007,1,NYN,NL,139,538,71,139,30,0,24,87.0,4.0,0.0,52,118.0,8.0,11.0,0.0,6.0,12.0 87 | 89493,cormirh01,2007,1,CIN,NL,6,0,0,0,0,0,0,0.0,0.0,0.0,0,0.0,0.0,0.0,0.0,0.0,0.0 88 | 89494,coninje01,2007,2,NYN,NL,21,41,2,8,2,0,0,5.0,0.0,0.0,7,8.0,2.0,0.0,1.0,1.0,1.0 89 | 89495,coninje01,2007,1,CIN,NL,80,215,23,57,11,1,6,32.0,4.0,0.0,20,28.0,0.0,0.0,1.0,6.0,4.0 90 | 89497,clemero02,2007,1,NYA,AL,2,2,0,1,0,0,0,0.0,0.0,0.0,0,0.0,0.0,0.0,0.0,0.0,0.0 91 | 89498,claytro01,2007,2,BOS,AL,8,6,1,0,0,0,0,0.0,0.0,0.0,0,3.0,0.0,0.0,0.0,0.0,2.0 92 | 89499,claytro01,2007,1,TOR,AL,69,189,23,48,14,0,1,12.0,2.0,1.0,14,50.0,0.0,1.0,3.0,3.0,8.0 93 | 89501,cirilje01,2007,2,ARI,NL,28,40,6,8,4,0,0,6.0,0.0,0.0,4,6.0,0.0,0.0,0.0,0.0,1.0 94 | 89502,cirilje01,2007,1,MIN,AL,50,153,18,40,9,2,2,21.0,2.0,0.0,15,13.0,0.0,1.0,3.0,2.0,9.0 95 | 89521,bondsba01,2007,1,SFN,NL,126,340,75,94,14,0,28,66.0,5.0,0.0,132,54.0,43.0,3.0,0.0,2.0,13.0 96 | 89523,biggicr01,2007,1,HOU,NL,141,517,68,130,31,3,10,50.0,4.0,3.0,23,112.0,0.0,3.0,7.0,5.0,5.0 97 | 89525,benitar01,2007,2,FLO,NL,34,0,0,0,0,0,0,0.0,0.0,0.0,0,0.0,0.0,0.0,0.0,0.0,0.0 98 | 89526,benitar01,2007,1,SFN,NL,19,0,0,0,0,0,0,0.0,0.0,0.0,0,0.0,0.0,0.0,0.0,0.0,0.0 99 | 89530,ausmubr01,2007,1,HOU,NL,117,349,38,82,16,3,3,25.0,6.0,1.0,37,74.0,3.0,6.0,4.0,1.0,11.0 100 | 89533,aloumo01,2007,1,NYN,NL,87,328,51,112,19,1,13,49.0,3.0,0.0,27,30.0,5.0,2.0,0.0,3.0,13.0 101 | 89534,alomasa02,2007,1,NYN,NL,8,22,1,3,1,0,0,0.0,0.0,0.0,0,3.0,0.0,0.0,0.0,0.0,0.0 --------------------------------------------------------------------------------