Talk given at RMACC August 17, 2017 titled "Practical Data Wrangling in Pandas".
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1_data_structures.ipynb 42 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623
  1. {
  2. "cells": [
  3. {
  4. "cell_type": "markdown",
  5. "metadata": {},
  6. "source": [
  7. "** NAVIGATION **\n",
  8. "\n",
  9. "**Got Pandas? _Practical Data Wrangling with Pandas_**\n",
  10. "\n",
  11. "* [Introduction](./0_introduction.ipynb)\n",
  12. "1. **Data Structures**\n",
  13. "2. [Importing Data](./2_importing_data.ipynb)\n",
  14. "3. [Manipulating DataFrames](./3_dataframe_operations.ipynb)\n",
  15. "4. [Wrap Up](./3_wrapping_up.ipynb)\n",
  16. "\n",
  17. "---"
  18. ]
  19. },
  20. {
  21. "cell_type": "markdown",
  22. "metadata": {
  23. "toc": "true"
  24. },
  25. "source": [
  26. "# Table of Contents\n",
  27. " <p><div class=\"lev1 toc-item\"><a href=\"#Core-Pandas-Data-Structures\" data-toc-modified-id=\"Core-Pandas-Data-Structures-1\"><span class=\"toc-item-num\">1&nbsp;&nbsp;</span>Core Pandas Data Structures</a></div><div class=\"lev2 toc-item\"><a href=\"#Series\" data-toc-modified-id=\"Series-11\"><span class=\"toc-item-num\">1.1&nbsp;&nbsp;</span>Series</a></div><div class=\"lev2 toc-item\"><a href=\"#DataFrames\" data-toc-modified-id=\"DataFrames-12\"><span class=\"toc-item-num\">1.2&nbsp;&nbsp;</span>DataFrames</a></div><div class=\"lev3 toc-item\"><a href=\"#[]-operator-for-basic-slicing\" data-toc-modified-id=\"[]-operator-for-basic-slicing-121\"><span class=\"toc-item-num\">1.2.1&nbsp;&nbsp;</span><code>[]</code> operator for basic slicing</a></div><div class=\"lev3 toc-item\"><a href=\"#iloc[]\" data-toc-modified-id=\"iloc[]-122\"><span class=\"toc-item-num\">1.2.2&nbsp;&nbsp;</span><code>iloc[]</code></a></div><div class=\"lev3 toc-item\"><a href=\"#More-sophisticated-slicing\" data-toc-modified-id=\"More-sophisticated-slicing-123\"><span class=\"toc-item-num\">1.2.3&nbsp;&nbsp;</span>More sophisticated slicing</a></div><div class=\"lev3 toc-item\"><a href=\"#loc()\" data-toc-modified-id=\"loc()-124\"><span class=\"toc-item-num\">1.2.4&nbsp;&nbsp;</span><code>loc()</code></a></div>"
  28. ]
  29. },
  30. {
  31. "cell_type": "markdown",
  32. "metadata": {},
  33. "source": [
  34. "---"
  35. ]
  36. },
  37. {
  38. "cell_type": "markdown",
  39. "metadata": {},
  40. "source": [
  41. "**NOTEBOOK OBJECTIVES**\n",
  42. "\n",
  43. "In this notebook we'll:\n",
  44. "\n",
  45. "* explore the Series and DataFrame data structures, \n",
  46. "* understand basic selection and slicing operation in each."
  47. ]
  48. },
  49. {
  50. "cell_type": "markdown",
  51. "metadata": {},
  52. "source": [
  53. "# Core Pandas Data Structures\n",
  54. "\n",
  55. "As we move through the content, we will be working with two of Pandas primary data structures. There are more, but we will only focus on these two:\n",
  56. "\n",
  57. "* Series \n",
  58. "* DataFrames\n",
  59. "\n",
  60. "The structures we won't have time to explore are [Panels](http://pandas.pydata.org/pandas-docs/stable/dsintro.html#panel), which should be explored when you're ready to do so.\n",
  61. "\n",
  62. "There are some key ideas behind the data structures provided by Pandas:\n",
  63. "\n",
  64. "* data may be heterogeneous\n",
  65. "* when data is numeric, convenience functions exist to provide aggregate statistical operations (`min()`, `max()`, `cumsum()`, `median()`, `mode()`, etc.),\n",
  66. "* data structures are decomposable and composable, that is making DataFrames from Series or Series from DataFrame is supported natively,\n",
  67. "* data structures are translatable, that is you can create Numpy [`NDArray`](https://docs.scipy.org/doc/numpy/reference/generated/numpy.ndarray.html#numpy.ndarray)."
  68. ]
  69. },
  70. {
  71. "cell_type": "markdown",
  72. "metadata": {},
  73. "source": [
  74. "## Series\n",
  75. "\n",
  76. "The Pandas [Series](http://pandas.pydata.org/pandas-docs/stable/generated/pandas.Series.html) data structure is a one dimensional structure much like a vector, that has axis labels. Series objects can be initialized from an array-like data object, dictionary or scalar value."
  77. ]
  78. },
  79. {
  80. "cell_type": "code",
  81. "execution_count": 1,
  82. "metadata": {
  83. "collapsed": true
  84. },
  85. "outputs": [],
  86. "source": [
  87. "import numpy as np\n",
  88. "import pandas as pd"
  89. ]
  90. },
  91. {
  92. "cell_type": "code",
  93. "execution_count": 2,
  94. "metadata": {},
  95. "outputs": [
  96. {
  97. "data": {
  98. "text/plain": [
  99. "0 43\n",
  100. "1 3\n",
  101. "2 15\n",
  102. "3 20\n",
  103. "4 3\n",
  104. "5 45\n",
  105. "6 44\n",
  106. "7 30\n",
  107. "8 25\n",
  108. "9 48\n",
  109. "dtype: int32"
  110. ]
  111. },
  112. "execution_count": 2,
  113. "metadata": {},
  114. "output_type": "execute_result"
  115. }
  116. ],
  117. "source": [
  118. "np_ints = np.random.randint(0,51,10)\n",
  119. "pd.Series(np_ints)"
  120. ]
  121. },
  122. {
  123. "cell_type": "markdown",
  124. "metadata": {},
  125. "source": [
  126. "Data in a series do not have to be numeric:"
  127. ]
  128. },
  129. {
  130. "cell_type": "code",
  131. "execution_count": 3,
  132. "metadata": {},
  133. "outputs": [
  134. {
  135. "data": {
  136. "text/plain": [
  137. "0 WhTIhhttjy\n",
  138. "1 ubtcrWeUrE\n",
  139. "2 pPzpwOsKpm\n",
  140. "3 pQLCcUiotK\n",
  141. "4 AiOwCuildy\n",
  142. "5 DCkniyiWqp\n",
  143. "6 TOCDhTYkFw\n",
  144. "7 ziJpNNTbRo\n",
  145. "8 UjveUhQFFm\n",
  146. "9 EDEqSQpCKV\n",
  147. "dtype: object"
  148. ]
  149. },
  150. "execution_count": 3,
  151. "metadata": {},
  152. "output_type": "execute_result"
  153. }
  154. ],
  155. "source": [
  156. "import string\n",
  157. "random_letters = [''\n",
  158. " .join([string.ascii_letters[c] for c in np.random.randint(0,51,10)]) \n",
  159. " for i in range(10)]\n",
  160. "pd.Series(random_letters)"
  161. ]
  162. },
  163. {
  164. "cell_type": "markdown",
  165. "metadata": {},
  166. "source": [
  167. "We can specify an index if we'd like that will allow us to have meaningful labels to access the data in the Series:"
  168. ]
  169. },
  170. {
  171. "cell_type": "code",
  172. "execution_count": 4,
  173. "metadata": {},
  174. "outputs": [
  175. {
  176. "data": {
  177. "text/plain": [
  178. "city Denver\n",
  179. "state CO\n",
  180. "zip 80023\n",
  181. "neigborhood Furhman\n",
  182. "area_code 303\n",
  183. "dtype: object"
  184. ]
  185. },
  186. "execution_count": 4,
  187. "metadata": {},
  188. "output_type": "execute_result"
  189. }
  190. ],
  191. "source": [
  192. "index = ['city', 'state', 'zip', 'neigborhood', 'area_code']\n",
  193. "data = ['Denver', 'CO', '80023', 'Furhman', '303']\n",
  194. "s = pd.Series(data, index)\n",
  195. "s"
  196. ]
  197. },
  198. {
  199. "cell_type": "markdown",
  200. "metadata": {},
  201. "source": [
  202. "Now we can access the data by its index label ..."
  203. ]
  204. },
  205. {
  206. "cell_type": "code",
  207. "execution_count": 5,
  208. "metadata": {},
  209. "outputs": [
  210. {
  211. "data": {
  212. "text/plain": [
  213. "('Denver', 'CO')"
  214. ]
  215. },
  216. "execution_count": 5,
  217. "metadata": {},
  218. "output_type": "execute_result"
  219. }
  220. ],
  221. "source": [
  222. "s['city'], s['state']"
  223. ]
  224. },
  225. {
  226. "cell_type": "markdown",
  227. "metadata": {},
  228. "source": [
  229. "Accessing data in a series is much like accessing data in a Python list. The usual slicing operator is available to get at the data in the Series."
  230. ]
  231. },
  232. {
  233. "cell_type": "code",
  234. "execution_count": 6,
  235. "metadata": {},
  236. "outputs": [
  237. {
  238. "data": {
  239. "text/plain": [
  240. "'CO'"
  241. ]
  242. },
  243. "execution_count": 6,
  244. "metadata": {},
  245. "output_type": "execute_result"
  246. }
  247. ],
  248. "source": [
  249. "s[1]"
  250. ]
  251. },
  252. {
  253. "cell_type": "code",
  254. "execution_count": 7,
  255. "metadata": {},
  256. "outputs": [
  257. {
  258. "data": {
  259. "text/plain": [
  260. "city Denver\n",
  261. "state CO\n",
  262. "zip 80023\n",
  263. "dtype: object"
  264. ]
  265. },
  266. "execution_count": 7,
  267. "metadata": {},
  268. "output_type": "execute_result"
  269. }
  270. ],
  271. "source": [
  272. "s[0:3]"
  273. ]
  274. },
  275. {
  276. "cell_type": "code",
  277. "execution_count": 8,
  278. "metadata": {},
  279. "outputs": [
  280. {
  281. "data": {
  282. "text/plain": [
  283. "city Denver\n",
  284. "zip 80023\n",
  285. "dtype: object"
  286. ]
  287. },
  288. "execution_count": 8,
  289. "metadata": {},
  290. "output_type": "execute_result"
  291. }
  292. ],
  293. "source": [
  294. "s[0:3:2]"
  295. ]
  296. },
  297. {
  298. "cell_type": "markdown",
  299. "metadata": {},
  300. "source": [
  301. "More sophisticated slicing by index using integers can be achieved with [`iloc`](http://pandas.pydata.org/pandas-docs/stable/generated/pandas.Series.iloc.html#pandas.Series.iloc). Here are some simple examples:"
  302. ]
  303. },
  304. {
  305. "cell_type": "code",
  306. "execution_count": 9,
  307. "metadata": {},
  308. "outputs": [
  309. {
  310. "data": {
  311. "text/plain": [
  312. "city Denver\n",
  313. "state CO\n",
  314. "zip 80023\n",
  315. "dtype: object"
  316. ]
  317. },
  318. "execution_count": 9,
  319. "metadata": {},
  320. "output_type": "execute_result"
  321. }
  322. ],
  323. "source": [
  324. "s.iloc[0:3]"
  325. ]
  326. },
  327. {
  328. "cell_type": "code",
  329. "execution_count": 10,
  330. "metadata": {},
  331. "outputs": [
  332. {
  333. "data": {
  334. "text/plain": [
  335. "city Denver\n",
  336. "zip 80023\n",
  337. "dtype: object"
  338. ]
  339. },
  340. "execution_count": 10,
  341. "metadata": {},
  342. "output_type": "execute_result"
  343. }
  344. ],
  345. "source": [
  346. "s.iloc[0:3:2]"
  347. ]
  348. },
  349. {
  350. "cell_type": "markdown",
  351. "metadata": {},
  352. "source": [
  353. "We can pass a list of the indices we'd like just as easily ..."
  354. ]
  355. },
  356. {
  357. "cell_type": "code",
  358. "execution_count": 11,
  359. "metadata": {},
  360. "outputs": [
  361. {
  362. "data": {
  363. "text/plain": [
  364. "state CO\n",
  365. "zip 80023\n",
  366. "area_code 303\n",
  367. "dtype: object"
  368. ]
  369. },
  370. "execution_count": 11,
  371. "metadata": {},
  372. "output_type": "execute_result"
  373. }
  374. ],
  375. "source": [
  376. "s.iloc[[1,2,4]]"
  377. ]
  378. },
  379. {
  380. "cell_type": "markdown",
  381. "metadata": {},
  382. "source": [
  383. "To get at all the values of the Series as an [NDArray](https://docs.scipy.org/doc/numpy/reference/arrays.ndarray.html), simply do"
  384. ]
  385. },
  386. {
  387. "cell_type": "code",
  388. "execution_count": 12,
  389. "metadata": {},
  390. "outputs": [
  391. {
  392. "data": {
  393. "text/plain": [
  394. "array(['Denver', 'CO', '80023', 'Furhman', '303'], dtype=object)"
  395. ]
  396. },
  397. "execution_count": 12,
  398. "metadata": {},
  399. "output_type": "execute_result"
  400. }
  401. ],
  402. "source": [
  403. "s.values"
  404. ]
  405. },
  406. {
  407. "cell_type": "markdown",
  408. "metadata": {},
  409. "source": [
  410. "which then allows us to convert to a list"
  411. ]
  412. },
  413. {
  414. "cell_type": "code",
  415. "execution_count": 13,
  416. "metadata": {},
  417. "outputs": [
  418. {
  419. "data": {
  420. "text/plain": [
  421. "['Denver', 'CO', '80023', 'Furhman', '303']"
  422. ]
  423. },
  424. "execution_count": 13,
  425. "metadata": {},
  426. "output_type": "execute_result"
  427. }
  428. ],
  429. "source": [
  430. "s.values.tolist()"
  431. ]
  432. },
  433. {
  434. "cell_type": "markdown",
  435. "metadata": {},
  436. "source": [
  437. "## DataFrames\n",
  438. "\n",
  439. "[DataFrames](http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.html#pandas.DataFrame) are a natural extension to Series in that they are 2-dimensional, and similarly to matrices (from vectors). They have many of the same operations (extended to 2 dimensions), but have some additional properties and operations. We'll cover the basics here.\n",
  440. "\n",
  441. "* a DataFrame has a _row_ and _column_ **axis**, which defaults to numeric values (0 and 1),\n",
  442. "* a DataFrame axis can be multi-level, that is multi-level indices can be created for _row_, _column_ or **both**,\n",
  443. "* DataFrames can be converted to `NDArray` and thus be converted to lists and dictionaries,\n",
  444. "* indexing is achieved either by integer value or index label (both where applicable),\n",
  445. "* DataFrame values may be heterogeous."
  446. ]
  447. },
  448. {
  449. "cell_type": "markdown",
  450. "metadata": {},
  451. "source": [
  452. "Let's first begin by building a DataFrame from a 2D Numpy array ..."
  453. ]
  454. },
  455. {
  456. "cell_type": "code",
  457. "execution_count": 14,
  458. "metadata": {},
  459. "outputs": [
  460. {
  461. "data": {
  462. "text/html": [
  463. "<div>\n",
  464. "<style>\n",
  465. " .dataframe thead tr:only-child th {\n",
  466. " text-align: right;\n",
  467. " }\n",
  468. "\n",
  469. " .dataframe thead th {\n",
  470. " text-align: left;\n",
  471. " }\n",
  472. "\n",
  473. " .dataframe tbody tr th {\n",
  474. " vertical-align: top;\n",
  475. " }\n",
  476. "</style>\n",
  477. "<table border=\"1\" class=\"dataframe\">\n",
  478. " <thead>\n",
  479. " <tr style=\"text-align: right;\">\n",
  480. " <th></th>\n",
  481. " <th>0</th>\n",
  482. " <th>1</th>\n",
  483. " <th>2</th>\n",
  484. " <th>3</th>\n",
  485. " <th>4</th>\n",
  486. " <th>5</th>\n",
  487. " <th>6</th>\n",
  488. " <th>7</th>\n",
  489. " <th>8</th>\n",
  490. " <th>9</th>\n",
  491. " </tr>\n",
  492. " </thead>\n",
  493. " <tbody>\n",
  494. " <tr>\n",
  495. " <th>0</th>\n",
  496. " <td>58</td>\n",
  497. " <td>6</td>\n",
  498. " <td>91</td>\n",
  499. " <td>11</td>\n",
  500. " <td>22</td>\n",
  501. " <td>29</td>\n",
  502. " <td>25</td>\n",
  503. " <td>36</td>\n",
  504. " <td>55</td>\n",
  505. " <td>87</td>\n",
  506. " </tr>\n",
  507. " <tr>\n",
  508. " <th>1</th>\n",
  509. " <td>83</td>\n",
  510. " <td>30</td>\n",
  511. " <td>8</td>\n",
  512. " <td>55</td>\n",
  513. " <td>43</td>\n",
  514. " <td>62</td>\n",
  515. " <td>82</td>\n",
  516. " <td>74</td>\n",
  517. " <td>12</td>\n",
  518. " <td>5</td>\n",
  519. " </tr>\n",
  520. " <tr>\n",
  521. " <th>2</th>\n",
  522. " <td>83</td>\n",
  523. " <td>95</td>\n",
  524. " <td>28</td>\n",
  525. " <td>33</td>\n",
  526. " <td>95</td>\n",
  527. " <td>28</td>\n",
  528. " <td>7</td>\n",
  529. " <td>32</td>\n",
  530. " <td>6</td>\n",
  531. " <td>2</td>\n",
  532. " </tr>\n",
  533. " <tr>\n",
  534. " <th>3</th>\n",
  535. " <td>17</td>\n",
  536. " <td>25</td>\n",
  537. " <td>82</td>\n",
  538. " <td>3</td>\n",
  539. " <td>65</td>\n",
  540. " <td>39</td>\n",
  541. " <td>73</td>\n",
  542. " <td>63</td>\n",
  543. " <td>6</td>\n",
  544. " <td>49</td>\n",
  545. " </tr>\n",
  546. " <tr>\n",
  547. " <th>4</th>\n",
  548. " <td>13</td>\n",
  549. " <td>63</td>\n",
  550. " <td>18</td>\n",
  551. " <td>86</td>\n",
  552. " <td>29</td>\n",
  553. " <td>35</td>\n",
  554. " <td>97</td>\n",
  555. " <td>24</td>\n",
  556. " <td>71</td>\n",
  557. " <td>50</td>\n",
  558. " </tr>\n",
  559. " <tr>\n",
  560. " <th>5</th>\n",
  561. " <td>33</td>\n",
  562. " <td>39</td>\n",
  563. " <td>84</td>\n",
  564. " <td>85</td>\n",
  565. " <td>15</td>\n",
  566. " <td>42</td>\n",
  567. " <td>68</td>\n",
  568. " <td>45</td>\n",
  569. " <td>26</td>\n",
  570. " <td>69</td>\n",
  571. " </tr>\n",
  572. " <tr>\n",
  573. " <th>6</th>\n",
  574. " <td>55</td>\n",
  575. " <td>27</td>\n",
  576. " <td>17</td>\n",
  577. " <td>44</td>\n",
  578. " <td>78</td>\n",
  579. " <td>19</td>\n",
  580. " <td>38</td>\n",
  581. " <td>63</td>\n",
  582. " <td>31</td>\n",
  583. " <td>60</td>\n",
  584. " </tr>\n",
  585. " <tr>\n",
  586. " <th>7</th>\n",
  587. " <td>41</td>\n",
  588. " <td>19</td>\n",
  589. " <td>48</td>\n",
  590. " <td>36</td>\n",
  591. " <td>92</td>\n",
  592. " <td>35</td>\n",
  593. " <td>41</td>\n",
  594. " <td>97</td>\n",
  595. " <td>98</td>\n",
  596. " <td>10</td>\n",
  597. " </tr>\n",
  598. " <tr>\n",
  599. " <th>8</th>\n",
  600. " <td>59</td>\n",
  601. " <td>65</td>\n",
  602. " <td>67</td>\n",
  603. " <td>58</td>\n",
  604. " <td>36</td>\n",
  605. " <td>84</td>\n",
  606. " <td>8</td>\n",
  607. " <td>45</td>\n",
  608. " <td>16</td>\n",
  609. " <td>76</td>\n",
  610. " </tr>\n",
  611. " <tr>\n",
  612. " <th>9</th>\n",
  613. " <td>56</td>\n",
  614. " <td>36</td>\n",
  615. " <td>98</td>\n",
  616. " <td>63</td>\n",
  617. " <td>73</td>\n",
  618. " <td>54</td>\n",
  619. " <td>36</td>\n",
  620. " <td>61</td>\n",
  621. " <td>56</td>\n",
  622. " <td>73</td>\n",
  623. " </tr>\n",
  624. " </tbody>\n",
  625. "</table>\n",
  626. "</div>"
  627. ],
  628. "text/plain": [
  629. " 0 1 2 3 4 5 6 7 8 9\n",
  630. "0 58 6 91 11 22 29 25 36 55 87\n",
  631. "1 83 30 8 55 43 62 82 74 12 5\n",
  632. "2 83 95 28 33 95 28 7 32 6 2\n",
  633. "3 17 25 82 3 65 39 73 63 6 49\n",
  634. "4 13 63 18 86 29 35 97 24 71 50\n",
  635. "5 33 39 84 85 15 42 68 45 26 69\n",
  636. "6 55 27 17 44 78 19 38 63 31 60\n",
  637. "7 41 19 48 36 92 35 41 97 98 10\n",
  638. "8 59 65 67 58 36 84 8 45 16 76\n",
  639. "9 56 36 98 63 73 54 36 61 56 73"
  640. ]
  641. },
  642. "execution_count": 14,
  643. "metadata": {},
  644. "output_type": "execute_result"
  645. }
  646. ],
  647. "source": [
  648. "df = pd.DataFrame(np.random.randint(1,100,100).reshape(10,10))\n",
  649. "df"
  650. ]
  651. },
  652. {
  653. "cell_type": "markdown",
  654. "metadata": {},
  655. "source": [
  656. "### `[]` operator for basic slicing\n",
  657. "\n",
  658. "The slicing operator `[]` works on DataFrames over **row slices**. Getting at data in the DataFrame is otherwise done with the [`iloc[]`](http://pandas.pydata.org/pandas-docs/version/0.17.0/generated/pandas.DataFrame.iloc.html) and [`loc[]`](http://pandas.pydata.org/pandas-docs/version/0.17.0/generated/pandas.DataFrame.loc.html) selectors.\n",
  659. "\n",
  660. "Be mindful to use this operator sparingly it is not consistent with `loc` and `iloc`, and may create confusing code if mixed arbitrarily with those selectors. Let's see a few basic cases for `[]` ..."
  661. ]
  662. },
  663. {
  664. "cell_type": "code",
  665. "execution_count": 15,
  666. "metadata": {},
  667. "outputs": [
  668. {
  669. "data": {
  670. "text/html": [
  671. "<div>\n",
  672. "<style>\n",
  673. " .dataframe thead tr:only-child th {\n",
  674. " text-align: right;\n",
  675. " }\n",
  676. "\n",
  677. " .dataframe thead th {\n",
  678. " text-align: left;\n",
  679. " }\n",
  680. "\n",
  681. " .dataframe tbody tr th {\n",
  682. " vertical-align: top;\n",
  683. " }\n",
  684. "</style>\n",
  685. "<table border=\"1\" class=\"dataframe\">\n",
  686. " <thead>\n",
  687. " <tr style=\"text-align: right;\">\n",
  688. " <th></th>\n",
  689. " <th>0</th>\n",
  690. " <th>1</th>\n",
  691. " <th>2</th>\n",
  692. " <th>3</th>\n",
  693. " <th>4</th>\n",
  694. " <th>5</th>\n",
  695. " <th>6</th>\n",
  696. " <th>7</th>\n",
  697. " <th>8</th>\n",
  698. " <th>9</th>\n",
  699. " </tr>\n",
  700. " </thead>\n",
  701. " <tbody>\n",
  702. " <tr>\n",
  703. " <th>1</th>\n",
  704. " <td>83</td>\n",
  705. " <td>30</td>\n",
  706. " <td>8</td>\n",
  707. " <td>55</td>\n",
  708. " <td>43</td>\n",
  709. " <td>62</td>\n",
  710. " <td>82</td>\n",
  711. " <td>74</td>\n",
  712. " <td>12</td>\n",
  713. " <td>5</td>\n",
  714. " </tr>\n",
  715. " <tr>\n",
  716. " <th>2</th>\n",
  717. " <td>83</td>\n",
  718. " <td>95</td>\n",
  719. " <td>28</td>\n",
  720. " <td>33</td>\n",
  721. " <td>95</td>\n",
  722. " <td>28</td>\n",
  723. " <td>7</td>\n",
  724. " <td>32</td>\n",
  725. " <td>6</td>\n",
  726. " <td>2</td>\n",
  727. " </tr>\n",
  728. " <tr>\n",
  729. " <th>3</th>\n",
  730. " <td>17</td>\n",
  731. " <td>25</td>\n",
  732. " <td>82</td>\n",
  733. " <td>3</td>\n",
  734. " <td>65</td>\n",
  735. " <td>39</td>\n",
  736. " <td>73</td>\n",
  737. " <td>63</td>\n",
  738. " <td>6</td>\n",
  739. " <td>49</td>\n",
  740. " </tr>\n",
  741. " </tbody>\n",
  742. "</table>\n",
  743. "</div>"
  744. ],
  745. "text/plain": [
  746. " 0 1 2 3 4 5 6 7 8 9\n",
  747. "1 83 30 8 55 43 62 82 74 12 5\n",
  748. "2 83 95 28 33 95 28 7 32 6 2\n",
  749. "3 17 25 82 3 65 39 73 63 6 49"
  750. ]
  751. },
  752. "execution_count": 15,
  753. "metadata": {},
  754. "output_type": "execute_result"
  755. }
  756. ],
  757. "source": [
  758. "df[1:4] # selecting the rows index 1 to 4"
  759. ]
  760. },
  761. {
  762. "cell_type": "code",
  763. "execution_count": 16,
  764. "metadata": {},
  765. "outputs": [
  766. {
  767. "data": {
  768. "text/plain": [
  769. "0 6\n",
  770. "1 30\n",
  771. "2 95\n",
  772. "3 25\n",
  773. "Name: 1, dtype: int32"
  774. ]
  775. },
  776. "execution_count": 16,
  777. "metadata": {},
  778. "output_type": "execute_result"
  779. }
  780. ],
  781. "source": [
  782. "df[0:4][1] # selecting rows index 0 to 4, column index 1"
  783. ]
  784. },
  785. {
  786. "cell_type": "code",
  787. "execution_count": 17,
  788. "metadata": {},
  789. "outputs": [
  790. {
  791. "data": {
  792. "text/plain": [
  793. "0 6\n",
  794. "1 30\n",
  795. "2 95\n",
  796. "3 25\n",
  797. "4 63\n",
  798. "5 39\n",
  799. "6 27\n",
  800. "7 19\n",
  801. "8 65\n",
  802. "9 36\n",
  803. "Name: 1, dtype: int32"
  804. ]
  805. },
  806. "execution_count": 17,
  807. "metadata": {},
  808. "output_type": "execute_result"
  809. }
  810. ],
  811. "source": [
  812. "df[1] # selecting column 1, rows 0 .. n"
  813. ]
  814. },
  815. {
  816. "cell_type": "code",
  817. "execution_count": 1,
  818. "metadata": {},
  819. "outputs": [
  820. {
  821. "ename": "NameError",
  822. "evalue": "name 'df' is not defined",
  823. "output_type": "error",
  824. "traceback": [
  825. "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
  826. "\u001b[1;31mNameError\u001b[0m Traceback (most recent call last)",
  827. "\u001b[1;32m<ipython-input-1-d68189ac4f7e>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mdf\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m4\u001b[0m\u001b[1;33m]\u001b[0m \u001b[1;31m# selecting the value at column index 1, row index 4\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
  828. "\u001b[1;31mNameError\u001b[0m: name 'df' is not defined"
  829. ]
  830. }
  831. ],
  832. "source": [
  833. "df[1][4] # selecting the value at column index 1, row index 4"
  834. ]
  835. },
  836. {
  837. "cell_type": "markdown",
  838. "metadata": {},
  839. "source": [
  840. "The `[]` operator is merely a convenience and does not provide the functionality of `iloc()` and `loc()` discussed below."
  841. ]
  842. },
  843. {
  844. "cell_type": "markdown",
  845. "metadata": {},
  846. "source": [
  847. "### `iloc[]`\n",
  848. "\n",
  849. "`iloc` is an integer-based selector. As such, you will need to know the integer values of the _row_ or _column_ indices as necessary. You may see some correspondence with the `[]` selector, but it provides much more."
  850. ]
  851. },
  852. {
  853. "cell_type": "code",
  854. "execution_count": 19,
  855. "metadata": {},
  856. "outputs": [
  857. {
  858. "data": {
  859. "text/plain": [
  860. "0 83\n",
  861. "1 30\n",
  862. "2 8\n",
  863. "3 55\n",
  864. "4 43\n",
  865. "5 62\n",
  866. "6 82\n",
  867. "7 74\n",
  868. "8 12\n",
  869. "9 5\n",
  870. "Name: 1, dtype: int32"
  871. ]
  872. },
  873. "execution_count": 19,
  874. "metadata": {},
  875. "output_type": "execute_result"
  876. }
  877. ],
  878. "source": [
  879. "df.iloc[1] # row index 1, returns the full ROW"
  880. ]
  881. },
  882. {
  883. "cell_type": "code",
  884. "execution_count": 20,
  885. "metadata": {},
  886. "outputs": [
  887. {
  888. "data": {
  889. "text/plain": [
  890. "8"
  891. ]
  892. },
  893. "execution_count": 20,
  894. "metadata": {},
  895. "output_type": "execute_result"
  896. }
  897. ],
  898. "source": [
  899. "df.iloc[1,2] # row index 1, column index 2"
  900. ]
  901. },
  902. {
  903. "cell_type": "code",
  904. "execution_count": 21,
  905. "metadata": {},
  906. "outputs": [
  907. {
  908. "data": {
  909. "text/plain": [
  910. "0 83\n",
  911. "1 30\n",
  912. "2 8\n",
  913. "3 55\n",
  914. "4 43\n",
  915. "5 62\n",
  916. "6 82\n",
  917. "7 74\n",
  918. "8 12\n",
  919. "9 5\n",
  920. "Name: 1, dtype: int32"
  921. ]
  922. },
  923. "execution_count": 21,
  924. "metadata": {},
  925. "output_type": "execute_result"
  926. }
  927. ],
  928. "source": [
  929. "df.iloc[1,:] # row index 1, as above"
  930. ]
  931. },
  932. {
  933. "cell_type": "code",
  934. "execution_count": 22,
  935. "metadata": {},
  936. "outputs": [
  937. {
  938. "data": {
  939. "text/plain": [
  940. "2 8\n",
  941. "3 55\n",
  942. "Name: 1, dtype: int32"
  943. ]
  944. },
  945. "execution_count": 22,
  946. "metadata": {},
  947. "output_type": "execute_result"
  948. }
  949. ],
  950. "source": [
  951. "df.iloc[1,2:4] # row index 2, column index 2:3"
  952. ]
  953. },
  954. {
  955. "cell_type": "code",
  956. "execution_count": 23,
  957. "metadata": {},
  958. "outputs": [
  959. {
  960. "data": {
  961. "text/html": [
  962. "<div>\n",
  963. "<style>\n",
  964. " .dataframe thead tr:only-child th {\n",
  965. " text-align: right;\n",
  966. " }\n",
  967. "\n",
  968. " .dataframe thead th {\n",
  969. " text-align: left;\n",
  970. " }\n",
  971. "\n",
  972. " .dataframe tbody tr th {\n",
  973. " vertical-align: top;\n",
  974. " }\n",
  975. "</style>\n",
  976. "<table border=\"1\" class=\"dataframe\">\n",
  977. " <thead>\n",
  978. " <tr style=\"text-align: right;\">\n",
  979. " <th></th>\n",
  980. " <th>0</th>\n",
  981. " <th>1</th>\n",
  982. " <th>2</th>\n",
  983. " <th>3</th>\n",
  984. " <th>4</th>\n",
  985. " <th>5</th>\n",
  986. " <th>6</th>\n",
  987. " <th>7</th>\n",
  988. " <th>8</th>\n",
  989. " <th>9</th>\n",
  990. " </tr>\n",
  991. " </thead>\n",
  992. " <tbody>\n",
  993. " <tr>\n",
  994. " <th>1</th>\n",
  995. " <td>83</td>\n",
  996. " <td>30</td>\n",
  997. " <td>8</td>\n",
  998. " <td>55</td>\n",
  999. " <td>43</td>\n",
  1000. " <td>62</td>\n",
  1001. " <td>82</td>\n",
  1002. " <td>74</td>\n",
  1003. " <td>12</td>\n",
  1004. " <td>5</td>\n",
  1005. " </tr>\n",
  1006. " </tbody>\n",
  1007. "</table>\n",
  1008. "</div>"
  1009. ],
  1010. "text/plain": [
  1011. " 0 1 2 3 4 5 6 7 8 9\n",
  1012. "1 83 30 8 55 43 62 82 74 12 5"
  1013. ]
  1014. },
  1015. "execution_count": 23,
  1016. "metadata": {},
  1017. "output_type": "execute_result"
  1018. }
  1019. ],
  1020. "source": [
  1021. "df.iloc[1:2,:] # row index 1:2, column index 0:-1 same as above"
  1022. ]
  1023. },
  1024. {
  1025. "cell_type": "markdown",
  1026. "metadata": {},
  1027. "source": [
  1028. "### More sophisticated slicing\n",
  1029. "\n",
  1030. "\n",
  1031. "More sophisticated slicing can be done over integer indices. For example, if we wanted specific rows and columns slicing, we can something like the following. Remember that the first argument to the selector is the _row_ and the second, the _column_."
  1032. ]
  1033. },
  1034. {
  1035. "cell_type": "code",
  1036. "execution_count": 24,
  1037. "metadata": {},
  1038. "outputs": [
  1039. {
  1040. "data": {
  1041. "text/html": [
  1042. "<div>\n",
  1043. "<style>\n",
  1044. " .dataframe thead tr:only-child th {\n",
  1045. " text-align: right;\n",
  1046. " }\n",
  1047. "\n",
  1048. " .dataframe thead th {\n",
  1049. " text-align: left;\n",
  1050. " }\n",
  1051. "\n",
  1052. " .dataframe tbody tr th {\n",
  1053. " vertical-align: top;\n",
  1054. " }\n",
  1055. "</style>\n",
  1056. "<table border=\"1\" class=\"dataframe\">\n",
  1057. " <thead>\n",
  1058. " <tr style=\"text-align: right;\">\n",
  1059. " <th></th>\n",
  1060. " <th>2</th>\n",
  1061. " <th>3</th>\n",
  1062. " <th>4</th>\n",
  1063. " </tr>\n",
  1064. " </thead>\n",
  1065. " <tbody>\n",
  1066. " <tr>\n",
  1067. " <th>1</th>\n",
  1068. " <td>8</td>\n",
  1069. " <td>55</td>\n",
  1070. " <td>43</td>\n",
  1071. " </tr>\n",
  1072. " </tbody>\n",
  1073. "</table>\n",
  1074. "</div>"
  1075. ],
  1076. "text/plain": [
  1077. " 2 3 4\n",
  1078. "1 8 55 43"
  1079. ]
  1080. },
  1081. "execution_count": 24,
  1082. "metadata": {},
  1083. "output_type": "execute_result"
  1084. }
  1085. ],
  1086. "source": [
  1087. "df.iloc[1:2,2:5] # row index 1:2, column index 2:5"
  1088. ]
  1089. },
  1090. {
  1091. "cell_type": "code",
  1092. "execution_count": 25,
  1093. "metadata": {},
  1094. "outputs": [
  1095. {
  1096. "data": {
  1097. "text/html": [
  1098. "<div>\n",
  1099. "<style>\n",
  1100. " .dataframe thead tr:only-child th {\n",
  1101. " text-align: right;\n",
  1102. " }\n",
  1103. "\n",
  1104. " .dataframe thead th {\n",
  1105. " text-align: left;\n",
  1106. " }\n",
  1107. "\n",
  1108. " .dataframe tbody tr th {\n",
  1109. " vertical-align: top;\n",
  1110. " }\n",
  1111. "</style>\n",
  1112. "<table border=\"1\" class=\"dataframe\">\n",
  1113. " <thead>\n",
  1114. " <tr style=\"text-align: right;\">\n",
  1115. " <th></th>\n",
  1116. " <th>2</th>\n",
  1117. " <th>5</th>\n",
  1118. " <th>6</th>\n",
  1119. " </tr>\n",
  1120. " </thead>\n",
  1121. " <tbody>\n",
  1122. " <tr>\n",
  1123. " <th>1</th>\n",
  1124. " <td>8</td>\n",
  1125. " <td>62</td>\n",
  1126. " <td>82</td>\n",
  1127. " </tr>\n",
  1128. " <tr>\n",
  1129. " <th>3</th>\n",
  1130. " <td>82</td>\n",
  1131. " <td>39</td>\n",
  1132. " <td>73</td>\n",
  1133. " </tr>\n",
  1134. " <tr>\n",
  1135. " <th>7</th>\n",
  1136. " <td>48</td>\n",
  1137. " <td>35</td>\n",
  1138. " <td>41</td>\n",
  1139. " </tr>\n",
  1140. " </tbody>\n",
  1141. "</table>\n",
  1142. "</div>"
  1143. ],
  1144. "text/plain": [
  1145. " 2 5 6\n",
  1146. "1 8 62 82\n",
  1147. "3 82 39 73\n",
  1148. "7 48 35 41"
  1149. ]
  1150. },
  1151. "execution_count": 25,
  1152. "metadata": {},
  1153. "output_type": "execute_result"
  1154. }
  1155. ],
  1156. "source": [
  1157. "df.iloc[[1,3,7], [2,5,6]] # row indices 1,3,7 column indices 2,5,6"
  1158. ]
  1159. },
  1160. {
  1161. "cell_type": "markdown",
  1162. "metadata": {},
  1163. "source": [
  1164. "### `loc()`\n",
  1165. "\n",
  1166. "`loc()` is a label-based selector, and provides a much richer experience in selecting data. It improves the overall readibility of analysis code and also allows for multi-indices to become more easily understood, as complex multi-indices that are numeric are often difficult to follow when complexity increases.\n",
  1167. "\n",
  1168. "We will create a DataFrame with explicit index and column labels, filling in the values of our previous DataFrame `df` above."
  1169. ]
  1170. },
  1171. {
  1172. "cell_type": "code",
  1173. "execution_count": 26,
  1174. "metadata": {
  1175. "collapsed": true
  1176. },
  1177. "outputs": [],
  1178. "source": [
  1179. "df_si = pd.DataFrame(df.values, \n",
  1180. " index=['r{}'.format(i) for i in range(0,10)],\n",
  1181. " columns=['c{}'.format(i) for i in range(0,10)])"
  1182. ]
  1183. },
  1184. {
  1185. "cell_type": "code",
  1186. "execution_count": 27,
  1187. "metadata": {},
  1188. "outputs": [
  1189. {
  1190. "data": {
  1191. "text/html": [
  1192. "<div>\n",
  1193. "<style>\n",
  1194. " .dataframe thead tr:only-child th {\n",
  1195. " text-align: right;\n",
  1196. " }\n",
  1197. "\n",
  1198. " .dataframe thead th {\n",
  1199. " text-align: left;\n",
  1200. " }\n",
  1201. "\n",
  1202. " .dataframe tbody tr th {\n",
  1203. " vertical-align: top;\n",
  1204. " }\n",
  1205. "</style>\n",
  1206. "<table border=\"1\" class=\"dataframe\">\n",
  1207. " <thead>\n",
  1208. " <tr style=\"text-align: right;\">\n",
  1209. " <th></th>\n",
  1210. " <th>c0</th>\n",
  1211. " <th>c1</th>\n",
  1212. " <th>c2</th>\n",
  1213. " <th>c3</th>\n",
  1214. " <th>c4</th>\n",
  1215. " <th>c5</th>\n",
  1216. " <th>c6</th>\n",
  1217. " <th>c7</th>\n",
  1218. " <th>c8</th>\n",
  1219. " <th>c9</th>\n",
  1220. " </tr>\n",
  1221. " </thead>\n",
  1222. " <tbody>\n",
  1223. " <tr>\n",
  1224. " <th>r0</th>\n",
  1225. " <td>58</td>\n",
  1226. " <td>6</td>\n",
  1227. " <td>91</td>\n",
  1228. " <td>11</td>\n",
  1229. " <td>22</td>\n",
  1230. " <td>29</td>\n",
  1231. " <td>25</td>\n",
  1232. " <td>36</td>\n",
  1233. " <td>55</td>\n",
  1234. " <td>87</td>\n",
  1235. " </tr>\n",
  1236. " <tr>\n",
  1237. " <th>r1</th>\n",
  1238. " <td>83</td>\n",
  1239. " <td>30</td>\n",
  1240. " <td>8</td>\n",
  1241. " <td>55</td>\n",
  1242. " <td>43</td>\n",
  1243. " <td>62</td>\n",
  1244. " <td>82</td>\n",
  1245. " <td>74</td>\n",
  1246. " <td>12</td>\n",
  1247. " <td>5</td>\n",
  1248. " </tr>\n",
  1249. " <tr>\n",
  1250. " <th>r2</th>\n",
  1251. " <td>83</td>\n",
  1252. " <td>95</td>\n",
  1253. " <td>28</td>\n",
  1254. " <td>33</td>\n",
  1255. " <td>95</td>\n",
  1256. " <td>28</td>\n",
  1257. " <td>7</td>\n",
  1258. " <td>32</td>\n",
  1259. " <td>6</td>\n",
  1260. " <td>2</td>\n",
  1261. " </tr>\n",
  1262. " <tr>\n",
  1263. " <th>r3</th>\n",
  1264. " <td>17</td>\n",
  1265. " <td>25</td>\n",
  1266. " <td>82</td>\n",
  1267. " <td>3</td>\n",
  1268. " <td>65</td>\n",
  1269. " <td>39</td>\n",
  1270. " <td>73</td>\n",
  1271. " <td>63</td>\n",
  1272. " <td>6</td>\n",
  1273. " <td>49</td>\n",
  1274. " </tr>\n",
  1275. " <tr>\n",
  1276. " <th>r4</th>\n",
  1277. " <td>13</td>\n",
  1278. " <td>63</td>\n",
  1279. " <td>18</td>\n",
  1280. " <td>86</td>\n",
  1281. " <td>29</td>\n",
  1282. " <td>35</td>\n",
  1283. " <td>97</td>\n",
  1284. " <td>24</td>\n",
  1285. " <td>71</td>\n",
  1286. " <td>50</td>\n",
  1287. " </tr>\n",
  1288. " <tr>\n",
  1289. " <th>r5</th>\n",
  1290. " <td>33</td>\n",
  1291. " <td>39</td>\n",
  1292. " <td>84</td>\n",
  1293. " <td>85</td>\n",
  1294. " <td>15</td>\n",
  1295. " <td>42</td>\n",
  1296. " <td>68</td>\n",
  1297. " <td>45</td>\n",
  1298. " <td>26</td>\n",
  1299. " <td>69</td>\n",
  1300. " </tr>\n",
  1301. " <tr>\n",
  1302. " <th>r6</th>\n",
  1303. " <td>55</td>\n",
  1304. " <td>27</td>\n",
  1305. " <td>17</td>\n",
  1306. " <td>44</td>\n",
  1307. " <td>78</td>\n",
  1308. " <td>19</td>\n",
  1309. " <td>38</td>\n",
  1310. " <td>63</td>\n",
  1311. " <td>31</td>\n",
  1312. " <td>60</td>\n",
  1313. " </tr>\n",
  1314. " <tr>\n",
  1315. " <th>r7</th>\n",
  1316. " <td>41</td>\n",
  1317. " <td>19</td>\n",
  1318. " <td>48</td>\n",
  1319. " <td>36</td>\n",
  1320. " <td>92</td>\n",
  1321. " <td>35</td>\n",
  1322. " <td>41</td>\n",
  1323. " <td>97</td>\n",
  1324. " <td>98</td>\n",
  1325. " <td>10</td>\n",
  1326. " </tr>\n",
  1327. " <tr>\n",
  1328. " <th>r8</th>\n",
  1329. " <td>59</td>\n",
  1330. " <td>65</td>\n",
  1331. " <td>67</td>\n",
  1332. " <td>58</td>\n",
  1333. " <td>36</td>\n",
  1334. " <td>84</td>\n",
  1335. " <td>8</td>\n",
  1336. " <td>45</td>\n",
  1337. " <td>16</td>\n",
  1338. " <td>76</td>\n",
  1339. " </tr>\n",
  1340. " <tr>\n",
  1341. " <th>r9</th>\n",
  1342. " <td>56</td>\n",
  1343. " <td>36</td>\n",
  1344. " <td>98</td>\n",
  1345. " <td>63</td>\n",
  1346. " <td>73</td>\n",
  1347. " <td>54</td>\n",
  1348. " <td>36</td>\n",
  1349. " <td>61</td>\n",
  1350. " <td>56</td>\n",
  1351. " <td>73</td>\n",
  1352. " </tr>\n",
  1353. " </tbody>\n",
  1354. "</table>\n",
  1355. "</div>"
  1356. ],
  1357. "text/plain": [
  1358. " c0 c1 c2 c3 c4 c5 c6 c7 c8 c9\n",
  1359. "r0 58 6 91 11 22 29 25 36 55 87\n",
  1360. "r1 83 30 8 55 43 62 82 74 12 5\n",
  1361. "r2 83 95 28 33 95 28 7 32 6 2\n",
  1362. "r3 17 25 82 3 65 39 73 63 6 49\n",
  1363. "r4 13 63 18 86 29 35 97 24 71 50\n",
  1364. "r5 33 39 84 85 15 42 68 45 26 69\n",
  1365. "r6 55 27 17 44 78 19 38 63 31 60\n",
  1366. "r7 41 19 48 36 92 35 41 97 98 10\n",
  1367. "r8 59 65 67 58 36 84 8 45 16 76\n",
  1368. "r9 56 36 98 63 73 54 36 61 56 73"
  1369. ]
  1370. },
  1371. "execution_count": 27,
  1372. "metadata": {},
  1373. "output_type": "execute_result"
  1374. }
  1375. ],
  1376. "source": [
  1377. "df_si"
  1378. ]
  1379. },
  1380. {
  1381. "cell_type": "markdown",
  1382. "metadata": {},
  1383. "source": [
  1384. "As a convenience, the `[]` selector is capable of also dealing with labels, though we will not go any further than this basic example."
  1385. ]
  1386. },
  1387. {
  1388. "cell_type": "code",
  1389. "execution_count": 28,
  1390. "metadata": {},
  1391. "outputs": [
  1392. {
  1393. "data": {
  1394. "text/plain": [
  1395. "r3 65\n",
  1396. "r4 29\n",
  1397. "r5 15\n",
  1398. "r6 78\n",
  1399. "Name: c4, dtype: int32"
  1400. ]
  1401. },
  1402. "execution_count": 28,
  1403. "metadata": {},
  1404. "output_type": "execute_result"
  1405. }
  1406. ],
  1407. "source": [
  1408. "df_si['r3':'r6']['c4']"
  1409. ]
  1410. },
  1411. {
  1412. "cell_type": "markdown",
  1413. "metadata": {},
  1414. "source": [
  1415. "Selecting contiguous slices (indices are sorted), is very straightforward."
  1416. ]
  1417. },
  1418. {
  1419. "cell_type": "code",
  1420. "execution_count": 29,
  1421. "metadata": {},
  1422. "outputs": [
  1423. {
  1424. "data": {
  1425. "text/html": [
  1426. "<div>\n",
  1427. "<style>\n",
  1428. " .dataframe thead tr:only-child th {\n",
  1429. " text-align: right;\n",
  1430. " }\n",
  1431. "\n",
  1432. " .dataframe thead th {\n",
  1433. " text-align: left;\n",
  1434. " }\n",
  1435. "\n",
  1436. " .dataframe tbody tr th {\n",
  1437. " vertical-align: top;\n",
  1438. " }\n",
  1439. "</style>\n",
  1440. "<table border=\"1\" class=\"dataframe\">\n",
  1441. " <thead>\n",
  1442. " <tr style=\"text-align: right;\">\n",
  1443. " <th></th>\n",
  1444. " <th>c0</th>\n",
  1445. " <th>c1</th>\n",
  1446. " <th>c2</th>\n",
  1447. " <th>c3</th>\n",
  1448. " <th>c4</th>\n",
  1449. " <th>c5</th>\n",
  1450. " <th>c6</th>\n",
  1451. " <th>c7</th>\n",
  1452. " <th>c8</th>\n",
  1453. " <th>c9</th>\n",
  1454. " </tr>\n",
  1455. " </thead>\n",
  1456. " <tbody>\n",
  1457. " <tr>\n",
  1458. " <th>r3</th>\n",
  1459. " <td>17</td>\n",
  1460. " <td>25</td>\n",
  1461. " <td>82</td>\n",
  1462. " <td>3</td>\n",
  1463. " <td>65</td>\n",
  1464. " <td>39</td>\n",
  1465. " <td>73</td>\n",
  1466. " <td>63</td>\n",
  1467. " <td>6</td>\n",
  1468. " <td>49</td>\n",
  1469. " </tr>\n",
  1470. " <tr>\n",
  1471. " <th>r4</th>\n",
  1472. " <td>13</td>\n",
  1473. " <td>63</td>\n",
  1474. " <td>18</td>\n",
  1475. " <td>86</td>\n",
  1476. " <td>29</td>\n",
  1477. " <td>35</td>\n",
  1478. " <td>97</td>\n",
  1479. " <td>24</td>\n",
  1480. " <td>71</td>\n",
  1481. " <td>50</td>\n",
  1482. " </tr>\n",
  1483. " </tbody>\n",
  1484. "</table>\n",
  1485. "</div>"
  1486. ],
  1487. "text/plain": [
  1488. " c0 c1 c2 c3 c4 c5 c6 c7 c8 c9\n",
  1489. "r3 17 25 82 3 65 39 73 63 6 49\n",
  1490. "r4 13 63 18 86 29 35 97 24 71 50"
  1491. ]
  1492. },
  1493. "execution_count": 29,
  1494. "metadata": {},
  1495. "output_type": "execute_result"
  1496. }
  1497. ],
  1498. "source": [
  1499. "df_si.loc['r3':'r4',]"
  1500. ]
  1501. },
  1502. {
  1503. "cell_type": "markdown",
  1504. "metadata": {},
  1505. "source": [
  1506. "As expected we can slice the columns as well."
  1507. ]
  1508. },
  1509. {
  1510. "cell_type": "code",
  1511. "execution_count": 30,
  1512. "metadata": {},
  1513. "outputs": [
  1514. {
  1515. "data": {
  1516. "text/html": [
  1517. "<div>\n",
  1518. "<style>\n",
  1519. " .dataframe thead tr:only-child th {\n",
  1520. " text-align: right;\n",
  1521. " }\n",
  1522. "\n",
  1523. " .dataframe thead th {\n",
  1524. " text-align: left;\n",
  1525. " }\n",
  1526. "\n",
  1527. " .dataframe tbody tr th {\n",
  1528. " vertical-align: top;\n",
  1529. " }\n",
  1530. "</style>\n",
  1531. "<table border=\"1\" class=\"dataframe\">\n",
  1532. " <thead>\n",
  1533. " <tr style=\"text-align: right;\">\n",
  1534. " <th></th>\n",
  1535. " <th>c2</th>\n",
  1536. " <th>c3</th>\n",
  1537. " </tr>\n",
  1538. " </thead>\n",
  1539. " <tbody>\n",
  1540. " <tr>\n",
  1541. " <th>r3</th>\n",
  1542. " <td>82</td>\n",
  1543. " <td>3</td>\n",
  1544. " </tr>\n",
  1545. " <tr>\n",
  1546. " <th>r4</th>\n",
  1547. " <td>18</td>\n",
  1548. " <td>86</td>\n",
  1549. " </tr>\n",
  1550. " </tbody>\n",
  1551. "</table>\n",
  1552. "</div>"
  1553. ],
  1554. "text/plain": [
  1555. " c2 c3\n",
  1556. "r3 82 3\n",
  1557. "r4 18 86"
  1558. ]
  1559. },
  1560. "execution_count": 30,
  1561. "metadata": {},
  1562. "output_type": "execute_result"
  1563. }
  1564. ],
  1565. "source": [
  1566. "df_si.loc['r3':'r4', 'c2':'c3']"
  1567. ]
  1568. },
  1569. {
  1570. "cell_type": "markdown",
  1571. "metadata": {},
  1572. "source": [
  1573. "This wraps up our basic discussion of selection, we will return to this discussion in part 3, when we talk about more complex boolean slicing and multi-index slicing. &Xi;"
  1574. ]
  1575. }
  1576. ],
  1577. "metadata": {
  1578. "anaconda-cloud": {},
  1579. "kernelspec": {
  1580. "display_name": "Python [conda root]",
  1581. "language": "python",
  1582. "name": "conda-root-py"
  1583. },
  1584. "language_info": {
  1585. "codemirror_mode": {
  1586. "name": "ipython",
  1587. "version": 3
  1588. },
  1589. "file_extension": ".py",
  1590. "mimetype": "text/x-python",
  1591. "name": "python",
  1592. "nbconvert_exporter": "python",
  1593. "pygments_lexer": "ipython3",
  1594. "version": "3.6.1"
  1595. },
  1596. "toc": {
  1597. "colors": {
  1598. "hover_highlight": "#DAA520",
  1599. "navigate_num": "#000000",
  1600. "navigate_text": "#333333",
  1601. "running_highlight": "#FF0000",
  1602. "selected_highlight": "#FFD700",
  1603. "sidebar_border": "#EEEEEE",
  1604. "wrapper_background": "#FFFFFF"
  1605. },
  1606. "moveMenuLeft": true,
  1607. "nav_menu": {
  1608. "height": "143px",
  1609. "width": "252px"
  1610. },
  1611. "navigate_menu": true,
  1612. "number_sections": false,
  1613. "sideBar": true,
  1614. "threshold": 4,
  1615. "toc_cell": true,
  1616. "toc_section_display": "block",
  1617. "toc_window_display": false,
  1618. "widenNotebook": false
  1619. }
  1620. },
  1621. "nbformat": 4,
  1622. "nbformat_minor": 2
  1623. }