<jupyter_start><jupyter_text>DatatableTon    💯 datatable exercises                                                             This is Set 3: Data Selection (Exercises 21-30) of DatatableTon: 💯 datatable exercises        You can find all the exercises and solutions on GitHub **Prerequisites*** The `datatable` package should be upgraded to the latest version (or v1.0.0+).* The sample dataset [datatableton_sample.csv](https://github.com/vopani/datatableton/blob/main/data/datatableton_sample.csv) will be used for the exercises.<jupyter_code>!python3 -m pip install -U pip
!python3 -m pip install -U datatable
!wget https://raw.githubusercontent.com/vopani/datatableton/main/data/datatableton_sample.csv
import datatable as dt

data = dt.fread('datatableton_sample.csv')
data<jupyter_output><empty_output><jupyter_text>**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`**<jupyter_code>data_upq = data[:, ['user', 'product', 'quantity']]
data_upq
data_u = data_upq['user']
data_u<jupyter_output><empty_output><jupyter_text>**Exercise 22: Select columns 2nd to 4th using indices from `data` and assign it to `data_c234`**<jupyter_code>data_c234 = data[:, 1:4]
data_c234<jupyter_output><empty_output><jupyter_text>**Exercise 23: Select all string columns from `data` and assign it to `data_str`**<jupyter_code>data_str = data[:, str]
data_str<jupyter_output><empty_output><jupyter_text>**Exercise 24: Select rows 4th and 1st from `data` and assign it to `data_r41`**<jupyter_code>data_r41 = data[[3, 0], :]
data_r41<jupyter_output><empty_output><jupyter_text>**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`**<jupyter_code>value_1 = data[3, 1]
value_1
value_2 = data[2, 'category']
value_2<jupyter_output><empty_output><jupyter_text>**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`**<jupyter_code>data_fruit = data[dt.f.category == 'Fruit', :]
data_fruit
data_multi_banana = data_fruit[(dt.f.product == 'Banana') & (dt.f.quantity > 1), :]
data_multi_banana<jupyter_output><empty_output><jupyter_text>**Exercise 27: Select columns from `data` that have no missing values and assign it to `data_nomissing_cols`**<jupyter_code>data_nomissing_cols = data[:, [column.countna1() == 0 for column in data]]
data_nomissing_cols<jupyter_output><empty_output><jupyter_text>**Exercise 28: Select rows from `data` that have no missing values and assign it to `data_nomissing_rows`**<jupyter_code>data_nomissing_rows = data[dt.rowcount(dt.f[:]) == data.ncols, :]
data_nomissing_rows<jupyter_output><empty_output><jupyter_text>**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`**<jupyter_code>data_unique = data[:1, :, dt.by(*data.names)]
data_unique
product_unique = dt.unique(data_unique['product'])
product_unique<jupyter_output><empty_output><jupyter_text>**Exercise 30: Select first row of each user from `data` and assign it to `data_first`**<jupyter_code>data_first = data[:1, :, dt.by(dt.f.user)]
data_first<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12079378774805867, 'fraction_numerical': 0.020419902214552775, 'mean_word_length': 6.415778251599147, 'pattern_counts': {'":': 0, '<': 45, '<?xml version=': 0, '>': 46, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '11', 'dir': 'jupyter-structured-clean-dedup', 'id': '413941', 'license': 'Apache-2.0', 'n_tokens_mistral': 1263, 'n_tokens_neox': 1207, 'n_words': 323, 'path': 'notebooks/03_data_selection_solutions.ipynb', 'repo_name': 'vopani/datatableton', 'size': '3477'}