05-PandasWeather.ipynb 218 KB
Newer Older
Simon Clarke's avatar
Simon Clarke committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## A Practical Introduction to Pandas "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Pandas is one of the main Python packages for dealing with data. It is used widely in Data Science, and provides many tools for the manipulation and investigation of data sets. This tutorial introduces the basic concept of Pandas and applies these to weather data for Melbourne Airport from the Bureau of Meteorology."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Contents"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* Introduction\n",
Simon Clarke's avatar
Simon Clarke committed
29
    "* Melbourne Weather Data (Combining Pandas and Plotting)\n",
Simon Clarke's avatar
Simon Clarke committed
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
    "* Exercises"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Introduction"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Before applying Pandas to the Melbourne weather data, we will introduce some basic concepts. \n",
    "\n",
    "First we import the necessary libraries. The convention for Pandas is to use the abbreviation `pd`."
   ]
  },
  {
   "cell_type": "code",
51
   "execution_count": 2,
Simon Clarke's avatar
Simon Clarke committed
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Series"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Pandas series can be thought of as generalizations of one-dimensional arrays or of dictionaries. In their simplest form they are an array with an explicit numerical index.\n",
    "\n",
    "To create a series we use the function `pd.Series()` and pass the argument as an array."
   ]
  },
  {
   "cell_type": "code",
Simon Clarke's avatar
Simon Clarke committed
78
   "execution_count": 2,
Simon Clarke's avatar
Simon Clarke committed
79
80
81
82
83
84
85
86
87
88
89
90
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    5\n",
       "1    6\n",
       "2    7\n",
       "3    8\n",
       "dtype: int64"
      ]
     },
Simon Clarke's avatar
Simon Clarke committed
91
     "execution_count": 2,
Simon Clarke's avatar
Simon Clarke committed
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = pd.Series([5, 6, 7, 8])\n",
    "data"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The series now has two objects we can view: the values for the series and the corresponding indices."
   ]
  },
  {
   "cell_type": "code",
Simon Clarke's avatar
Simon Clarke committed
110
   "execution_count": 3,
Simon Clarke's avatar
Simon Clarke committed
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Series values: [5 6 7 8]\n",
      "Series indices: RangeIndex(start=0, stop=4, step=1)\n"
     ]
    }
   ],
   "source": [
    "print('Series values:',data.values)\n",
    "print('Series indices:',data.index)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can now access the data in the same way we would for a `numpy` array, except that we also get information about the indices."
   ]
  },
  {
   "cell_type": "code",
Simon Clarke's avatar
Simon Clarke committed
136
   "execution_count": 4,
Simon Clarke's avatar
Simon Clarke committed
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Second element of series is: 6\n",
      "Second and third elements of series are:\n",
      "1    6\n",
      "2    7\n",
      "dtype: int64\n"
     ]
    }
   ],
   "source": [
    "print('Second element of series is:',data[1])\n",
    "print('Second and third elements of series are:')\n",
    "print(data[1:3])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The indices for the series are no longer limited to being integers. For example, by specifying the argument `index`, we can make the indices a series of strings."
   ]
  },
  {
   "cell_type": "code",
Simon Clarke's avatar
Simon Clarke committed
166
   "execution_count": 5,
Simon Clarke's avatar
Simon Clarke committed
167
168
169
170
171
172
173
174
175
176
177
178
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "alpha    5\n",
       "beta     6\n",
       "gamma    7\n",
       "delta    8\n",
       "dtype: int64"
      ]
     },
Simon Clarke's avatar
Simon Clarke committed
179
     "execution_count": 5,
Simon Clarke's avatar
Simon Clarke committed
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = pd.Series([5, 6, 7, 8], index=['alpha','beta','gamma','delta'])\n",
    "data"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now the data can be referred to by our new indices. Notice that using a slice of these indices the last element is included, compared to when we used integer indices and the last element is not included. "
   ]
  },
  {
   "cell_type": "code",
Simon Clarke's avatar
Simon Clarke committed
198
   "execution_count": 6,
Simon Clarke's avatar
Simon Clarke committed
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Single element: 5\n",
      "Slice of elements:\n",
      "alpha    5\n",
      "beta     6\n",
      "gamma    7\n",
      "dtype: int64\n"
     ]
    }
   ],
   "source": [
    "print('Single element:',data['alpha'])\n",
    "print('Slice of elements:')\n",
    "print(data['alpha':'gamma'])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The series can now be sorted using the method `s.sort_values()` (here `s` represents the name of the current series). To sort our series in descending order, we can therefore use:"
   ]
  },
  {
   "cell_type": "code",
Simon Clarke's avatar
Simon Clarke committed
229
   "execution_count": 7,
Simon Clarke's avatar
Simon Clarke committed
230
231
232
233
234
235
236
237
238
239
240
241
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "delta    8\n",
       "gamma    7\n",
       "beta     6\n",
       "alpha    5\n",
       "dtype: int64"
      ]
     },
Simon Clarke's avatar
Simon Clarke committed
242
     "execution_count": 7,
Simon Clarke's avatar
Simon Clarke committed
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.sort_values(ascending=False)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We also filter the results using a `mask`. For example, if we want all values in the series equal to 6, we use:"
   ]
  },
  {
   "cell_type": "code",
Simon Clarke's avatar
Simon Clarke committed
260
   "execution_count": 8,
Simon Clarke's avatar
Simon Clarke committed
261
262
263
264
265
266
267
268
269
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "beta    6\n",
       "dtype: int64"
      ]
     },
Simon Clarke's avatar
Simon Clarke committed
270
     "execution_count": 8,
Simon Clarke's avatar
Simon Clarke committed
271
272
273
274
275
276
277
278
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data[data == 6]"
   ]
  },
279
280
281
282
283
284
285
286
287
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The mask is itself a series with booleans as values instead of the original values that can be used to subset the orginal series."
   ]
  },
  {
   "cell_type": "code",
Simon Clarke's avatar
Simon Clarke committed
288
   "execution_count": 9,
289
290
291
292
293
294
295
296
297
298
299
300
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "alpha    False\n",
       "beta      True\n",
       "gamma    False\n",
       "delta    False\n",
       "dtype: bool"
      ]
     },
Simon Clarke's avatar
Simon Clarke committed
301
     "execution_count": 9,
302
303
304
305
306
307
308
309
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data == 6"
   ]
  },
Simon Clarke's avatar
Simon Clarke committed
310
311
312
313
314
315
316
317
318
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Masks can have multiple criteria, which are separated by an `&` (and) to specify both criteria must be true, or `!` (or) to specify either criteria to be true. Each of these criteria needs to be surrounded by brackets. To find all elements in the series greater than or equal to 5 and less than 7, we can write:"
   ]
  },
  {
   "cell_type": "code",
Simon Clarke's avatar
Simon Clarke committed
319
   "execution_count": 10,
Simon Clarke's avatar
Simon Clarke committed
320
321
322
323
324
325
326
327
328
329
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "alpha    5\n",
       "beta     6\n",
       "dtype: int64"
      ]
     },
Simon Clarke's avatar
Simon Clarke committed
330
     "execution_count": 10,
