pandas_cours.ipynb 208 KB
Newer Older
1
2
3
4
{
 "cells": [
  {
   "cell_type": "markdown",
Etienne Kornobis's avatar
Etienne Kornobis committed
5
   "id": "horizontal-listening",
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
   "metadata": {},
   "source": [
    "# <center>**Cours**</center>\n",
    "\n",
    "<div style=\"text-align:center\">\n",
    "    <img src=\"images/pandas_logo.svg\" width=\"600px\">\n",
    "    <div>\n",
    "       Bertrand Néron, François Laurent, Etienne Kornobis\n",
    "       <br />\n",
    "       <a src=\" https://research.pasteur.fr/en/team/bioinformatics-and-biostatistics-hub/\">Bioinformatics and Biostatistiqucs HUB</a>\n",
    "       <br />\n",
    "       © Institut Pasteur, 2021\n",
    "    </div>    \n",
    "</div>"
   ]
  },
  {
   "cell_type": "markdown",
Etienne Kornobis's avatar
Etienne Kornobis committed
24
   "id": "sophisticated-concept",
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
   "metadata": {},
   "source": [
    "# Intro\n",
    "\n",
    "**Pandas** is a library to manipulate data structures and perform data analysis and visualization. Pandas is built on top of **Numpy**, a widely used library for mathematical operation particularly on arrays and matrices. Pandas is helping with data analysis stack, including data cleaning/formatting followed by analysis and visualization.\n",
    "\n",
    "Pandas is particularly well suited to deal with tabular data which can be imported from different formats such are **csv**, **tsv** or even **xlsx**.\n",
    "\n",
    "The two primary data structures in pandas are **Series** and **DataFrames**.\n",
    "\n",
    "Pandas is designed to manipulate tabulated data, Numpy is designed to do computation on arrays. So here are the differences: \n",
    "\n",
    "**Numpy** \n",
    "* handles one structure: the ndarray.\n",
    "* an *array* can have 1, 2 or more dimensions.\n",
    "* A *ndarray* handles homogeneous data, only one datatype in an array.\n",
    "* So numpy is mostly used to do math on arrays.\n",
    "\n",
    "**Pandas** \n",
    "* *Series* have 1 dimension, *DataFrame* have 2 dimensions.\n",
    "* *Pandas* does **not** handle structures with more than 2 dimensions.\n",
    "* But a *DataFrame* can contain heterogenous data, each column can have a different datatype.\n",
    "* *Pandas* is more powerful to query data or manipulate them.\n",
    "\n",
    "So *Numpy* is mostly used to do math, *Pandas* to explore data structured in tables. "
   ]
  },
  {
   "cell_type": "markdown",
Etienne Kornobis's avatar
Etienne Kornobis committed
54
   "id": "velvet-payroll",
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
   "metadata": {},
   "source": [
    "# Installation\n",
    "\n",
    "For *conda* users\n",
    "\n",
    "```shell\n",
    "conda install pandas\n",
    "```\n",
    "\n",
    "for *pip* users\n",
    "```shell\n",
    "pip install pandas\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
Etienne Kornobis's avatar
Etienne Kornobis committed
73
   "id": "falling-radar",
74
75
76
77
78
79
80
   "metadata": {},
   "source": [
    "# Import Convention"
   ]
  },
  {
   "cell_type": "code",
Etienne Kornobis's avatar
Etienne Kornobis committed
81
82
   "execution_count": 171,
   "id": "executed-tsunami",
83
84
85
86
87
88
89
90
91
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "markdown",
Etienne Kornobis's avatar
Etienne Kornobis committed
92
   "id": "foster-convert",
93
94
95
96
97
98
99
100
101
102
103
104
105
   "metadata": {},
   "source": [
    "# Series\n",
    "\n",
    "A Series is a one-dimensional array with axis labels. Labels do not need to be\n",
    "unique but must be hashable.\n",
    "\n",
    "To create a series, use the pandas `Series` object and specify a list or tuple\n",
    "of value to feed your serie with as the first argument:"
   ]
  },
  {
   "cell_type": "code",
Etienne Kornobis's avatar
Etienne Kornobis committed
106
107
   "execution_count": 172,
   "id": "musical-civilization",
108
109
110
111
112
113
114
115
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "pandas.core.series.Series"
      ]
     },
Etienne Kornobis's avatar
Etienne Kornobis committed
116
     "execution_count": 172,
117
118
119
120
121
122
123
124
125
126
127
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "serie_nolabel = pd.Series([1,2,3])\n",
    "type(serie_nolabel)"
   ]
  },
  {
   "cell_type": "code",
Etienne Kornobis's avatar
Etienne Kornobis committed
128
129
   "execution_count": 173,
   "id": "superb-relaxation",
130
131
132
133
134
135
136
137
138
139
140
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    1\n",
       "1    2\n",
       "2    3\n",
       "dtype: int64"
      ]
     },
Etienne Kornobis's avatar
Etienne Kornobis committed
141
     "execution_count": 173,
142
143
144
145
146
147
148
149
150
151
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "serie_nolabel"
   ]
  },
  {
   "cell_type": "markdown",
Etienne Kornobis's avatar
Etienne Kornobis committed
152
   "id": "coordinated-issue",
153
154
155
156
157
158
159
160
   "metadata": {},
   "source": [
    "You can specify the labels of your Series by providing a list of labels as\n",
    "for the `index` argument:"
   ]
  },
  {
   "cell_type": "code",
Etienne Kornobis's avatar
Etienne Kornobis committed
161
162
   "execution_count": 174,
   "id": "received-flash",
163
164
165
166
167
168
169
170
171
172
173
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "A    1\n",
       "B    2\n",
       "C    3\n",
       "dtype: int64"
      ]
     },
Etienne Kornobis's avatar
Etienne Kornobis committed
174
     "execution_count": 174,
175
176
177
178
179
180
181
182
183
184
185
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "serie_label = pd.Series([1,2,3], index=['A', 'B', 'C'])\n",
    "serie_label"
   ]
  },
  {
   "cell_type": "markdown",
Etienne Kornobis's avatar
Etienne Kornobis committed
186
   "id": "sorted-optimum",
187
188
189
190
191
192
193
   "metadata": {},
   "source": [
    "And we can access these indices with the `index` property:"
   ]
  },
  {
   "cell_type": "code",
Etienne Kornobis's avatar
Etienne Kornobis committed
194
195
   "execution_count": 175,
   "id": "immune-physiology",
196
197
198
199
200
201
202
203
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "RangeIndex(start=0, stop=3, step=1)"
      ]
     },
Etienne Kornobis's avatar
Etienne Kornobis committed
204
     "execution_count": 175,
205
206
207
208
209
210
211
212
213
214
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "serie_nolabel.index"
   ]
  },
  {
   "cell_type": "code",
Etienne Kornobis's avatar
Etienne Kornobis committed
215
216
   "execution_count": 176,
   "id": "systematic-working",
217
218
219
220
221
222
223
224
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index(['A', 'B', 'C'], dtype='object')"
      ]
     },
