Prerequisites
datatable
package should be upgraded to the latest version (or v1.0.0+).!python3 -m pip install -U pip
!python3 -m pip install -U datatable
!wget https://raw.githubusercontent.com/vopani/datatableton/main/data/datatableton_sample.csv
Requirement already satisfied: pip in /opt/conda/lib/python3.7/site-packages (21.1.2) Collecting pip Downloading pip-21.1.3-py3-none-any.whl (1.5 MB) |████████████████████████████████| 1.5 MB 1.1 MB/s Installing collected packages: pip Attempting uninstall: pip Found existing installation: pip 21.1.2 Uninstalling pip-21.1.2: Successfully uninstalled pip-21.1.2 Successfully installed pip-21.1.3 WARNING: Running pip as root will break packages and permissions. You should install packages reliably by using venv: https://pip.pypa.io/warnings/venv Requirement already satisfied: datatable in /opt/conda/lib/python3.7/site-packages (1.0.0) WARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv --2021-07-13 06:22:16-- https://raw.githubusercontent.com/vopani/datatableton/main/data/datatableton_sample.csv Resolving raw.githubusercontent.com (raw.githubusercontent.com)... 185.199.108.133, 185.199.111.133, 185.199.109.133, ... Connecting to raw.githubusercontent.com (raw.githubusercontent.com)|185.199.108.133|:443... connected. HTTP request sent, awaiting response... 200 OK Length: 940 [text/plain] Saving to: ‘datatableton_sample.csv’ datatableton_sample 100%[===================>] 940 --.-KB/s in 0s 2021-07-13 06:22:16 (60.9 MB/s) - ‘datatableton_sample.csv’ saved [940/940]
import datatable as dt
data = dt.fread('datatableton_sample.csv')
data
timestamp | user | age | product | category | price | quantity | |
---|---|---|---|---|---|---|---|
▪▪▪▪▪▪▪▪ | ▪▪▪▪ | ▪▪▪▪ | ▪▪▪▪ | ▪▪▪▪ | ▪▪▪▪▪▪▪▪ | ▪▪▪▪ | |
0 | 2017-01-01T13:22:41 | U1 | 21 | Eggs | Dairy | 2.3 | 2 |
1 | 2017-05-22T09:54:21 | U2 | 41 | Bread | NA | 0.6 | 2 |
2 | 2018-11-09T15:00:01 | U3 | NA | Banana | Fruit | 0.7 | 1 |
3 | 2018-12-24T23:03:33 | U1 | 21 | Water | Drink | 0.2 | 1 |
4 | 2019-03-03T06:21:58 | U4 | 19 | Eggs | Dairy | 2.3 | 2 |
5 | 2019-06-17T16:13:39 | U5 | 25 | Grapes | Fruit | 1.5 | 1 |
6 | 2019-07-28T21:03:11 | U5 | 25 | Bread | NA | 0.6 | 1 |
7 | 2019-12-05T04:15:42 | U1 | 21 | Banana | Fruit | 0.7 | 2 |
8 | 2020-02-02T03:45:34 | U3 | NA | Banana | Fruit | 0.7 | 1 |
9 | 2020-03-05T07:09:12 | U4 | 19 | Grapes | Fruit | 1.5 | 1 |
10 | 2020-03-22T19:29:38 | U1 | 21 | Water | Drink | 0.2 | 1 |
11 | 2020-03-30T09:44:30 | U4 | 19 | Water | Drink | 0.2 | 1 |
12 | 2020-04-01T13:21:41 | U1 | 21 | Banana | Fruit | 0.7 | 2 |
13 | 2020-07-08T11:45:25 | U2 | 41 | Grapes | Fruit | 1.5 | 1 |
14 | 2020-11-19T18:51:22 | U5 | 25 | Water | Drink | 0.2 | 1 |
15 | 2020-12-03T16:23:48 | U3 | NA | Banana | Fruit | 0.5 | 3 |
16 | 2021-02-03T01:14:40 | U5 | 25 | Eggs | Dairy | 2.1 | 4 |
17 | 2021-05-26T22:42:15 | U3 | NA | Bread | NA | 0.6 | 1 |
18 | 2021-06-14T15:49:28 | U4 | 19 | Eggs | Dairy | 2.3 | 2 |
19 | 2021-07-01T04:37:31 | U4 | 19 | Water | Drink | 0.3 | 1 |
Exercise 21: Select the columns user
, product
and quantity
from data
and assign it to data_upq
and select the user
column from data_upq
and assign it to data_u
Exercise 22: Select columns 2nd to 4th using indices from data
and assign it to data_c234
Exercise 23: Select all string columns from data
and assign it to data_str
Exercise 24: Select rows 4th and 1st from data
and assign it to data_r41
Exercise 25: Select the element in the 4th row and 2nd column in data
and assign it to value_1
and select the 3rd element of category
column in data
and assign it to value_2
Exercise 26: Select rows from data
where category
is Fruit
and assign it to data_fruit
and select rows from data_fruit
where product
is Banana
and quantity
is more than 1 and assign it to data_multi_banana
Exercise 27: Select columns from data
that have no missing values and assign it to data_nomissing_cols
Exercise 28: Select rows from data
that have no missing values and assign it to data_nomissing_rows
Exercise 29: Select unique rows from data
and assign it to data_unique
and select unique product
from data_unique
and assign it to product_unique
Exercise 30: Select first row of each user from data
and assign it to data_first