Simon Clarke's avatar
Simon Clarke committed
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data[(data >= 5) & (data < 7)]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### DataFrames"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "DataFrames are two-dimensional versions of series, which can have row indices and column names. The columns can be different datatypes, e.g., integers, floats, strings or Boolean. Generally, dataframes are used much more than series, and can be thought of as analogous to an Excel worksheet or database table. \n",
    "\n",
    "There are a number of ways to create dataframes, we will just cover two here. The first is to create the dataframe from a sequence of series. Below we define three series corresponding to properties of the states and territories of Australia. Notice that for the last series the indices are ordered differently from the first two series."
   ]
  },
  {
   "cell_type": "code",
Simon Clarke's avatar
Simon Clarke committed
357
   "execution_count": 11,
Simon Clarke's avatar
Simon Clarke committed
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
   "metadata": {},
   "outputs": [],
   "source": [
    "area = pd.Series([2523924, 1723030, 1334404, 979651, 801137, 227038, 64519, 2358], \n",
    "                 index=['WA', 'QLD', 'NT', 'SA', 'NSW', 'VIC', 'TAS', 'ACT'])\n",
    "population = pd.Series([2667130, 5184847, 246500, 1770591, 8166369, 6680648, 541071, 431215], \n",
    "                 index=['WA', 'QLD', 'NT', 'SA', 'NSW', 'VIC', 'TAS', 'ACT'])\n",
    "capitals = pd.Series(['Sydney', 'Melbourne', 'Brisbane', 'Adelaide', 'Perth', 'Hobart', 'Darwin', 'Canberra'], \n",
    "                 index=['NSW', 'VIC', 'QLD', 'SA', 'WA', 'TAS', 'NT', 'ACT'])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can now create a dataframe using the function `pd.DataFrame()`, and passing the argument as a dictionary for the three series. The key for the dictionary (the first argument for each pair) is then used as the name for the columns of the dataframe. Notice that the dataframe is created so that the index of the entries match, and is then ordered based on the index."
   ]
  },
  {
   "cell_type": "code",
Simon Clarke's avatar
Simon Clarke committed
378
   "execution_count": 12,
Simon Clarke's avatar
Simon Clarke committed
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
   "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>population</th>\n",
       "      <th>area</th>\n",
       "      <th>capitals</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
Simon Clarke's avatar
Simon Clarke committed
409
       "      <td>ACT</td>\n",
Simon Clarke's avatar
Simon Clarke committed
410
411
412
413
414
       "      <td>431215</td>\n",
       "      <td>2358</td>\n",
       "      <td>Canberra</td>\n",
       "    </tr>\n",
       "    <tr>\n",
Simon Clarke's avatar
Simon Clarke committed
415
       "      <td>NSW</td>\n",
Simon Clarke's avatar
Simon Clarke committed
416
417
418
419
420
       "      <td>8166369</td>\n",
       "      <td>801137</td>\n",
       "      <td>Sydney</td>\n",
       "    </tr>\n",
       "    <tr>\n",
Simon Clarke's avatar
Simon Clarke committed
421
       "      <td>NT</td>\n",
Simon Clarke's avatar
Simon Clarke committed
422
423
424
425
426
       "      <td>246500</td>\n",
       "      <td>1334404</td>\n",
       "      <td>Darwin</td>\n",
       "    </tr>\n",
       "    <tr>\n",
Simon Clarke's avatar
Simon Clarke committed
427
       "      <td>QLD</td>\n",
Simon Clarke's avatar
Simon Clarke committed
428
429
430
431
432
       "      <td>5184847</td>\n",
       "      <td>1723030</td>\n",
       "      <td>Brisbane</td>\n",
       "    </tr>\n",
       "    <tr>\n",
Simon Clarke's avatar
Simon Clarke committed
433
       "      <td>SA</td>\n",
Simon Clarke's avatar
Simon Clarke committed
434
435
436
437
438
       "      <td>1770591</td>\n",
       "      <td>979651</td>\n",
       "      <td>Adelaide</td>\n",
       "    </tr>\n",
       "    <tr>\n",
Simon Clarke's avatar
Simon Clarke committed
439
       "      <td>TAS</td>\n",
Simon Clarke's avatar
Simon Clarke committed
440
441
442
443
444
       "      <td>541071</td>\n",
       "      <td>64519</td>\n",
       "      <td>Hobart</td>\n",
       "    </tr>\n",
       "    <tr>\n",
Simon Clarke's avatar
Simon Clarke committed
445
       "      <td>VIC</td>\n",
Simon Clarke's avatar
Simon Clarke committed
446
447
448
449
450
       "      <td>6680648</td>\n",
       "      <td>227038</td>\n",
       "      <td>Melbourne</td>\n",
       "    </tr>\n",
       "    <tr>\n",
Simon Clarke's avatar
Simon Clarke committed
451
       "      <td>WA</td>\n",
Simon Clarke's avatar
Simon Clarke committed
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
       "      <td>2667130</td>\n",
       "      <td>2523924</td>\n",
       "      <td>Perth</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     population     area   capitals\n",
       "ACT      431215     2358   Canberra\n",
       "NSW     8166369   801137     Sydney\n",
       "NT       246500  1334404     Darwin\n",
       "QLD     5184847  1723030   Brisbane\n",
       "SA      1770591   979651   Adelaide\n",
       "TAS      541071    64519     Hobart\n",
       "VIC     6680648   227038  Melbourne\n",
       "WA      2667130  2523924      Perth"
      ]
     },
Simon Clarke's avatar
Simon Clarke committed
472
     "execution_count": 12,
Simon Clarke's avatar
Simon Clarke committed
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "states = pd.DataFrame({'population': population,\n",
    "                       'area': area,\n",
    "                       'capitals': capitals})\n",
    "states"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
488
    "The datatypes for each column are then stored in the property `df.dtypes` (here `df` refers to the name of the current dataframe). In this case, everything is as expected."
Simon Clarke's avatar
Simon Clarke committed
489
490
491
492
   ]
  },
  {
   "cell_type": "code",
Simon Clarke's avatar
Simon Clarke committed
493
   "execution_count": 13,
Simon Clarke's avatar
Simon Clarke committed
494
495
496
497
498
499
500
501
502
503
504
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "population     int64\n",
       "area           int64\n",
       "capitals      object\n",
       "dtype: object"
      ]
     },
Simon Clarke's avatar
Simon Clarke committed
505
     "execution_count": 13,
Simon Clarke's avatar
Simon Clarke committed
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "states.dtypes"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can view other properties of the dataframe, e.g., the column names and the row indices."
   ]
  },
  {
   "cell_type": "code",
Simon Clarke's avatar
Simon Clarke committed
523
   "execution_count": 14,
Simon Clarke's avatar
Simon Clarke committed
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Column names: Index(['population', 'area', 'capitals'], dtype='object')\n",
      "Row indices: Index(['ACT', 'NSW', 'NT', 'QLD', 'SA', 'TAS', 'VIC', 'WA'], dtype='object')\n"
     ]
    }
   ],
   "source": [
    "print('Column names:',states.columns)\n",
    "print('Row indices:',states.index)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The other way to create the dataframe is to specify an array or list as the argument. Here we use an `numpy` array as the argument and use the `values` for the series which have already been defined. We could also explicitly define the arrays. The argument `T` at the end of the array is to take the transpose, i.e., swap the rows with the columns. The first problem that we notice is that because the indices for the capitals are different from that of the other two columns, the capitals are incorrectly assigned."
   ]
  },
  {
   "cell_type": "code",
Simon Clarke's avatar
Simon Clarke committed
549
   "execution_count": 15,
Simon Clarke's avatar
Simon Clarke committed
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
   "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>area</th>\n",
       "      <th>population</th>\n",
       "      <th>capitals</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
Simon Clarke's avatar
Simon Clarke committed
580
       "      <td>WA</td>\n",
Simon Clarke's avatar
Simon Clarke committed
581
582
583
584
585
       "      <td>2523924</td>\n",
       "      <td>2667130</td>\n",
       "      <td>Sydney</td>\n",
       "    </tr>\n",
       "    <tr>\n",
Simon Clarke's avatar
Simon Clarke committed
586
       "      <td>QLD</td>\n",
Simon Clarke's avatar
Simon Clarke committed
587
588
589
590
591
       "      <td>1723030</td>\n",
       "      <td>5184847</td>\n",
       "      <td>Melbourne</td>\n",
       "    </tr>\n",
       "    <tr>\n",
Simon Clarke's avatar
Simon Clarke committed
592
       "      <td>NT</td>\n",
Simon Clarke's avatar
Simon Clarke committed
593
594
595
596
597
       "      <td>1334404</td>\n",
       "      <td>246500</td>\n",
       "      <td>Brisbane</td>\n",
       "    </tr>\n",
       "    <tr>\n",
Simon Clarke's avatar
Simon Clarke committed
598
       "      <td>SA</td>\n",
Simon Clarke's avatar
Simon Clarke committed
599
600
601
602
603
       "      <td>979651</td>\n",
       "      <td>1770591</td>\n",
       "      <td>Adelaide</td>\n",
       "    </tr>\n",
       "    <tr>\n",
Simon Clarke's avatar
Simon Clarke committed
604
       "      <td>NSW</td>\n",
Simon Clarke's avatar
Simon Clarke committed
605
606
607
608
609
       "      <td>801137</td>\n",
       "      <td>8166369</td>\n",
       "      <td>Perth</td>\n",
       "    </tr>\n",
       "    <tr>\n",
Simon Clarke's avatar
Simon Clarke committed
610
       "      <td>VIC</td>\n",
Simon Clarke's avatar
Simon Clarke committed
611
612
613
614
615
       "      <td>227038</td>\n",
       "      <td>6680648</td>\n",
       "      <td>Hobart</td>\n",
       "    </tr>\n",
       "    <tr>\n",
Simon Clarke's avatar
Simon Clarke committed
616
       "      <td>TAS</td>\n",
Simon Clarke's avatar
Simon Clarke committed
617
618
619
620
621
       "      <td>64519</td>\n",
       "      <td>541071</td>\n",
       "      <td>Darwin</td>\n",
       "    </tr>\n",
       "    <tr>\n",
Simon Clarke's avatar
Simon Clarke committed
622
       "      <td>ACT</td>\n",
Simon Clarke's avatar
Simon Clarke committed
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
       "      <td>2358</td>\n",
       "      <td>431215</td>\n",
       "      <td>Canberra</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "        area population   capitals\n",
       "WA   2523924    2667130     Sydney\n",
       "QLD  1723030    5184847  Melbourne\n",
       "NT   1334404     246500   Brisbane\n",
       "SA    979651    1770591   Adelaide\n",
       "NSW   801137    8166369      Perth\n",
       "VIC   227038    6680648     Hobart\n",
       "TAS    64519     541071     Darwin\n",
       "ACT     2358     431215   Canberra"
      ]
     },