Etienne Kornobis's avatar
Etienne Kornobis committed
225
     "execution_count": 176,
226
227
228
229
230
231
232
233
234
235
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "serie_label.index"
   ]
  },
  {
   "cell_type": "markdown",
Etienne Kornobis's avatar
Etienne Kornobis committed
236
   "id": "arctic-gibson",
237
238
239
240
241
242
243
244
245
   "metadata": {},
   "source": [
    "## Indexing/Slicing\n",
    "\n",
    "In order to subset a serie based on an **integer index**, you can use the `iloc` attribute:"
   ]
  },
  {
   "cell_type": "code",
Etienne Kornobis's avatar
Etienne Kornobis committed
246
247
   "execution_count": 177,
   "id": "alternate-banks",
248
249
250
251
252
253
254
255
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2"
      ]
     },
Etienne Kornobis's avatar
Etienne Kornobis committed
256
     "execution_count": 177,
257
258
259
260
261
262
263
264
265
266
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "serie_nolabel.iloc[1]"
   ]
  },
  {
   "cell_type": "code",
Etienne Kornobis's avatar
Etienne Kornobis committed
267
268
   "execution_count": 178,
   "id": "standing-train",
269
270
271
272
273
274
275
276
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2"
      ]
     },
Etienne Kornobis's avatar
Etienne Kornobis committed
277
     "execution_count": 178,
278
279
280
281
282
283
284
285
286
287
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "serie_label.iloc[1]"
   ]
  },
  {
   "cell_type": "code",
Etienne Kornobis's avatar
Etienne Kornobis committed
288
289
   "execution_count": 179,
   "id": "severe-correlation",
290
291
292
293
294
295
296
297
298
299
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "A    1\n",
       "B    2\n",
       "dtype: int64"
      ]
     },
Etienne Kornobis's avatar
Etienne Kornobis committed
300
     "execution_count": 179,
301
302
303
304
305
306
307
308
309
310
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "serie_label.iloc[0:2]"
   ]
  },
  {
   "cell_type": "code",
Etienne Kornobis's avatar
Etienne Kornobis committed
311
312
   "execution_count": 180,
   "id": "raising-grenada",
313
314
315
316
317
318
319
320
321
322
323
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "C    3\n",
       "B    2\n",
       "A    1\n",
       "dtype: int64"
      ]
     },
Etienne Kornobis's avatar
Etienne Kornobis committed
324
     "execution_count": 180,
325
326
327
328
329
330
331
332
333
334
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "serie_label.iloc[::-1]"
   ]
  },
  {
   "cell_type": "markdown",
Etienne Kornobis's avatar
Etienne Kornobis committed
335
   "id": "blocked-roommate",
336
337
338
339
340
341
342
   "metadata": {},
   "source": [
    "Most commonly, You can use **labels** as well for subsetting, using the `loc` attribute:"
   ]
  },
  {
   "cell_type": "code",
Etienne Kornobis's avatar
Etienne Kornobis committed
343
344
   "execution_count": 181,
   "id": "accompanied-pantyhose",
345
346
347
348
349
350
351
352
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2"
      ]
     },
Etienne Kornobis's avatar
Etienne Kornobis committed
353
     "execution_count": 181,
354
355
356
357
358
359
360
361
362
363
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "serie_label.loc[\"B\"]"
   ]
  },
  {
   "cell_type": "markdown",
Etienne Kornobis's avatar
Etienne Kornobis committed
364
   "id": "durable-lesson",
365
366
367
368
369
370
371
372
373
374
   "metadata": {},
   "source": [
    "**WARNING**: With `loc`, the value is interpreted as a label of the\n",
    "   index, and **never** as an integer position along the index, there is `iloc` for this.\n",
    "   \n",
    "When index labels are strings, you can as well access the corresponding value using this simple syntax `.LABEL_VALUE`"
   ]
  },
  {
   "cell_type": "code",
Etienne Kornobis's avatar
Etienne Kornobis committed
375
376
   "execution_count": 182,
   "id": "comparative-guinea",
377
378
379
380
381
382
383
384
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
Etienne Kornobis's avatar
Etienne Kornobis committed
385
     "execution_count": 182,
386
387
388
389
390
391
392
393
394
395
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "serie_label.A"
   ]
  },
  {
   "cell_type": "markdown",
Etienne Kornobis's avatar
Etienne Kornobis committed
396
   "id": "convenient-constitution",
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
   "metadata": {},
   "source": [
    "Serie objects benefit from many attributes and methods (see [pandas documentation](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.Series.html)), lot's of them being common with pandas DataFrames. We will see some of the one listed below in action in the DataFrame section of this course.\n",
    "\n",
    "Here are some attributes of interest:\n",
    "\n",
    "|Attribute|Action|\n",
    "|-|-|\n",
    "|index|Returns the index (0 axis labels) of the Serie|\n",
    "|name|Return the name of the Serie|\n",
    "|shape|Return the number of element in the Serie|\n",
    "\n",
    "And some useful methods:\n",
    "\n",
    "|Method|Action|\n",
    "|-|-|\n",
    "|aggregate|Aggregate using one or more operations over the specified axis|\n",
    "|all|Return whether all elements are True potentially over an axis|\n",
    "|any|Return whether any element is True potentially over an axis|\n",
    "|apply|Invoke function on values of Series|\n",
    "|astype|Cast a pandas object to a specified dtype|\n",
    "|copy|Make a copy of this object’s indices and data|\n",
    "|count|Return number of non-NA/null observations in the Series|\n",
    "|describe|Generate descriptive statistics that summarize the central tendency dispersion and shape of a dataset’s distribution, excluding NaN values|\n",
    "|drop|Return Series with specified index labels removed|\n",
    "|groupby|Group DataFrame or Series using a mapper or by a Series of columns|\n",
    "|head / tail|Return the first / last n rows|\n",
    "|max, min, median, mean, sum|Perform the corresponding operation on the Serie|\n",
    "|plot|Plot graphs from Serie/DataFrame|\n",
    "|reset_index|Generate a new DataFrame or Series with the index reset|\n",
    "|sort_values|Sort by values a the specified column|\n",
    "|str|String methods for series|              |\n",
    "|to_csv, to_excel|Export to csv or excel file|\n",
    "|unique|Return unique values of Series object|\n",
    "|value_counts|Return a Series containing counts of unique values|\n"
   ]
  },
  {
   "cell_type": "markdown",
Etienne Kornobis's avatar
Etienne Kornobis committed
436
   "id": "arabic-affairs",
437
438
439
440
441
442
443
444
445
446
447
   "metadata": {},
   "source": [
    "## Operations on Series\n",
    "\n",
    "Comparison operators (ie `==`, `<`, `<=`, `>=`, `>`) can be used on Series as well as DataFrames for subsetting.\n",
    "\n",
    "For example, we want to see which values are superior to one in our previous Serie:"
   ]
  },
  {
   "cell_type": "code",
Etienne Kornobis's avatar
Etienne Kornobis committed
448
449
   "execution_count": 183,
   "id": "million-richards",
450
451
452
453
454
455
456
457
458
459
460
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "A    False\n",
       "B     True\n",
       "C     True\n",
       "dtype: bool"
      ]
     },