Simon Clarke's avatar
Simon Clarke committed
643
     "execution_count": 15,
Simon Clarke's avatar
Simon Clarke committed
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "states_alt = pd.DataFrame(np.array([area.values, population.values, capitals.values]).T,\n",
    "             columns=['area','population','capitals'],\n",
    "             index=['WA', 'QLD', 'NT', 'SA', 'NSW', 'VIC', 'TAS', 'ACT'])\n",
    "states_alt"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The second problem is that `numpy` creates an array with all the same data types, and consequently, tries to make everything a string. If we view `df.dtypes` all the columns are `object` (string)."
   ]
  },
  {
   "cell_type": "code",
Simon Clarke's avatar
Simon Clarke committed
664
   "execution_count": 16,
Simon Clarke's avatar
Simon Clarke committed
665
666
667
668
669
670
671
672
673
674
675
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "area          object\n",
       "population    object\n",
       "capitals      object\n",
       "dtype: object"
      ]
     },
Simon Clarke's avatar
Simon Clarke committed
676
     "execution_count": 16,
Simon Clarke's avatar
Simon Clarke committed
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "states_alt.dtypes"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Hence, if you are using an array to create a dataframe, you need to beware of these gotchas. The following cell shows how lists can be used to correctly create the required dataframe."
   ]
  },
  {
   "cell_type": "code",
Simon Clarke's avatar
Simon Clarke committed
694
   "execution_count": 17,
Simon Clarke's avatar
Simon Clarke committed
695
   "metadata": {},
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
   "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>area</th>\n",
       "      <th>population</th>\n",
       "      <th>capitals</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
Simon Clarke's avatar
Simon Clarke committed
725
       "      <td>WA</td>\n",
726
727
728
729
730
       "      <td>2523924</td>\n",
       "      <td>2667130</td>\n",
       "      <td>Perth</td>\n",
       "    </tr>\n",
       "    <tr>\n",
Simon Clarke's avatar
Simon Clarke committed
731
       "      <td>QLD</td>\n",
732
733
734
735
736
       "      <td>1723030</td>\n",
       "      <td>5184847</td>\n",
       "      <td>Brisbane</td>\n",
       "    </tr>\n",
       "    <tr>\n",
Simon Clarke's avatar
Simon Clarke committed
737
       "      <td>NT</td>\n",
738
739
740
741
742
       "      <td>1334404</td>\n",
       "      <td>246500</td>\n",
       "      <td>Darwin</td>\n",
       "    </tr>\n",
       "    <tr>\n",
Simon Clarke's avatar
Simon Clarke committed
743
       "      <td>SA</td>\n",
744
745
746
747
748
       "      <td>979651</td>\n",
       "      <td>1770591</td>\n",
       "      <td>Adelaide</td>\n",
       "    </tr>\n",
       "    <tr>\n",
Simon Clarke's avatar
Simon Clarke committed
749
       "      <td>NSW</td>\n",
750
751
752
753
754
       "      <td>801137</td>\n",
       "      <td>8166369</td>\n",
       "      <td>Sydney</td>\n",
       "    </tr>\n",
       "    <tr>\n",
Simon Clarke's avatar
Simon Clarke committed
755
       "      <td>VIC</td>\n",
756
757
758
759
760
       "      <td>227038</td>\n",
       "      <td>6680648</td>\n",
       "      <td>Melbourne</td>\n",
       "    </tr>\n",
       "    <tr>\n",
Simon Clarke's avatar
Simon Clarke committed
761
       "      <td>TAS</td>\n",
762
763
764
765
766
       "      <td>64519</td>\n",
       "      <td>541071</td>\n",
       "      <td>Hobart</td>\n",
       "    </tr>\n",
       "    <tr>\n",
Simon Clarke's avatar
Simon Clarke committed
767
       "      <td>ACT</td>\n",
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
       "      <td>2358</td>\n",
       "      <td>431215</td>\n",
       "      <td>Canberra</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "        area  population   capitals\n",
       "WA   2523924     2667130      Perth\n",
       "QLD  1723030     5184847   Brisbane\n",
       "NT   1334404      246500     Darwin\n",
       "SA    979651     1770591   Adelaide\n",
       "NSW   801137     8166369     Sydney\n",
       "VIC   227038     6680648  Melbourne\n",
       "TAS    64519      541071     Hobart\n",
       "ACT     2358      431215   Canberra"
      ]
     },
Simon Clarke's avatar
Simon Clarke committed
788
     "execution_count": 17,
789
790
791
792
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
Simon Clarke's avatar
Simon Clarke committed
793
794
795
796
797
798
799
800
801
802
   "source": [
    "states_alt = pd.DataFrame([[2523924, 2667130, 'Perth'],\n",
    "                           [1723030, 5184847, 'Brisbane'],\n",
    "                           [1334404, 246500,  'Darwin'],\n",
    "                           [979651,  1770591, 'Adelaide'],\n",
    "                           [801137, 8166369, 'Sydney'],\n",
    "                           [227038, 6680648, 'Melbourne'],\n",
    "                           [64519, 541071, 'Hobart'],\n",
    "                           [2358, 431215, 'Canberra']],\n",
    "                          columns=['area','population','capitals'],\n",
803
804
805
    "                          index=['WA', 'QLD', 'NT', 'SA', 'NSW', 'VIC', 'TAS', 'ACT'])\n",
    "\n",
    "states_alt"
Simon Clarke's avatar
Simon Clarke committed
806
807
808
809
810
811
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
812
    "#### Manipulating Dataframes\n",
Simon Clarke's avatar
Simon Clarke committed
813
814
815
816
817
    "As with series, we can sort dataframes based on specific columns, by using the method `df.sort_values()` and specificing the argument `by`. If we want to sort the dataframe by descending population, then we can use:"
   ]
  },
  {
   "cell_type": "code",
Simon Clarke's avatar
Simon Clarke committed
818
   "execution_count": 18,
Simon Clarke's avatar
Simon Clarke committed
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
   "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>population</th>\n",
       "      <th>area</th>\n",
       "      <th>capitals</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
Simon Clarke's avatar
Simon Clarke committed
849
       "      <td>NSW</td>\n",
Simon Clarke's avatar
Simon Clarke committed
850
851
852
853
854
       "      <td>8166369</td>\n",
       "      <td>801137</td>\n",
       "      <td>Sydney</td>\n",
       "    </tr>\n",
       "    <tr>\n",
Simon Clarke's avatar
Simon Clarke committed
855
       "      <td>VIC</td>\n",
Simon Clarke's avatar
Simon Clarke committed
856
857
858
859
860
       "      <td>6680648</td>\n",
       "      <td>227038</td>\n",
       "      <td>Melbourne</td>\n",
       "    </tr>\n",
       "    <tr>\n",
Simon Clarke's avatar
Simon Clarke committed
861
       "      <td>QLD</td>\n",
Simon Clarke's avatar
Simon Clarke committed
862
863
864
865
866
       "      <td>5184847</td>\n",
       "      <td>1723030</td>\n",
       "      <td>Brisbane</td>\n",
       "    </tr>\n",
       "    <tr>\n",
Simon Clarke's avatar
Simon Clarke committed
867
       "      <td>WA</td>\n",
Simon Clarke's avatar
Simon Clarke committed
868
869
870
871
872
       "      <td>2667130</td>\n",
       "      <td>2523924</td>\n",
       "      <td>Perth</td>\n",
       "    </tr>\n",
       "    <tr>\n",
Simon Clarke's avatar
Simon Clarke committed
873
       "      <td>SA</td>\n",
Simon Clarke's avatar
Simon Clarke committed
874
875
876
877
878
       "      <td>1770591</td>\n",
       "      <td>979651</td>\n",
       "      <td>Adelaide</td>\n",
       "    </tr>\n",
       "    <tr>\n",
Simon Clarke's avatar
Simon Clarke committed
879
       "      <td>TAS</td>\n",
Simon Clarke's avatar
Simon Clarke committed
880
881
882
883
884
       "      <td>541071</td>\n",
       "      <td>64519</td>\n",
       "      <td>Hobart</td>\n",
       "    </tr>\n",
       "    <tr>\n",
Simon Clarke's avatar
Simon Clarke committed
885
       "      <td>ACT</td>\n",
Simon Clarke's avatar
Simon Clarke committed
886
887
888
889
890
       "      <td>431215</td>\n",
       "      <td>2358</td>\n",
       "      <td>Canberra</td>\n",
       "    </tr>\n",
       "    <tr>\n",
Simon Clarke's avatar
Simon Clarke committed
891
       "      <td>NT</td>\n",
Simon Clarke's avatar
Simon Clarke committed
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
       "      <td>246500</td>\n",
       "      <td>1334404</td>\n",
       "      <td>Darwin</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     population     area   capitals\n",
       "NSW     8166369   801137     Sydney\n",
       "VIC     6680648   227038  Melbourne\n",
       "QLD     5184847  1723030   Brisbane\n",
       "WA      2667130  2523924      Perth\n",
       "SA      1770591   979651   Adelaide\n",
       "TAS      541071    64519     Hobart\n",
       "ACT      431215     2358   Canberra\n",
       "NT       246500  1334404     Darwin"
      ]
     },
Simon Clarke's avatar
Simon Clarke committed
912
     "execution_count": 18,
Simon Clarke's avatar
Simon Clarke committed
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "states.sort_values(by=['population'], ascending=False)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Operations on the elements of the dataframe are now very easy. For example, if we want to calculate the population density in terms of people per square km, we just need to divide the population column by the area column. This can be assigned to a new column, as shown in the following cell."
   ]
  },
  {
   "cell_type": "code",
Simon Clarke's avatar
Simon Clarke committed
930
   "execution_count": 19,
Simon Clarke's avatar
Simon Clarke committed
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
   "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>population</th>\n",
       "      <th>area</th>\n",
       "      <th>capitals</th>\n",
       "      <th>density</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
Simon Clarke's avatar
Simon Clarke committed
962
       "      <td>ACT</td>\n",
Simon Clarke's avatar
Simon Clarke committed
963
964
965
966
967
968
       "      <td>431215</td>\n",
       "      <td>2358</td>\n",
       "      <td>Canberra</td>\n",
       "      <td>182.873198</td>\n",
       "    </tr>\n",
       "    <tr>\n",
Simon Clarke's avatar
Simon Clarke committed
969
       "      <td>NSW</td>\n",
Simon Clarke's avatar
Simon Clarke committed
970
971
972
973
974
975
       "      <td>8166369</td>\n",
       "      <td>801137</td>\n",
       "      <td>Sydney</td>\n",
       "      <td>10.193474</td>\n",
       "    </tr>\n",
       "    <tr>\n",
Simon Clarke's avatar
Simon Clarke committed
976
       "      <td>NT</td>\n",
Simon Clarke's avatar
Simon Clarke committed
977
978
979
980
981
982
       "      <td>246500</td>\n",
       "      <td>1334404</td>\n",
       "      <td>Darwin</td>\n",
       "      <td>0.184727</td>\n",
       "    </tr>\n",
       "    <tr>\n",
Simon Clarke's avatar
Simon Clarke committed
983
       "      <td>QLD</td>\n",
Simon Clarke's avatar
Simon Clarke committed
984
985
986
987
988
989
       "      <td>5184847</td>\n",
       "      <td>1723030</td>\n",
       "      <td>Brisbane</td>\n",
       "      <td>3.009145</td>\n",
       "    </tr>\n",
       "    <tr>\n",
Simon Clarke's avatar
Simon Clarke committed
990
       "      <td>SA</td>\n",
Simon Clarke's avatar
Simon Clarke committed
991
992
993
994
995
996
       "      <td>1770591</td>\n",
       "      <td>979651</td>\n",
       "      <td>Adelaide</td>\n",
       "      <td>1.807369</td>\n",
       "    </tr>\n",
       "    <tr>\n",
Simon Clarke's avatar
Simon Clarke committed
997
       "      <td>TAS</td>\n",
Simon Clarke's avatar
Simon Clarke committed
998
999
1000
1001
1002
1003
       "      <td>541071</td>\n",
       "      <td>64519</td>\n",
       "      <td>Hobart</td>\n",
       "      <td>8.386227</td>\n",
       "    </tr>\n",
       "    <tr>\n",
Simon Clarke's avatar
Simon Clarke committed
1004
       "      <td>VIC</td>\n",
Simon Clarke's avatar
Simon Clarke committed
1005
1006
1007
1008
1009
1010
       "      <td>6680648</td>\n",
       "      <td>227038</td>\n",
       "      <td>Melbourne</td>\n",
       "      <td>29.425242</td>\n",
       "    </tr>\n",
       "    <tr>\n",
Simon Clarke's avatar
Simon Clarke committed
1011
       "      <td>WA</td>\n",
Simon Clarke's avatar
Simon Clarke committed
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
       "      <td>2667130</td>\n",
       "      <td>2523924</td>\n",
       "      <td>Perth</td>\n",
       "      <td>1.056739</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     population     area   capitals     density\n",
       "ACT      431215     2358   Canberra  182.873198\n",
       "NSW     8166369   801137     Sydney   10.193474\n",
       "NT       246500  1334404     Darwin    0.184727\n",
       "QLD     5184847  1723030   Brisbane    3.009145\n",
       "SA      1770591   979651   Adelaide    1.807369\n",
       "TAS      541071    64519     Hobart    8.386227\n",
       "VIC     6680648   227038  Melbourne   29.425242\n",
       "WA      2667130  2523924      Perth    1.056739"
      ]
     },
Simon Clarke's avatar
Simon Clarke committed
1033
     "execution_count": 19,
Simon Clarke's avatar
Simon Clarke committed
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "states['density'] = states['population']/states['area']\n",
    "states"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "To view a subset of the columns of the dataframe, we just need to specify a list of the required columns. For example, to view only the area and population we can use:"
   ]
  },
  {
   "cell_type": "code",
Simon Clarke's avatar
Simon Clarke committed
1052
   "execution_count": 20,
Simon Clarke's avatar
Simon Clarke committed
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
   "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>area</th>\n",
       "      <th>population</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
Simon Clarke's avatar
Simon Clarke committed
1082
       "      <td>ACT</td>\n",
Simon Clarke's avatar
Simon Clarke committed
1083
1084
1085
1086
       "      <td>2358</td>\n",
       "      <td>431215</td>\n",
       "    </tr>\n",
       "    <tr>\n",
Simon Clarke's avatar
Simon Clarke committed
1087
       "      <td>NSW</td>\n",
Simon Clarke's avatar
Simon Clarke committed
1088
1089
1090
1091
       "      <td>801137</td>\n",
       "      <td>8166369</td>\n",
       "    </tr>\n",
       "    <tr>\n",
Simon Clarke's avatar
Simon Clarke committed
1092
       "      <td>NT</td>\n",
Simon Clarke's avatar
Simon Clarke committed
1093
1094
1095
1096
       "      <td>1334404</td>\n",
       "      <td>246500</td>\n",
       "    </tr>\n",
       "    <tr>\n",
Simon Clarke's avatar
Simon Clarke committed
1097
       "      <td>QLD</td>\n",
Simon Clarke's avatar
Simon Clarke committed
1098
1099
1100
1101
       "      <td>1723030</td>\n",
       "      <td>5184847</td>\n",
       "    </tr>\n",
       "    <tr>\n",
Simon Clarke's avatar
Simon Clarke committed
1102
       "      <td>SA</td>\n",
Simon Clarke's avatar
Simon Clarke committed
1103
1104
1105
1106
       "      <td>979651</td>\n",
       "      <td>1770591</td>\n",
       "    </tr>\n",
       "    <tr>\n",
Simon Clarke's avatar
Simon Clarke committed
1107
       "      <td>TAS</td>\n",
Simon Clarke's avatar
Simon Clarke committed
1108
1109
1110
1111
       "      <td>64519</td>\n",
       "      <td>541071</td>\n",
       "    </tr>\n",
       "    <tr>\n",
Simon Clarke's avatar
Simon Clarke committed
1112
       "      <td>VIC</td>\n",
Simon Clarke's avatar
Simon Clarke committed
1113
1114
1115
1116
       "      <td>227038</td>\n",
       "      <td>6680648</td>\n",
       "    </tr>\n",
       "    <tr>\n",
Simon Clarke's avatar
Simon Clarke committed
1117
       "      <td>WA</td>\n",
Simon Clarke's avatar
Simon Clarke committed
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
       "      <td>2523924</td>\n",
       "      <td>2667130</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "        area  population\n",
       "ACT     2358      431215\n",
       "NSW   801137     8166369\n",
       "NT   1334404      246500\n",
       "QLD  1723030     5184847\n",
       "SA    979651     1770591\n",
       "TAS    64519      541071\n",
       "VIC   227038     6680648\n",
       "WA   2523924     2667130"
      ]
     },
Simon Clarke's avatar
Simon Clarke committed
1137
     "execution_count": 20,