Etienne Kornobis's avatar
Etienne Kornobis committed
461
     "execution_count": 183,
462
463
464
465
466
467
468
469
470
471
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "serie_label > 1"
   ]
  },
  {
   "cell_type": "markdown",
Etienne Kornobis's avatar
Etienne Kornobis committed
472
   "id": "unlike-monaco",
473
474
475
476
477
478
479
   "metadata": {},
   "source": [
    "Since `loc` can take list or Series of booleans as input, we can then apply this Boolean Serie as a mask for our Serie:"
   ]
  },
  {
   "cell_type": "code",
Etienne Kornobis's avatar
Etienne Kornobis committed
480
481
   "execution_count": 184,
   "id": "ordered-rendering",
482
483
484
485
486
487
488
489
490
491
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "B    2\n",
       "C    3\n",
       "dtype: int64"
      ]
     },
Etienne Kornobis's avatar
Etienne Kornobis committed
492
     "execution_count": 184,
493
494
495
496
497
498
499
500
501
502
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "serie_label.loc[serie_label>1]"
   ]
  },
  {
   "cell_type": "markdown",
Etienne Kornobis's avatar
Etienne Kornobis committed
503
   "id": "major-intermediate",
504
505
506
507
508
509
510
   "metadata": {},
   "source": [
    "## Operations between Series"
   ]
  },
  {
   "cell_type": "markdown",
Etienne Kornobis's avatar
Etienne Kornobis committed
511
   "id": "suitable-focus",
512
513
514
515
516
517
518
519
   "metadata": {},
   "source": [
    "Operations (ie `+`, `-`, `*`, `/`) between Series will trigger an alignment of the values\n",
    "based on the index values:"
   ]
  },
  {
   "cell_type": "code",
Etienne Kornobis's avatar
Etienne Kornobis committed
520
521
   "execution_count": 185,
   "id": "least-cruise",
522
523
524
525
526
527
528
529
530
531
532
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "A    2\n",
       "B    4\n",
       "C    6\n",
       "dtype: int64"
      ]
     },
Etienne Kornobis's avatar
Etienne Kornobis committed
533
     "execution_count": 185,
534
535
536
537
538
539
540
541
542
543
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "serie_label + serie_label"
   ]
  },
  {
   "cell_type": "markdown",
Etienne Kornobis's avatar
Etienne Kornobis committed
544
   "id": "herbal-collaboration",
545
546
547
548
549
550
551
   "metadata": {},
   "source": [
    "We can see here that the label are aligned prior operation"
   ]
  },
  {
   "cell_type": "code",
Etienne Kornobis's avatar
Etienne Kornobis committed
552
553
   "execution_count": 186,
   "id": "better-blame",
554
555
556
557
558
559
560
561
562
563
564
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "A    2\n",
       "B    4\n",
       "C    6\n",
       "dtype: int64"
      ]
     },
Etienne Kornobis's avatar
Etienne Kornobis committed
565
     "execution_count": 186,
566
567
568
569
570
571
572
573
574
575
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "serie_label + serie_label.iloc[::-1]"
   ]
  },
  {
   "cell_type": "markdown",
Etienne Kornobis's avatar
Etienne Kornobis committed
576
   "id": "loved-orleans",
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
   "metadata": {},
   "source": [
    "# DataFrames\n",
    "\n",
    "A pandas DataFrame is a two-dimensional data structure with axis labels. Labels do not need to be unique but must be hashable. DataFrame in pandas are like dictionary containers of Series objects.\n",
    "\n",
    "## DataFrame Terminology\n",
    "\n",
    "<img src=\"images/pandas_dataframe.png\" width=\"300px\" />\n",
    "\n",
    "## Create a DataFrame\n",
    "\n",
    "[Dataframes](https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.html) in pandas are rarely created from scratch. One common approach is to create a pandas DataFrame from a dictionary or a file, but you can as well create them from a list of lists or numpy ndarrays. \n",
    "\n",
    "### From a list of lists:"
   ]
  },
  {
   "cell_type": "code",
Etienne Kornobis's avatar
Etienne Kornobis committed
596
597
   "execution_count": 187,
   "id": "regulated-ready",
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>a</th>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>b</th>\n",
       "      <td>4</td>\n",
       "      <td>5</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   A  B  C\n",
       "a  1  2  3\n",
       "b  4  5  6"
      ]
     },
Etienne Kornobis's avatar
Etienne Kornobis committed
649
     "execution_count": 187,
650
651
652
653
654
655
656
657
658
659
660
661
662
663
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = pd.DataFrame([[1,2,3],\n",
    "                   [4,5,6]],\n",
    "                 columns=['A', 'B', 'C'],\n",
    "                 index= ['a', 'b'])\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
Etienne Kornobis's avatar
Etienne Kornobis committed
664
665
   "execution_count": 188,
   "id": "stable-discharge",
666
667
668
669
670
671
672
673
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index(['a', 'b'], dtype='object')"
      ]
     },
Etienne Kornobis's avatar
Etienne Kornobis committed
674
     "execution_count": 188,
675
676
677
678
679
680
681
682
683
684
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.index"
   ]
  },
  {
   "cell_type": "code",
Etienne Kornobis's avatar
Etienne Kornobis committed
685
686
   "execution_count": 189,
   "id": "configured-coral",
687
688
689
690
691
692
693
694
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index(['A', 'B', 'C'], dtype='object')"
      ]
     },
Etienne Kornobis's avatar
Etienne Kornobis committed
695
     "execution_count": 189,
696
697
698
699
700
701
702
703
704
705
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.columns"
   ]
  },
  {
   "cell_type": "markdown",
Etienne Kornobis's avatar
Etienne Kornobis committed
706
   "id": "exclusive-brave",
707
708
709
710
711
712
713
   "metadata": {},
   "source": [
    "### From a numpy ndarray"
   ]
  },
  {
   "cell_type": "code",
Etienne Kornobis's avatar
Etienne Kornobis committed
714
715
   "execution_count": 190,
   "id": "facial-curve",
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "      <th>2</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>3</td>\n",
       "      <td>4</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>6</td>\n",
       "      <td>7</td>\n",
       "      <td>8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>9</td>\n",
       "      <td>10</td>\n",
       "      <td>11</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   0   1   2\n",
       "0  0   1   2\n",
       "1  3   4   5\n",
       "2  6   7   8\n",
       "3  9  10  11"
      ]
     },
Etienne Kornobis's avatar
Etienne Kornobis committed
781
     "execution_count": 190,