Simon Clarke's avatar
Simon Clarke committed
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "states[['area','population']]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Each of the columns is also a property of the dataframe, so we could view the area by specifying:"
   ]
  },
  {
   "cell_type": "code",
Simon Clarke's avatar
Simon Clarke committed
1155
   "execution_count": 21,
Simon Clarke's avatar
Simon Clarke committed
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "ACT       2358\n",
       "NSW     801137\n",
       "NT     1334404\n",
       "QLD    1723030\n",
       "SA      979651\n",
       "TAS      64519\n",
       "VIC     227038\n",
       "WA     2523924\n",
       "Name: area, dtype: int64"
      ]
     },
Simon Clarke's avatar
Simon Clarke committed
1172
     "execution_count": 21,
Simon Clarke's avatar
Simon Clarke committed
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "states.area"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "As with series we can filter the rows of the dataframe based on their column properties. If we want to find the states with population density between 1 and 5, we can create a mask and specify that as the argument for the dataframe:"
   ]
  },
  {
   "cell_type": "code",
Simon Clarke's avatar
Simon Clarke committed
1190
   "execution_count": 22,
Simon Clarke's avatar
Simon Clarke committed
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
1220
1221
   "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>population</th>\n",
       "      <th>area</th>\n",
       "      <th>capitals</th>\n",
       "      <th>density</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
Simon Clarke's avatar
Simon Clarke committed
1222
       "      <td>QLD</td>\n",
Simon Clarke's avatar
Simon Clarke committed
1223
1224
1225
1226
1227
1228
       "      <td>5184847</td>\n",
       "      <td>1723030</td>\n",
       "      <td>Brisbane</td>\n",
       "      <td>3.009145</td>\n",
       "    </tr>\n",
       "    <tr>\n",
Simon Clarke's avatar
Simon Clarke committed
1229
       "      <td>SA</td>\n",
Simon Clarke's avatar
Simon Clarke committed
1230
1231
1232
1233
1234
1235
       "      <td>1770591</td>\n",
       "      <td>979651</td>\n",
       "      <td>Adelaide</td>\n",
       "      <td>1.807369</td>\n",
       "    </tr>\n",
       "    <tr>\n",
Simon Clarke's avatar
Simon Clarke committed
1236
       "      <td>WA</td>\n",
Simon Clarke's avatar
Simon Clarke committed
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
       "      <td>2667130</td>\n",
       "      <td>2523924</td>\n",
       "      <td>Perth</td>\n",
       "      <td>1.056739</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     population     area  capitals   density\n",
       "QLD     5184847  1723030  Brisbane  3.009145\n",
       "SA      1770591   979651  Adelaide  1.807369\n",
       "WA      2667130  2523924     Perth  1.056739"
      ]
     },
Simon Clarke's avatar
Simon Clarke committed
1253
     "execution_count": 22,
Simon Clarke's avatar
Simon Clarke committed
1254
1255
1256
1257
1258
1259
1260
1261
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "states[(states.density < 5) & (states.density > 1)]"
   ]
  },
Simon Clarke's avatar
Simon Clarke committed
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
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The filter components do not need to be based on tests of the same columns. For example, if we want to find states with areas less than 1,000,000 square km and populations less than 2,000,000, we can use:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "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>population</th>\n",
       "      <th>area</th>\n",
       "      <th>capitals</th>\n",
       "      <th>density</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <td>ACT</td>\n",
       "      <td>431215</td>\n",
       "      <td>2358</td>\n",
       "      <td>Canberra</td>\n",
       "      <td>182.873198</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>SA</td>\n",
       "      <td>1770591</td>\n",
       "      <td>979651</td>\n",
       "      <td>Adelaide</td>\n",
       "      <td>1.807369</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>TAS</td>\n",
       "      <td>541071</td>\n",
       "      <td>64519</td>\n",
       "      <td>Hobart</td>\n",
       "      <td>8.386227</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     population    area  capitals     density\n",
       "ACT      431215    2358  Canberra  182.873198\n",
       "SA      1770591  979651  Adelaide    1.807369\n",
       "TAS      541071   64519    Hobart    8.386227"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "states[(states.area < 1000000) & (states.population < 2000000)]"
   ]
  },
Simon Clarke's avatar
Simon Clarke committed
1343
1344
1345
1346
1347
1348
1349
1350
1351
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Often we want to update the index of a dataframe. As an example consider creating the states dataframe, without setting the index. The index will then be an integer ranging from 0 to the number of rows of the dataframe."
   ]
  },
  {
   "cell_type": "code",
Simon Clarke's avatar
Simon Clarke committed
1352
   "execution_count": 24,
Simon Clarke's avatar
Simon Clarke committed
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
   "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>area</th>\n",
       "      <th>population</th>\n",
       "      <th>capitals</th>\n",
       "      <th>state</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
Simon Clarke's avatar
Simon Clarke committed
1384
       "      <td>0</td>\n",
Simon Clarke's avatar
Simon Clarke committed
1385
1386
1387
1388
1389
1390
       "      <td>2523924</td>\n",
       "      <td>2667130</td>\n",
       "      <td>Perth</td>\n",
       "      <td>WA</td>\n",
       "    </tr>\n",
       "    <tr>\n",
Simon Clarke's avatar
Simon Clarke committed
1391
       "      <td>1</td>\n",
Simon Clarke's avatar
Simon Clarke committed
1392
1393
1394
1395
1396
1397
       "      <td>1723030</td>\n",
       "      <td>5184847</td>\n",
       "      <td>Brisbane</td>\n",
       "      <td>QLD</td>\n",
       "    </tr>\n",
       "    <tr>\n",
Simon Clarke's avatar
Simon Clarke committed
1398
       "      <td>2</td>\n",
Simon Clarke's avatar
Simon Clarke committed
1399
1400
1401
1402
1403
1404
       "      <td>1334404</td>\n",
       "      <td>246500</td>\n",
       "      <td>Darwin</td>\n",
       "      <td>NT</td>\n",
       "    </tr>\n",
       "    <tr>\n",
Simon Clarke's avatar
Simon Clarke committed
1405
       "      <td>3</td>\n",
Simon Clarke's avatar
Simon Clarke committed
1406
1407
1408
1409
1410
1411
       "      <td>979651</td>\n",
       "      <td>1770591</td>\n",
       "      <td>Adelaide</td>\n",
       "      <td>SA</td>\n",
       "    </tr>\n",
       "    <tr>\n",
Simon Clarke's avatar
Simon Clarke committed
1412
       "      <td>4</td>\n",
Simon Clarke's avatar
Simon Clarke committed
1413
1414
1415
1416
1417
1418
       "      <td>801137</td>\n",
       "      <td>8166369</td>\n",
       "      <td>Sydney</td>\n",
       "      <td>NSW</td>\n",
       "    </tr>\n",
       "    <tr>\n",
Simon Clarke's avatar
Simon Clarke committed
1419
       "      <td>5</td>\n",
Simon Clarke's avatar
Simon Clarke committed
1420
1421
1422
1423
1424
1425
       "      <td>227038</td>\n",
       "      <td>6680648</td>\n",
       "      <td>Melbourne</td>\n",
       "      <td>VIC</td>\n",
       "    </tr>\n",
       "    <tr>\n",
Simon Clarke's avatar
Simon Clarke committed
1426
       "      <td>6</td>\n",
Simon Clarke's avatar
Simon Clarke committed
1427
1428
1429
1430
1431
1432
       "      <td>64519</td>\n",
       "      <td>541071</td>\n",
       "      <td>Hobart</td>\n",
       "      <td>TAS</td>\n",
       "    </tr>\n",
       "    <tr>\n",
Simon Clarke's avatar
Simon Clarke committed
1433
       "      <td>7</td>\n",
Simon Clarke's avatar
Simon Clarke committed
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
       "      <td>2358</td>\n",
       "      <td>431215</td>\n",
       "      <td>Canberra</td>\n",
       "      <td>ACT</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "      area  population   capitals state\n",
       "0  2523924     2667130      Perth    WA\n",
       "1  1723030     5184847   Brisbane   QLD\n",
       "2  1334404      246500     Darwin    NT\n",
       "3   979651     1770591   Adelaide    SA\n",
       "4   801137     8166369     Sydney   NSW\n",
       "5   227038     6680648  Melbourne   VIC\n",
       "6    64519      541071     Hobart   TAS\n",
       "7     2358      431215   Canberra   ACT"
      ]
     },
Simon Clarke's avatar
Simon Clarke committed
1455
     "execution_count": 24,