782
783
784
785
786
787
788
789
790
791
792
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = pd.DataFrame(np.arange(12).reshape(4,3))\n",
    "df"
   ]
  },
  {
   "cell_type": "markdown",
Etienne Kornobis's avatar
Etienne Kornobis committed
793
   "id": "committed-planning",
794
795
   "metadata": {},
   "source": [
Etienne Kornobis's avatar
Etienne Kornobis committed
796
    "### From a dictionnary"
797
798
799
800
   ]
  },
  {
   "cell_type": "code",
Etienne Kornobis's avatar
Etienne Kornobis committed
801
802
   "execution_count": 191,
   "id": "suspected-nirvana",
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   A  B\n",
       "0  1  4\n",
       "1  2  5\n",
       "2  3  6"
      ]
     },
Etienne Kornobis's avatar
Etienne Kornobis committed
857
     "execution_count": 191,
858
859
860
861
862
863
864
865
866
867
868
869
870
871
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = pd.DataFrame({'A': [1,2,3],\n",
    "                   'B': np.arange(4,7),\n",
    "                  })\n",
    "                   \n",
    "df"
   ]
  },
  {
   "cell_type": "markdown",
Etienne Kornobis's avatar
Etienne Kornobis committed
872
   "id": "vocational-peoples",
873
874
875
876
877
878
879
880
881
882
883
884
   "metadata": {},
   "source": [
    "- From a file, many options are available, to name only a few:\n",
    "    - [pd.read_csv](https://pandas.pydata.org/docs/reference/api/pandas.read_csv.html)\n",
    "    - [pd.read_excel](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.read_excel.html)\n",
    "    - [pd.read_html](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.read_html.html)\n",
    "    \n",
    "NB: For excel and html imports, you might need to install extra libraries."
   ]
  },
  {
   "cell_type": "code",
Etienne Kornobis's avatar
Etienne Kornobis committed
885
886
   "execution_count": 192,
   "id": "sonic-shock",
887
888
889
890
891
892
893
894
895
896
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "titanic = pd.read_csv(\"data/titanic.csv\")"
   ]
  },
  {
   "cell_type": "markdown",
Etienne Kornobis's avatar
Etienne Kornobis committed
897
   "id": "about-cursor",
898
899
900
901
902
903
904
905
906
   "metadata": {},
   "source": [
    "We want to open *data/bar_data.tsv* file but the 2 first lines are comments and the separator between fields is *tab*\n",
    "\n",
    "See below the 5 first lines (using the `!` jupyter magic for bash subprocesses)"
   ]
  },
  {
   "cell_type": "code",
Etienne Kornobis's avatar
Etienne Kornobis committed
907
908
   "execution_count": 193,
   "id": "bridal-development",
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "# generated with fooo software version 12bis\n",
      "# 2021/02/31\n",
      "cond1\tcond2\tcond3\tcontrol\n",
      "14.644417316782045\t2.9453091400880465\t24.81171864537413\t5.114340165446571\n",
      "12.071043262601615\t4.406424332565544\t21.574601309211538\t2.5071180945299716\n"
     ]
    }
   ],
   "source": [
    "! head -5 data/bar_data.tsv"
   ]
  },
  {
   "cell_type": "code",
Etienne Kornobis's avatar
Etienne Kornobis committed
929
930
   "execution_count": 194,
   "id": "listed-framework",
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>cond1</th>\n",
       "      <th>cond2</th>\n",
       "      <th>cond3</th>\n",
       "      <th>control</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>14.644417</td>\n",
       "      <td>2.945309</td>\n",
       "      <td>24.811719</td>\n",
       "      <td>5.114340</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>12.071043</td>\n",
       "      <td>4.406424</td>\n",
       "      <td>21.574601</td>\n",
       "      <td>2.507118</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>8.227469</td>\n",
       "      <td>3.185252</td>\n",
       "      <td>20.651623</td>\n",
       "      <td>4.449593</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>8.980799</td>\n",
       "      <td>9.233560</td>\n",
       "      <td>24.859737</td>\n",
       "      <td>4.127919</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>9.080359</td>\n",
       "      <td>5.629192</td>\n",
       "      <td>18.443504</td>\n",
       "      <td>4.268572</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "       cond1     cond2      cond3   control\n",
       "0  14.644417  2.945309  24.811719  5.114340\n",
       "1  12.071043  4.406424  21.574601  2.507118\n",
       "2   8.227469  3.185252  20.651623  4.449593\n",
       "3   8.980799  9.233560  24.859737  4.127919\n",
       "4   9.080359  5.629192  18.443504  4.268572"
      ]
     },
Etienne Kornobis's avatar
Etienne Kornobis committed
1009
     "execution_count": 194,
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "bar = pd.read_csv(\"data/bar_data.tsv\", sep=\"\\t\", comment=\"#\")\n",
    "bar.head()"
   ]
  },
  {
   "cell_type": "markdown",
Etienne Kornobis's avatar
Etienne Kornobis committed
1021
   "id": "explicit-monitoring",
1022
1023
1024
1025
1026
1027
1028
   "metadata": {},
   "source": [
    "If the data in the file are already indexed like in this one:"
   ]
  },
  {
   "cell_type": "code",
Etienne Kornobis's avatar
Etienne Kornobis committed
1029
1030
   "execution_count": 195,
   "id": "allied-artist",
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\tMW\tAlogP\tPSA\tHBA\n",
      "0\t0.0\t1.0\t72.73111270481336\t1.1416684150966834\n",
      "1\t3.63\t544.59\t391.4275648686457\t0.9848635571682688\n",
      "2\t2.11\t383.4\t437.4589821943501\t15.040385372412596\n",
      "3\t1.24\t162.23\t480.1112629835199\t11.401906578750385\n"
     ]
    }
   ],
   "source": [
    "! head -5 data/data_for_plt.csv"
   ]
  },
  {
   "cell_type": "code",
Etienne Kornobis's avatar
Etienne Kornobis committed
1051
1052
   "execution_count": 196,
   "id": "limiting-tokyo",
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Unnamed: 0</th>\n",
       "      <th>MW</th>\n",
       "      <th>AlogP</th>\n",
       "      <th>PSA</th>\n",
       "      <th>HBA</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0</td>\n",
       "      <td>0.00</td>\n",
       "      <td>1.00</td>\n",
       "      <td>72.731113</td>\n",
       "      <td>1.141668</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>3.63</td>\n",
       "      <td>544.59</td>\n",
       "      <td>391.427565</td>\n",
       "      <td>0.984864</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2</td>\n",
       "      <td>2.11</td>\n",
       "      <td>383.40</td>\n",
       "      <td>437.458982</td>\n",
       "      <td>15.040385</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   Unnamed: 0    MW   AlogP         PSA        HBA\n",
       "0           0  0.00    1.00   72.731113   1.141668\n",
       "1           1  3.63  544.59  391.427565   0.984864\n",
       "2           2  2.11  383.40  437.458982  15.040385"
      ]
     },