Simon Clarke's avatar
Simon Clarke committed
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
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "states = pd.DataFrame([[2523924, 2667130, 'Perth', 'WA'],\n",
    "                           [1723030, 5184847, 'Brisbane', 'QLD'],\n",
    "                           [1334404, 246500,  'Darwin', 'NT'],\n",
    "                           [979651,  1770591, 'Adelaide', 'SA'],\n",
    "                           [801137, 8166369, 'Sydney', 'NSW'],\n",
    "                           [227038, 6680648, 'Melbourne', 'VIC'],\n",
    "                           [64519, 541071, 'Hobart', 'TAS'],\n",
    "                           [2358, 431215, 'Canberra', 'ACT']],\n",
    "                          columns=['area','population','capitals', 'state'])\n",
    "states"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "If we want to change the index to the specific state, then we can use `df.set_index()`. Note the column that is chosen to be the new index should have unique values in each row. The argument `inplace=True`, says to update the current dataframe, rather than copy to a new dataframe."
   ]
  },
  {
   "cell_type": "code",
Simon Clarke's avatar
Simon Clarke committed
1482
   "execution_count": 25,
Simon Clarke's avatar
Simon Clarke committed
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
   "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>area</th>\n",
       "      <th>population</th>\n",
       "      <th>capitals</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>state</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
Simon Clarke's avatar
Simon Clarke committed
1519
       "      <td>WA</td>\n",
Simon Clarke's avatar
Simon Clarke committed
1520
1521
1522
1523
1524
       "      <td>2523924</td>\n",
       "      <td>2667130</td>\n",
       "      <td>Perth</td>\n",
       "    </tr>\n",
       "    <tr>\n",
Simon Clarke's avatar
Simon Clarke committed
1525
       "      <td>QLD</td>\n",
Simon Clarke's avatar
Simon Clarke committed
1526
1527
1528
1529
1530
       "      <td>1723030</td>\n",
       "      <td>5184847</td>\n",
       "      <td>Brisbane</td>\n",
       "    </tr>\n",
       "    <tr>\n",
Simon Clarke's avatar
Simon Clarke committed
1531
       "      <td>NT</td>\n",
Simon Clarke's avatar
Simon Clarke committed
1532
1533
1534
1535
1536
       "      <td>1334404</td>\n",
       "      <td>246500</td>\n",
       "      <td>Darwin</td>\n",
       "    </tr>\n",
       "    <tr>\n",
Simon Clarke's avatar
Simon Clarke committed
1537
       "      <td>SA</td>\n",
Simon Clarke's avatar
Simon Clarke committed
1538
1539
1540
1541
1542
       "      <td>979651</td>\n",
       "      <td>1770591</td>\n",
       "      <td>Adelaide</td>\n",
       "    </tr>\n",
       "    <tr>\n",
Simon Clarke's avatar
Simon Clarke committed
1543
       "      <td>NSW</td>\n",
Simon Clarke's avatar
Simon Clarke committed
1544
1545
1546
1547
1548
       "      <td>801137</td>\n",
       "      <td>8166369</td>\n",
       "      <td>Sydney</td>\n",
       "    </tr>\n",
       "    <tr>\n",
Simon Clarke's avatar
Simon Clarke committed
1549
       "      <td>VIC</td>\n",
Simon Clarke's avatar
Simon Clarke committed
1550
1551
1552
1553
1554
       "      <td>227038</td>\n",
       "      <td>6680648</td>\n",
       "      <td>Melbourne</td>\n",
       "    </tr>\n",
       "    <tr>\n",
Simon Clarke's avatar
Simon Clarke committed
1555
       "      <td>TAS</td>\n",
Simon Clarke's avatar
Simon Clarke committed
1556
1557
1558
1559
1560
       "      <td>64519</td>\n",
       "      <td>541071</td>\n",
       "      <td>Hobart</td>\n",
       "    </tr>\n",
       "    <tr>\n",
Simon Clarke's avatar
Simon Clarke committed
1561
       "      <td>ACT</td>\n",
Simon Clarke's avatar
Simon Clarke committed
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
       "      <td>2358</td>\n",
       "      <td>431215</td>\n",
       "      <td>Canberra</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          area  population   capitals\n",
       "state                                \n",
       "WA     2523924     2667130      Perth\n",
       "QLD    1723030     5184847   Brisbane\n",
       "NT     1334404      246500     Darwin\n",
       "SA      979651     1770591   Adelaide\n",
       "NSW     801137     8166369     Sydney\n",
       "VIC     227038     6680648  Melbourne\n",
       "TAS      64519      541071     Hobart\n",
       "ACT       2358      431215   Canberra"
      ]
     },
Simon Clarke's avatar
Simon Clarke committed
1583
     "execution_count": 25,
Simon Clarke's avatar
Simon Clarke committed
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "states.set_index('state', inplace=True)\n",
    "states"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "There are two ways to reference elements or block of elements in the dataframe. The first of these is `df.loc` which refers to the location by the column and index names. So to refer to the WA, QLD and NT, areas and populations we can use:"
   ]
  },
  {
   "cell_type": "code",
Simon Clarke's avatar
Simon Clarke committed
1602
   "execution_count": 26,
Simon Clarke's avatar
Simon Clarke committed
1603
1604
1605
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
   "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>area</th>\n",
       "      <th>population</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>state</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
Simon Clarke's avatar
Simon Clarke committed
1637
       "      <td>WA</td>\n",
Simon Clarke's avatar
Simon Clarke committed
1638
1639
1640
1641
       "      <td>2523924</td>\n",
       "      <td>2667130</td>\n",
       "    </tr>\n",
       "    <tr>\n",
Simon Clarke's avatar
Simon Clarke committed
1642
       "      <td>QLD</td>\n",
Simon Clarke's avatar
Simon Clarke committed
1643
1644
1645
1646
       "      <td>1723030</td>\n",
       "      <td>5184847</td>\n",
       "    </tr>\n",
       "    <tr>\n",
Simon Clarke's avatar
Simon Clarke committed
1647
       "      <td>NT</td>\n",
Simon Clarke's avatar
Simon Clarke committed
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
       "      <td>1334404</td>\n",
       "      <td>246500</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          area  population\n",
       "state                     \n",
       "WA     2523924     2667130\n",
       "QLD    1723030     5184847\n",
       "NT     1334404      246500"
      ]
     },
Simon Clarke's avatar
Simon Clarke committed
1663
     "execution_count": 26,
Simon Clarke's avatar
Simon Clarke committed
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "states.loc['WA':'NT','area':'population']"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
1676
    "In this case, the last argument of the slice is included.\n",
Simon Clarke's avatar
Simon Clarke committed
1677
1678
1679
1680
1681
1682
    "\n",
    "Since WA is the first index and area is the first column, we could omit the first argument of the slices and use:"
   ]
  },
  {
   "cell_type": "code",
Simon Clarke's avatar
Simon Clarke committed
1683
   "execution_count": 27,
Simon Clarke's avatar
Simon Clarke committed
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
   "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>area</th>\n",
       "      <th>population</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>state</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
Simon Clarke's avatar
Simon Clarke committed
1718
       "      <td>WA</td>\n",
Simon Clarke's avatar
Simon Clarke committed
1719
1720
1721
1722
       "      <td>2523924</td>\n",
       "      <td>2667130</td>\n",
       "    </tr>\n",
       "    <tr>\n",
Simon Clarke's avatar
Simon Clarke committed
1723
       "      <td>QLD</td>\n",
Simon Clarke's avatar
Simon Clarke committed
1724
1725
1726
1727
       "      <td>1723030</td>\n",
       "      <td>5184847</td>\n",
       "    </tr>\n",
       "    <tr>\n",
Simon Clarke's avatar
Simon Clarke committed
1728
       "      <td>NT</td>\n",
Simon Clarke's avatar
Simon Clarke committed
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
       "      <td>1334404</td>\n",
       "      <td>246500</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          area  population\n",
       "state                     \n",
       "WA     2523924     2667130\n",
       "QLD    1723030     5184847\n",
       "NT     1334404      246500"
      ]
     },
Simon Clarke's avatar
Simon Clarke committed
1744
     "execution_count": 27,
Simon Clarke's avatar
Simon Clarke committed
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "states.loc[:'NT',:'population']"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The other way to refer to location is to use `df.iloc`, which refers to index and columns by the index values. This syntax is the same as for `numpy` arrays and the last argument of a slice is not included. Hence, to refer to the same positions we want the first three rows and the first two columns, and consequently use:"
   ]
  },
  {
   "cell_type": "code",
Simon Clarke's avatar
Simon Clarke committed
1762
   "execution_count": 28,
Simon Clarke's avatar
Simon Clarke committed
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
1795
1796
   "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>area</th>\n",
       "      <th>population</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>state</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
Simon Clarke's avatar
Simon Clarke committed
1797
       "      <td>WA</td>\n",
Simon Clarke's avatar
Simon Clarke committed
1798
1799
1800
1801
       "      <td>2523924</td>\n",
       "      <td>2667130</td>\n",
       "    </tr>\n",
       "    <tr>\n",
Simon Clarke's avatar
Simon Clarke committed
1802
       "      <td>QLD</td>\n",
Simon Clarke's avatar
Simon Clarke committed
1803
1804
1805
1806
       "      <td>1723030</td>\n",
       "      <td>5184847</td>\n",
       "    </tr>\n",
       "    <tr>\n",
Simon Clarke's avatar
Simon Clarke committed
1807
       "      <td>NT</td>\n",
Simon Clarke's avatar
Simon Clarke committed
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
       "      <td>1334404</td>\n",
       "      <td>246500</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          area  population\n",
       "state                     \n",
       "WA     2523924     2667130\n",
       "QLD    1723030     5184847\n",
       "NT     1334404      246500"
      ]
     },