Etienne Kornobis's avatar
Etienne Kornobis committed
1119
     "execution_count": 196,
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = pd.read_csv(\"data/data_for_plt.csv\", sep=\"\\t\")\n",
    "data.head(3)"
   ]
  },
  {
   "cell_type": "markdown",
Etienne Kornobis's avatar
Etienne Kornobis committed
1131
   "id": "european-tunisia",
1132
1133
   "metadata": {},
   "source": [
Etienne Kornobis's avatar
Etienne Kornobis committed
1134
    "To avoid to have an extra column, you can specify which columns to use as index.\n",
1135
1136
1137
1138
1139
    "This column **must** have distincts values."
   ]
  },
  {
   "cell_type": "code",
Etienne Kornobis's avatar
Etienne Kornobis committed
1140
1141
   "execution_count": 197,
   "id": "crucial-flight",
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>MW</th>\n",
       "      <th>AlogP</th>\n",
       "      <th>PSA</th>\n",
       "      <th>HBA</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0.00</td>\n",
       "      <td>1.00</td>\n",
       "      <td>72.731113</td>\n",
       "      <td>1.141668</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>3.63</td>\n",
       "      <td>544.59</td>\n",
       "      <td>391.427565</td>\n",
       "      <td>0.984864</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2.11</td>\n",
       "      <td>383.40</td>\n",
       "      <td>437.458982</td>\n",
       "      <td>15.040385</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1.24</td>\n",
       "      <td>162.23</td>\n",
       "      <td>480.111263</td>\n",
       "      <td>11.401907</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>-1.37</td>\n",
       "      <td>361.37</td>\n",
       "      <td>448.864769</td>\n",
       "      <td>5.732690</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     MW   AlogP         PSA        HBA\n",
       "0  0.00    1.00   72.731113   1.141668\n",
       "1  3.63  544.59  391.427565   0.984864\n",
       "2  2.11  383.40  437.458982  15.040385\n",
       "3  1.24  162.23  480.111263  11.401907\n",
       "4 -1.37  361.37  448.864769   5.732690"
      ]
     },
Etienne Kornobis's avatar
Etienne Kornobis committed
1220
     "execution_count": 197,
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = pd.read_csv(\"data/data_for_plt.csv\", sep=\"\\t\", index_col=0)\n",
    "data.head()"
   ]
  },
  {
   "cell_type": "markdown",
Etienne Kornobis's avatar
Etienne Kornobis committed
1232
   "id": "occasional-carnival",
1233
1234
1235
1236
1237
1238
1239
1240
1241
   "metadata": {},
   "source": [
    "The first line is used as header.<br />\n",
    "So you can specify the number of the row which represents the header,\n",
    "or you can set this parameter to None if the table has no header."
   ]
  },
  {
   "cell_type": "code",
Etienne Kornobis's avatar
Etienne Kornobis committed
1242
1243
   "execution_count": 198,
   "id": "oriented-bleeding",
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>1</th>\n",
       "      <th>2</th>\n",
       "      <th>3</th>\n",
       "      <th>4</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0.00</td>\n",
       "      <td>1.00</td>\n",
       "      <td>72.731113</td>\n",
       "      <td>1.141668</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>3.63</td>\n",
       "      <td>544.59</td>\n",
       "      <td>391.427565</td>\n",
       "      <td>0.984864</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2.11</td>\n",
       "      <td>383.40</td>\n",
       "      <td>437.458982</td>\n",
       "      <td>15.040385</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1.24</td>\n",
       "      <td>162.23</td>\n",
       "      <td>480.111263</td>\n",
       "      <td>11.401907</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>-1.37</td>\n",
       "      <td>361.37</td>\n",
       "      <td>448.864769</td>\n",
       "      <td>5.732690</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "      1       2           3          4\n",
       "0                                     \n",
       "0  0.00    1.00   72.731113   1.141668\n",
       "1  3.63  544.59  391.427565   0.984864\n",
       "2  2.11  383.40  437.458982  15.040385\n",
       "3  1.24  162.23  480.111263  11.401907\n",
       "4 -1.37  361.37  448.864769   5.732690"
      ]
     },
Etienne Kornobis's avatar
Etienne Kornobis committed
1330
     "execution_count": 198,
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = pd.read_csv(\"data/no_header.tsv\", sep=\"\\t\", index_col=0, header=None)\n",
    "data.head()"
   ]
  },
  {
   "cell_type": "markdown",
Etienne Kornobis's avatar
Etienne Kornobis committed
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
   "id": "reasonable-straight",
   "metadata": {},
   "source": [
    "### Going back to np.array and list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 199,
   "id": "competent-negative",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 4],\n",
       "       [2, 5],\n",
       "       [3, 6]])"
      ]
     },
     "execution_count": 199,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.values"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 200,
   "id": "fantastic-monday",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[[1, 4], [2, 5], [3, 6]]"
      ]
     },
     "execution_count": 200,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.values.tolist()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "formal-example",