Simon Clarke's avatar
Simon Clarke committed
1823
     "execution_count": 28,
Simon Clarke's avatar
Simon Clarke committed
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "states.iloc[:3,:2]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
1836
    "## Melbourne Weather Data (Combining Pandas & Plotting)"
Simon Clarke's avatar
Simon Clarke committed
1837
1838
1839
1840
1841
1842
1843
1844
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We will analyse here the weather data collected by the Bureau of Meteorology at [Melbourne Airport](http://www.bom.gov.au/products/IDV60901/IDV60901.94866.shtml). The data we will analyse is a modified version of the `axf` (csv) file at the bottom of this page.\n",
    "\n",
1845
    "The data file is comma separated values (csv) file, which can be imported and stored as dataframe using `pd.read_csv()`. We will store this in the dataframe `apw`. Pandas can read many of other types of files, see [IO tools user guide](https://pandas.pydata.org/pandas-docs/stable/user_guide/io.html). For example this data also comes as JSON files, which can be read using `pd.read_json`."
Simon Clarke's avatar
Simon Clarke committed
1846
1847
1848
1849
   ]
  },
  {
   "cell_type": "code",
Simon Clarke's avatar
Simon Clarke committed
1850
   "execution_count": 29,
Simon Clarke's avatar
Simon Clarke committed
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
   "metadata": {},
   "outputs": [],
   "source": [
    "apw = pd.read_csv('IDV60901.94866.csv') # Airport weather data"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can view the file which has 166 entries and 17 columns. This last detail could also be obtained using `df.shape`."
   ]
  },
  {
   "cell_type": "code",
Simon Clarke's avatar
Simon Clarke committed
1866
   "execution_count": 30,
Simon Clarke's avatar
Simon Clarke committed
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
   "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>local_date_time[80]</th>\n",
       "      <th>local_date_time_full[80]</th>\n",
       "      <th>apparent_t</th>\n",
       "      <th>cloud[80]</th>\n",
       "      <th>cloud_base_m</th>\n",
       "      <th>cloud_type[80]</th>\n",
       "      <th>gust_kmh</th>\n",
       "      <th>air_temp</th>\n",
       "      <th>dewpt</th>\n",
       "      <th>press_msl</th>\n",
       "      <th>press_tend[80]</th>\n",
       "      <th>rain_trace[80]</th>\n",
       "      <th>rel_hum</th>\n",
       "      <th>vis_km[80]</th>\n",
       "      <th>weather[80]</th>\n",
       "      <th>wind_dir[80]</th>\n",
       "      <th>wind_spd_kmh</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
Simon Clarke's avatar
Simon Clarke committed
1911
       "      <td>0</td>\n",
Simon Clarke's avatar
Simon Clarke committed
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
       "      <td>26/03:30pm</td>\n",
       "      <td>20210326153000</td>\n",
       "      <td>15.6</td>\n",
       "      <td>Partly cloudy</td>\n",
       "      <td>1410</td>\n",
       "      <td>Stratocumulus</td>\n",
       "      <td>32</td>\n",
       "      <td>19.9</td>\n",
       "      <td>8.0</td>\n",
       "      <td>1011.2</td>\n",
       "      <td>-</td>\n",
       "      <td>0.0</td>\n",
       "      <td>46</td>\n",
       "      <td>10</td>\n",
       "      <td>-</td>\n",
       "      <td>NNW</td>\n",
       "      <td>20</td>\n",
       "    </tr>\n",
       "    <tr>\n",
Simon Clarke's avatar
Simon Clarke committed
1931
       "      <td>1</td>\n",
Simon Clarke's avatar
Simon Clarke committed
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
       "      <td>26/03:00pm</td>\n",
       "      <td>20210326150000</td>\n",
       "      <td>15.6</td>\n",
       "      <td>Mostly cloudy</td>\n",
       "      <td>1500</td>\n",
       "      <td>-</td>\n",
       "      <td>30</td>\n",
       "      <td>19.8</td>\n",
       "      <td>8.3</td>\n",
       "      <td>1011.3</td>\n",
       "      <td>R</td>\n",
       "      <td>0.0</td>\n",
       "      <td>47</td>\n",
       "      <td>30</td>\n",
       "      <td>Fine</td>\n",
       "      <td>N</td>\n",
       "      <td>20</td>\n",
       "    </tr>\n",
       "    <tr>\n",
Simon Clarke's avatar
Simon Clarke committed
1951
       "      <td>2</td>\n",
Simon Clarke's avatar
Simon Clarke committed
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
       "      <td>26/02:30pm</td>\n",
       "      <td>20210326143000</td>\n",
       "      <td>15.8</td>\n",
       "      <td>Partly cloudy</td>\n",
       "      <td>1350</td>\n",
       "      <td>Stratocumulus</td>\n",
       "      <td>28</td>\n",
       "      <td>19.9</td>\n",
       "      <td>8.5</td>\n",
       "      <td>1011.4</td>\n",
       "      <td>-</td>\n",
       "      <td>0.0</td>\n",
       "      <td>47</td>\n",
       "      <td>10</td>\n",
       "      <td>-</td>\n",
       "      <td>N</td>\n",
       "      <td>20</td>\n",
       "    </tr>\n",
       "    <tr>\n",
Simon Clarke's avatar
Simon Clarke committed
1971
       "      <td>3</td>\n",
Simon Clarke's avatar
Simon Clarke committed
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
       "      <td>26/02:00pm</td>\n",
       "      <td>20210326140000</td>\n",
       "      <td>16.4</td>\n",
       "      <td>Partly cloudy</td>\n",
       "      <td>1350</td>\n",
       "      <td>Stratocumulus</td>\n",
       "      <td>26</td>\n",
       "      <td>20.1</td>\n",
       "      <td>8.0</td>\n",
       "      <td>1012.0</td>\n",
       "      <td>-</td>\n",
       "      <td>0.0</td>\n",
       "      <td>45</td>\n",
       "      <td>10</td>\n",
       "      <td>-</td>\n",
       "      <td>NNW</td>\n",
       "      <td>17</td>\n",
       "    </tr>\n",
       "    <tr>\n",
Simon Clarke's avatar
Simon Clarke committed
1991
       "      <td>4</td>\n",
Simon Clarke's avatar
Simon Clarke committed
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
       "      <td>26/01:30pm</td>\n",
       "      <td>20210326133000</td>\n",
       "      <td>16.0</td>\n",
       "      <td>Partly cloudy</td>\n",
       "      <td>1560</td>\n",
       "      <td>-</td>\n",
       "      <td>26</td>\n",
       "      <td>19.9</td>\n",
       "      <td>8.5</td>\n",
       "      <td>1012.2</td>\n",
       "      <td>-</td>\n",
       "      <td>0.0</td>\n",
       "      <td>47</td>\n",
       "      <td>10</td>\n",
       "      <td>-</td>\n",
       "      <td>NW</td>\n",
       "      <td>19</td>\n",
       "    </tr>\n",
       "    <tr>\n",
Simon Clarke's avatar
Simon Clarke committed
2011
       "      <td>...</td>\n",
Simon Clarke's avatar
Simon Clarke committed
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
Simon Clarke's avatar
Simon Clarke committed
2031
       "      <td>161</td>\n",
Simon Clarke's avatar
Simon Clarke committed
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
       "      <td>23/06:00pm</td>\n",
       "      <td>20210323180000</td>\n",
       "      <td>17.5</td>\n",
       "      <td>Mostly cloudy</td>\n",
       "      <td>300</td>\n",
       "      <td>-</td>\n",
       "      <td>28</td>\n",
       "      <td>19.8</td>\n",
       "      <td>15.6</td>\n",
       "      <td>1001.3</td>\n",
       "      <td>F</td>\n",
       "      <td>0.0</td>\n",
       "      <td>77</td>\n",
       "      <td>30</td>\n",
       "      <td>Distant precip.</td>\n",
       "      <td>SW</td>\n",
       "      <td>22</td>\n",
       "    </tr>\n",
       "    <tr>\n",
Simon Clarke's avatar
Simon Clarke committed
2051
       "      <td>162</td>\n",
Simon Clarke's avatar
Simon Clarke committed
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
       "      <td>23/05:30pm</td>\n",
       "      <td>20210323173000</td>\n",
       "      <td>18.6</td>\n",
       "      <td>Mostly clear</td>\n",
       "      <td>390</td>\n",
       "      <td>Stratus</td>\n",
       "      <td>28</td>\n",
       "      <td>20.5</td>\n",
       "      <td>15.7</td>\n",
       "      <td>1001.4</td>\n",
       "      <td>-</td>\n",
       "      <td>0.0</td>\n",
       "      <td>74</td>\n",
       "      <td>10</td>\n",
       "      <td>-</td>\n",
       "      <td>SSW</td>\n",
       "      <td>20</td>\n",
       "    </tr>\n",
       "    <tr>\n",
Simon Clarke's avatar
Simon Clarke committed
2071
       "      <td>163</td>\n",
Simon Clarke's avatar
Simon Clarke committed
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
       "      <td>23/05:00pm</td>\n",
       "      <td>20210323170000</td>\n",
       "      <td>18.3</td>\n",
       "      <td>Mostly clear</td>\n",
       "      <td>390</td>\n",
       "      <td>Stratus</td>\n",
       "      <td>30</td>\n",
       "      <td>20.1</td>\n",
       "      <td>16.0</td>\n",
       "      <td>1001.7</td>\n",
       "      <td>-</td>\n",
       "      <td>0.0</td>\n",
       "      <td>77</td>\n",
       "      <td>10</td>\n",
       "      <td>-</td>\n",
       "      <td>SSW</td>\n",
       "      <td>20</td>\n",
       "    </tr>\n",
       "    <tr>\n",
Simon Clarke's avatar
Simon Clarke committed
2091
       "      <td>164</td>\n",
Simon Clarke's avatar
Simon Clarke committed
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
       "      <td>23/04:30pm</td>\n",
       "      <td>20210323163000</td>\n",
       "      <td>17.6</td>\n",
       "      <td>Mostly clear</td>\n",
       "      <td>450</td>\n",
       "      <td>Stratus</td>\n",
       "      <td>28</td>\n",
       "      <td>19.9</td>\n",
       "      <td>15.6</td>\n",
       "      <td>1002.0</td>\n",
       "      <td>-</td>\n",
       "      <td>0.0</td>\n",
       "      <td>76</td>\n",
       "      <td>10</td>\n",
       "      <td>-</td>\n",
       "      <td>SSW</td>\n",
       "      <td>22</td>\n",
       "    </tr>\n",
       "    <tr>\n",
Simon Clarke's avatar
Simon Clarke committed
2111
       "      <td>165</td>\n",
Simon Clarke's avatar
Simon Clarke committed
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
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
       "      <td>23/04:00pm</td>\n",
       "      <td>20210323160000</td>\n",
       "      <td>17.8</td>\n",
       "      <td>Mostly clear</td>\n",
       "      <td>480</td>\n",
       "      <td>Stratus</td>\n",
       "      <td>28</td>\n",
       "      <td>20.0</td>\n",
       "      <td>16.0</td>\n",
       "      <td>1002.3</td>\n",
       "      <td>-</td>\n",
       "      <td>0.0</td>\n",
       "      <td>78</td>\n",
       "      <td>10</td>\n",
       "      <td>-</td>\n",
       "      <td>SSW</td>\n",
       "      <td>22</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>166 rows × 17 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "    local_date_time[80]  local_date_time_full[80]  apparent_t      cloud[80]  \\\n",
       "0            26/03:30pm            20210326153000        15.6  Partly cloudy   \n",
       "1            26/03:00pm            20210326150000        15.6  Mostly cloudy   \n",
       "2            26/02:30pm            20210326143000        15.8  Partly cloudy   \n",
       "3            26/02:00pm            20210326140000        16.4  Partly cloudy   \n",
       "4            26/01:30pm            20210326133000        16.0  Partly cloudy   \n",
       "..                  ...                       ...         ...            ...   \n",
       "161          23/06:00pm            20210323180000        17.5  Mostly cloudy   \n",
       "162          23/05:30pm            20210323173000        18.6   Mostly clear   \n",
       "163          23/05:00pm            20210323170000        18.3   Mostly clear   \n",
       "164          23/04:30pm            20210323163000        17.6   Mostly clear   \n",
       "165          23/04:00pm            20210323160000        17.8   Mostly clear   \n",
       "\n",
       "     cloud_base_m cloud_type[80]  gust_kmh  air_temp  dewpt  press_msl  \\\n",
       "0            1410  Stratocumulus        32      19.9    8.0     1011.2   \n",
       "1            1500              -        30      19.8    8.3     1011.3   \n",
       "2            1350  Stratocumulus        28      19.9    8.5     1011.4   \n",
       "3            1350  Stratocumulus        26      20.1    8.0     1012.0   \n",
       "4            1560              -        26      19.9    8.5     1012.2   \n",
       "..            ...            ...       ...       ...    ...        ...   \n",
       "161           300              -        28      19.8   15.6     1001.3   \n",
       "162           390        Stratus        28      20.5   15.7     1001.4   \n",
       "163           390        Stratus        30      20.1   16.0     1001.7   \n",
       "164           450        Stratus        28      19.9   15.6     1002.0   \n",
       "165           480        Stratus        28      20.0   16.0     1002.3   \n",
       "\n",
       "    press_tend[80]  rain_trace[80]  rel_hum  vis_km[80]      weather[80]  \\\n",
       "0                -             0.0       46          10                -   \n",
       "1                R             0.0       47          30             Fine   \n",
       "2                -             0.0       47          10                -   \n",
       "3                -             0.0       45          10                -   \n",
       "4                -             0.0       47          10                -   \n",
       "..             ...             ...      ...         ...              ...   \n",
       "161              F             0.0       77          30  Distant precip.   \n",
       "162              -             0.0       74          10                -   \n",
       "163              -             0.0       77          10                -   \n",
       "164              -             0.0       76          10                -   \n",
       "165              -             0.0       78          10                -   \n",
       "\n",
       "    wind_dir[80]  wind_spd_kmh  \n",
       "0            NNW            20  \n",
       "1              N            20  \n",
       "2              N            20  \n",
       "3            NNW            17  \n",
       "4             NW            19  \n",
       "..           ...           ...  \n",
       "161           SW            22  \n",
       "162          SSW            20  \n",
       "163          SSW            20  \n",
       "164          SSW            22  \n",
       "165          SSW            22  \n",
       "\n",
       "[166 rows x 17 columns]"
      ]
     },