1395
1396
1397
1398
1399
1400
1401
1402
1403
   "metadata": {},
   "source": [
    "## Characterizing a DataFrame\n",
    "\n",
    "Several DataFrame attributes and methods are provided to characterize your dataset. Here is a subset of them most commonly used."
   ]
  },
  {
   "cell_type": "code",
Etienne Kornobis's avatar
Etienne Kornobis committed
1404
1405
   "execution_count": 201,
   "id": "simple-luxury",
1406
1407
1408
1409
1410
1411
1412
1413
   "metadata": {},
   "outputs": [],
   "source": [
    "titanic = pd.read_csv(\"data/titanic.csv\")"
   ]
  },
  {
   "cell_type": "markdown",
Etienne Kornobis's avatar
Etienne Kornobis committed
1414
   "id": "continuing-activity",
1415
1416
1417
1418
1419
1420
1421
   "metadata": {},
   "source": [
    "`shape` to get the dimensions of the dataframe (ie number or rows, number of columns):"
   ]
  },
  {
   "cell_type": "code",
Etienne Kornobis's avatar
Etienne Kornobis committed
1422
1423
   "execution_count": 202,
   "id": "wound-asbestos",
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The titanic dataset is 891 length\n",
      "The titanic dataset contains 891 rows x 12 columns\n"
     ]
    }
   ],
   "source": [
    "print(f\"The titanic dataset is {len(titanic)} length\")\n",
    "rows, cols = titanic.shape\n",
    "print(f\"The titanic dataset contains {rows} rows x {cols} columns\")"
   ]
  },
  {
   "cell_type": "markdown",
Etienne Kornobis's avatar
Etienne Kornobis committed
1443
   "id": "equal-original",
1444
1445
1446
1447
1448
1449
1450
   "metadata": {},
   "source": [
    "`head` to get the first lines of your dataframe:"
   ]
  },
  {
   "cell_type": "code",
Etienne Kornobis's avatar
Etienne Kornobis committed
1451
1452
   "execution_count": 203,
   "id": "worthy-bridge",
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>PassengerId</th>\n",
       "      <th>Survived</th>\n",
       "      <th>Pclass</th>\n",
       "      <th>Name</th>\n",
       "      <th>Sex</th>\n",
       "      <th>Age</th>\n",
       "      <th>SibSp</th>\n",
       "      <th>Parch</th>\n",
       "      <th>Ticket</th>\n",
       "      <th>Fare</th>\n",
       "      <th>Cabin</th>\n",
       "      <th>Embarked</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>3</td>\n",
       "      <td>Braund, Mr. Owen Harris</td>\n",
       "      <td>male</td>\n",
       "      <td>22.0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>A/5 21171</td>\n",
       "      <td>7.2500</td>\n",
       "      <td>NaN</td>\n",
       "      <td>S</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>Cumings, Mrs. John Bradley (Florence Briggs Th...</td>\n",
       "      <td>female</td>\n",
       "      <td>38.0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>PC 17599</td>\n",
       "      <td>71.2833</td>\n",
       "      <td>C85</td>\n",
       "      <td>C</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>Heikkinen, Miss. Laina</td>\n",
       "      <td>female</td>\n",
       "      <td>26.0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>STON/O2. 3101282</td>\n",
       "      <td>7.9250</td>\n",
       "      <td>NaN</td>\n",
       "      <td>S</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>Futrelle, Mrs. Jacques Heath (Lily May Peel)</td>\n",
       "      <td>female</td>\n",
       "      <td>35.0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>113803</td>\n",
       "      <td>53.1000</td>\n",
       "      <td>C123</td>\n",
       "      <td>S</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5</td>\n",
       "      <td>0</td>\n",
       "      <td>3</td>\n",
       "      <td>Allen, Mr. William Henry</td>\n",
       "      <td>male</td>\n",
       "      <td>35.0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>373450</td>\n",
       "      <td>8.0500</td>\n",
       "      <td>NaN</td>\n",
       "      <td>S</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   PassengerId  Survived  Pclass  \\\n",
       "0            1         0       3   \n",
       "1            2         1       1   \n",
       "2            3         1       3   \n",
       "3            4         1       1   \n",
       "4            5         0       3   \n",
       "\n",
       "                                                Name     Sex   Age  SibSp  \\\n",
       "0                            Braund, Mr. Owen Harris    male  22.0      1   \n",
       "1  Cumings, Mrs. John Bradley (Florence Briggs Th...  female  38.0      1   \n",
       "2                             Heikkinen, Miss. Laina  female  26.0      0   \n",
       "3       Futrelle, Mrs. Jacques Heath (Lily May Peel)  female  35.0      1   \n",
       "4                           Allen, Mr. William Henry    male  35.0      0   \n",
       "\n",
       "   Parch            Ticket     Fare Cabin Embarked  \n",
       "0      0         A/5 21171   7.2500   NaN        S  \n",
       "1      0          PC 17599  71.2833   C85        C  \n",
       "2      0  STON/O2. 3101282   7.9250   NaN        S  \n",
       "3      0            113803  53.1000  C123        S  \n",
       "4      0            373450   8.0500   NaN        S  "
      ]
     },
Etienne Kornobis's avatar
Etienne Kornobis committed
1593
     "execution_count": 203,
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "titanic.head()"
   ]
  },
  {
   "cell_type": "code",
Etienne Kornobis's avatar
Etienne Kornobis committed
1604
1605
   "execution_count": 204,
   "id": "absent-authorization",
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>PassengerId</th>\n",
       "      <th>Survived</th>\n",
       "      <th>Pclass</th>\n",
       "      <th>Name</th>\n",
       "      <th>Sex</th>\n",
       "      <th>Age</th>\n",
       "      <th>SibSp</th>\n",
       "      <th>Parch</th>\n",
       "      <th>Ticket</th>\n",
       "      <th>Fare</th>\n",
       "      <th>Cabin</th>\n",
       "      <th>Embarked</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>3</td>\n",
       "      <td>Braund, Mr. Owen Harris</td>\n",
       "      <td>male</td>\n",
       "      <td>22.0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>A/5 21171</td>\n",
       "      <td>7.2500</td>\n",
       "      <td>NaN</td>\n",
       "      <td>S</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>Cumings, Mrs. John Bradley (Florence Briggs Th...</td>\n",
       "      <td>female</td>\n",
       "      <td>38.0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>PC 17599</td>\n",
       "      <td>71.2833</td>\n",
       "      <td>C85</td>\n",
       "      <td>C</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   PassengerId  Survived  Pclass  \\\n",
       "0            1         0       3   \n",
       "1            2         1       1   \n",
       "\n",
       "                                                Name     Sex   Age  SibSp  \\\n",
       "0                            Braund, Mr. Owen Harris    male  22.0      1   \n",
       "1  Cumings, Mrs. John Bradley (Florence Briggs Th...  female  38.0      1   \n",
       "\n",
       "   Parch     Ticket     Fare Cabin Embarked  \n",
       "0      0  A/5 21171   7.2500   NaN        S  \n",
       "1      0   PC 17599  71.2833   C85        C  "
      ]
     },
Etienne Kornobis's avatar
Etienne Kornobis committed
1692
     "execution_count": 204,
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "titanic.head(n=2)"
   ]
  },
  {
   "cell_type": "markdown",
Etienne Kornobis's avatar
Etienne Kornobis committed
1703
   "id": "clinical-debate",
1704
1705
1706
1707
1708
1709
1710
   "metadata": {},
   "source": [
    "`tail` to get the last lines of your dataframe:"
   ]
  },
  {
   "cell_type": "code",
Etienne Kornobis's avatar
Etienne Kornobis committed
1711
1712
   "execution_count": 205,
   "id": "aboriginal-smith",
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>PassengerId</th>\n",
       "      <th>Survived</th>\n",
       "      <th>Pclass</th>\n",
       "      <th>Name</th>\n",
       "      <th>Sex</th>\n",
       "      <th>Age</th>\n",
       "      <th>SibSp</th>\n",
       "      <th>Parch</th>\n",
       "      <th>Ticket</th>\n",
       "      <th>Fare</th>\n",
       "      <th>Cabin</th>\n",
       "      <th>Embarked</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>889</th>\n",
       "      <td>890</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>Behr, Mr. Karl Howell</td>\n",
       "      <td>male</td>\n",
       "      <td>26.0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>111369</td>\n",
       "      <td>30.00</td>\n",
       "      <td>C148</td>\n",
       "      <td>C</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>890</th>\n",
       "      <td>891</td>\n",
       "      <td>0</td>\n",
       "      <td>3</td>\n",
       "      <td>Dooley, Mr. Patrick</td>\n",
       "      <td>male</td>\n",
       "      <td>32.0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>370376</td>\n",
       "      <td>7.75</td>\n",
       "      <td>NaN</td>\n",
       "      <td>Q</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     PassengerId  Survived  Pclass                   Name   Sex   Age  SibSp  \\\n",
       "889          890         1       1  Behr, Mr. Karl Howell  male  26.0      0   \n",
       "890          891         0       3    Dooley, Mr. Patrick  male  32.0      0   \n",
       "\n",
       "     Parch  Ticket   Fare Cabin Embarked  \n",
       "889      0  111369  30.00  C148        C  \n",
       "890      0  370376   7.75   NaN        Q  "
      ]
     },