Simon Clarke's avatar
Simon Clarke committed
2191
     "execution_count": 30,
Simon Clarke's avatar
Simon Clarke committed
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "apw"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can get a description of the data types. Pandas infers these when reading in the file."
   ]
  },
  {
   "cell_type": "code",
Simon Clarke's avatar
Simon Clarke committed
2209
   "execution_count": 31,
Simon Clarke's avatar
Simon Clarke committed
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "local_date_time[80]          object\n",
       "local_date_time_full[80]      int64\n",
       "apparent_t                  float64\n",
       "cloud[80]                    object\n",
       "cloud_base_m                  int64\n",
       "cloud_type[80]               object\n",
       "gust_kmh                      int64\n",
       "air_temp                    float64\n",
       "dewpt                       float64\n",
       "press_msl                   float64\n",
       "press_tend[80]               object\n",
       "rain_trace[80]              float64\n",
       "rel_hum                       int64\n",
       "vis_km[80]                    int64\n",
       "weather[80]                  object\n",
       "wind_dir[80]                 object\n",
       "wind_spd_kmh                  int64\n",
       "dtype: object"
      ]
     },
Simon Clarke's avatar
Simon Clarke committed
2235
     "execution_count": 31,
Simon Clarke's avatar
Simon Clarke committed
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "apw.dtypes"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
2248
2249
2250
2251
2252
2253
2254
2255
    "For these data, let's say we want to understand the following:\n",
    "<ul>\n",
    "<li>cloud base height over time</li>\n",
    "<li>the relationship between air temperature, dewpoint temperature, and rainfall</li>\n",
    "<li>the extent to which there are linear relationships between features in our data</li>\n",
    "</ul> \n",
    "\n",
    "For our analysis, we won't use all of the features in the data (columns), so we can drop the columns that are not required using `df.drop()`. "
Simon Clarke's avatar
Simon Clarke committed
2256
2257
2258
2259
   ]
  },
  {
   "cell_type": "code",
Simon Clarke's avatar
Simon Clarke committed
2260
   "execution_count": 32,
Simon Clarke's avatar
Simon Clarke committed
2261
2262
2263
2264
2265
2266
2267
2268
2269