Etienne Kornobis's avatar
Etienne Kornobis committed
1795
     "execution_count": 205,
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "titanic.tail(2)"
   ]
  },
  {
   "cell_type": "markdown",
Etienne Kornobis's avatar
Etienne Kornobis committed
1806
   "id": "tight-craps",
1807
1808
1809
1810
1811
1812
1813
   "metadata": {},
   "source": [
    "`describe` to have basic descriptive statistics. The columns on which pandas cannot do statistics are omitted (Name, Sex, ...)"
   ]
  },
  {
   "cell_type": "code",
Etienne Kornobis's avatar
Etienne Kornobis committed
1814
1815
   "execution_count": 206,
   "id": "sunset-ballot",
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>PassengerId</th>\n",
       "      <th>Survived</th>\n",
       "      <th>Pclass</th>\n",
       "      <th>Age</th>\n",
       "      <th>SibSp</th>\n",
       "      <th>Parch</th>\n",
       "      <th>Fare</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>count</th>\n",
       "      <td>891.000000</td>\n",
       "      <td>891.000000</td>\n",
       "      <td>891.000000</td>\n",
       "      <td>714.000000</td>\n",
       "      <td>891.000000</td>\n",
       "      <td>891.000000</td>\n",
       "      <td>891.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>mean</th>\n",
       "      <td>446.000000</td>\n",
       "      <td>0.383838</td>\n",
       "      <td>2.308642</td>\n",
       "      <td>29.699118</td>\n",
       "      <td>0.523008</td>\n",
       "      <td>0.381594</td>\n",
       "      <td>32.204208</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>std</th>\n",
       "      <td>257.353842</td>\n",
       "      <td>0.486592</td>\n",
       "      <td>0.836071</td>\n",
       "      <td>14.526497</td>\n",
       "      <td>1.102743</td>\n",
       "      <td>0.806057</td>\n",
       "      <td>49.693429</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>min</th>\n",
       "      <td>1.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>0.420000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25%</th>\n",
       "      <td>223.500000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>2.000000</td>\n",
       "      <td>20.125000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>7.910400</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>50%</th>\n",
       "      <td>446.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>3.000000</td>\n",
       "      <td>28.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>14.454200</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>75%</th>\n",
       "      <td>668.500000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>3.000000</td>\n",
       "      <td>38.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>31.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>max</th>\n",
       "      <td>891.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>3.000000</td>\n",
       "      <td>80.000000</td>\n",
       "      <td>8.000000</td>\n",
       "      <td>6.000000</td>\n",
       "      <td>512.329200</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "       PassengerId    Survived      Pclass         Age       SibSp  \\\n",
       "count   891.000000  891.000000  891.000000  714.000000  891.000000   \n",
       "mean    446.000000    0.383838    2.308642   29.699118    0.523008   \n",
       "std     257.353842    0.486592    0.836071   14.526497    1.102743   \n",
       "min       1.000000    0.000000    1.000000    0.420000    0.000000   \n",
       "25%     223.500000    0.000000    2.000000   20.125000    0.000000   \n",
       "50%     446.000000    0.000000    3.000000   28.000000    0.000000   \n",
       "75%     668.500000    1.000000    3.000000   38.000000    1.000000   \n",
       "max     891.000000    1.000000    3.000000   80.000000    8.000000   \n",
       "\n",
       "            Parch        Fare  \n",
       "count  891.000000  891.000000  \n",
       "mean     0.381594   32.204208  \n",
       "std      0.806057   49.693429  \n",
       "min      0.000000    0.000000  \n",
       "25%      0.000000    7.910400  \n",
       "50%      0.000000   14.454200  \n",
       "75%      0.000000   31.000000  \n",
       "max      6.000000  512.329200  "
      ]
     },
Etienne Kornobis's avatar
Etienne Kornobis committed
1955
     "execution_count": 206,
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "desc = titanic.describe()\n",
    "desc"
   ]
  },
  {
   "cell_type": "code",
Etienne Kornobis's avatar
Etienne Kornobis committed
1967
1968
   "execution_count": 207,
   "id": "whole-township",
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "titanic have 12 cols\n",
      "desc have 7 cols\n"
     ]
    }
   ],
   "source": [
    "print(f\"titanic have {len(titanic.columns)} cols\\ndesc have {len(desc.columns)} cols\")"
   ]
  },
  {
   "cell_type": "markdown",
Etienne Kornobis's avatar
Etienne Kornobis committed
1986
   "id": "certified-thunder",
1987
1988
1989
1990
1991
1992
1993
   "metadata": {},
   "source": [
    "`median` to get the median by columns with numerical values:"
   ]
  },
  {
   "cell_type": "code",
Etienne Kornobis's avatar
Etienne Kornobis committed
1994
1995
   "execution_count": 208,
   "id": "furnished-dealing",
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "PassengerId    446.0000\n",
       "Survived         0.0000\n",
       "Pclass           3.0000\n",
       "Age             28.0000\n",
       "SibSp            0.0000\n",
       "Parch            0.0000\n",
       "Fare            14.4542\n",
       "dtype: float64"
      ]
     },
Etienne Kornobis's avatar
Etienne Kornobis committed
2011
     "execution_count": 208,
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "titanic.median()"
   ]
  },
  {
   "cell_type": "markdown",
Etienne Kornobis's avatar
Etienne Kornobis committed
2022
   "id": "protected-fleece",
2023
2024
2025
2026
2027
2028
2029
   "metadata": {},
   "source": [
    "`mean` similarly for the mean:"
   ]
  },
  {
   "cell_type": "code",
Etienne Kornobis's avatar
Etienne Kornobis committed
2030
2031
   "execution_count": 209,
   "id": "further-circular",
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "PassengerId    446.000000\n",
       "Survived         0.383838\n",
       "Pclass           2.308642\n",
       "Age             29.699118\n",
       "SibSp            0.523008\n",
       "Parch            0.381594\n",
       "Fare            32.204208\n",
       "dtype: float64"
      ]
     },
Etienne Kornobis's avatar
Etienne Kornobis committed
2047
     "execution_count": 209,
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "titanic.mean()"
   ]
  },
  {
   "cell_type": "markdown",
Etienne Kornobis's avatar
Etienne Kornobis committed
2058
   "id": "every-skirt",
2059
2060
2061
2062
2063
2064
2065
   "metadata": {},
   "source": [
    "`value_counts` is useful the count the number of occurences of a value. For example:"
   ]
  },
  {
   "cell_type": "code",
Etienne Kornobis's avatar
Etienne Kornobis committed
2066
2067
   "execution_count": 210,
   "id": "comprehensive-division",
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "male      577\n",
       "female    314\n",
       "Name: Sex, dtype: int64"
      ]
     },
Etienne Kornobis's avatar
Etienne Kornobis committed
2078
     "execution_count": 210,
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "titanic.Sex.value_counts()"
   ]
  },
  {
   "cell_type": "markdown",
Etienne Kornobis's avatar
Etienne Kornobis committed
2089
   "id": "indirect-nutrition",
2090
2091
2092
2093
2094
2095
2096
   "metadata": {},
   "source": [
    "`max` and `min` to get the maximum and minimum:"
   ]
  },
  {
   "cell_type": "code",
Etienne Kornobis's avatar
Etienne Kornobis committed
2097
2098
   "execution_count": 211,
   "id": "universal-boutique",
2099
2100
2101
2102
2103
2104
2105
2106
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "80.0"
      ]
     },
Etienne Kornobis's avatar
Etienne Kornobis committed
2107
     "execution_count": 211,
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "titanic.Age.max()"
   ]
  },
  {
   "cell_type": "code",
Etienne Kornobis's avatar
Etienne Kornobis committed
2118
2119
   "execution_count": 212,
   "id": "several-principle",
2120
2121
2122
2123
2124
2125
2126
2127
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.42"
      ]
     },
Etienne Kornobis's avatar
Etienne Kornobis committed
2128
     "execution_count": 212,
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "titanic.Age.min()"
   ]
  },
  {
   "cell_type": "markdown",
Etienne Kornobis's avatar
Etienne Kornobis committed
2139
   "id": "eastern-timeline",
2140
2141
2142
2143
2144
2145
2146
   "metadata": {},
   "source": [
    "## DataFrame manipulation"
   ]
  },
  {
   "cell_type": "markdown",
Etienne Kornobis's avatar
Etienne Kornobis committed
2147
   "id": "primary-printer",
2148
2149
2150
2151
2152
2153
2154
   "metadata": {},
   "source": [
    "### Renaming columns"
   ]
  },
  {
   "cell_type": "code",
Etienne Kornobis's avatar
Etienne Kornobis committed
2155
2156
   "execution_count": 213,
   "id": "received-editing",
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>3</td>\n",
       "      <td>4</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>6</td>\n",
       "      <td>7</td>\n",
       "      <td>8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>9</td>\n",
       "      <td>10</td>\n",
       "      <td>11</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   A   B   C\n",
       "0  0   1   2\n",
       "1  3   4   5\n",
       "2  6   7   8\n",
       "3  9  10  11"
      ]
     },
Etienne Kornobis's avatar
Etienne Kornobis committed
2222
     "execution_count": 213,
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = pd.DataFrame(np.arange(12).reshape(4,3),\n",
    "                 columns=['A', 'B', 'C'])\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
Etienne Kornobis's avatar
Etienne Kornobis committed
2235
2236
   "execution_count": 214,
   "id": "classified-pittsburgh",
2237
2238
2239
2240
2241
2242
2243
2244
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index(['A', 'B', 'Z'], dtype='object')"
      ]
     },
Etienne Kornobis's avatar
Etienne Kornobis committed
2245
     "execution_count": 214,
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cols = list(df.columns)\n",
    "cols[2] = 'Z'\n",
    "df.columns = cols\n",
    "df.columns"
   ]
  },
  {
   "cell_type": "code",
Etienne Kornobis's avatar
Etienne Kornobis committed
2259
2260
   "execution_count": 215,
   "id": "exceptional-roberts",
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>X</th>\n",
       "      <th>Y</th>\n",
       "      <th>Z</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>3</td>\n",
       "      <td>4</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>6</td>\n",
       "      <td>7</td>\n",
       "      <td>8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>9</td>\n",
       "      <td>10</td>\n",
       "      <td>11</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   X   Y   Z\n",
       "0  0   1   2\n",
       "1  3   4   5\n",
       "2  6   7   8\n",
       "3  9  10  11"
      ]
     },
Etienne Kornobis's avatar
Etienne Kornobis committed
2326
     "execution_count": 215,
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.columns = ['X', 'Y', 'Z']\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
Etienne Kornobis's avatar
Etienne Kornobis committed
2338
2339
   "execution_count": 216,
   "id": "surprised-burns",
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>Y</th>\n",
       "      <th>Z</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>3</td>\n",
       "      <td>4</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>6</td>\n",
       "      <td>7</td>\n",
       "      <td>8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>9</td>\n",
       "      <td>10</td>\n",
       "      <td>11</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   A   Y   Z\n",
       "0  0   1   2\n",
       "1  3   4   5\n",
       "2  6   7   8\n",
       "3  9  10  11"
      ]
     },
Etienne Kornobis's avatar
Etienne Kornobis committed
2405
     "execution_count": 216,
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.rename(columns={'X': 'A'})"
   ]
  },
  {
   "cell_type": "markdown",
Etienne Kornobis's avatar
Etienne Kornobis committed
2416
   "id": "novel-sheet",
2417
2418
2419
2420
2421
2422
2423
   "metadata": {},
   "source": [
    "### Rename index"
   ]
  },
  {
   "cell_type": "code",
Etienne Kornobis's avatar
Etienne Kornobis committed
2424
2425
   "execution_count": 217,
   "id": "breathing-yeast",
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>X</th>\n",
       "      <th>Y</th>\n",
       "      <th>Z</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>a</th>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>b</th>\n",
       "      <td>3</td>\n",
       "      <td>4</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>c</th>\n",
       "      <td>6</td>\n",
       "      <td>7</td>\n",
       "      <td>8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>e</th>\n",
       "      <td>9</td>\n",
       "      <td>10</td>\n",
       "      <td>11</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   X   Y   Z\n",
       "a  0   1   2\n",
       "b  3   4   5\n",
       "c  6   7   8\n",
       "e  9  10  11"
      ]
     },
Etienne Kornobis's avatar
Etienne Kornobis committed
2491
     "execution_count": 217,
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.index = ['a', 'b', 'c', 'e']\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
Etienne Kornobis's avatar
Etienne Kornobis committed
2503
2504
   "execution_count": 218,
   "id": "central-columbus",
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>X</th>\n",
       "      <th>Y</th>\n",
       "      <th>Z</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>a</th>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>b</th>\n",
       "      <td>3</td>\n",
       "      <td>4</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>c</th>\n",
       "      <td>6</td>\n",
       "      <td>7</td>\n",
       "      <td>8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>d</th>\n",
       "      <td>9</td>\n",
       "      <td>10</td>\n",
       "      <td>11</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   X   Y   Z\n",
       "a  0   1   2\n",
       "b  3   4   5\n",
       "c  6   7   8\n",
       "d  9  10  11"
      ]
     },
Etienne Kornobis's avatar
Etienne Kornobis committed
2570
     "execution_count": 218,
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.rename(index={'e':'d'})"
   ]
  },
  {
   "cell_type": "markdown",
Etienne Kornobis's avatar
Etienne Kornobis committed
2581
   "id": "august-store",
2582
2583
2584
2585
2586
2587
2588
   "metadata": {},
   "source": [
    "### Add column"
   ]
  },
  {
   "cell_type": "code",
Etienne Kornobis's avatar
Etienne Kornobis committed
2589
2590
   "execution_count": 219,
   "id": "outer-access",
2591
2592
2593
2594
2595
2596
2597
2598