|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "id": "6b575a2c-ad21-4fd3-a0ff-d1668d2e6c62", |
| 6 | + "metadata": {}, |
| 7 | + "source": [ |
| 8 | + "## To read dataframe, just use ```pandas.read.....()``` functions, then convert to datar tibble if needed." |
| 9 | + ] |
| 10 | + }, |
| 11 | + { |
| 12 | + "cell_type": "code", |
| 13 | + "execution_count": 13, |
| 14 | + "id": "7f8dc3a2-8046-4af8-8b19-f449c1f9cb0b", |
| 15 | + "metadata": {}, |
| 16 | + "outputs": [], |
| 17 | + "source": [ |
| 18 | + "import datar.all as dr\n", |
| 19 | + "import pandas as pd\n", |
| 20 | + "from pathlib import Path\n", |
| 21 | + "\n", |
| 22 | + "pd.set_option(\"display.width\", 200)\n", |
| 23 | + "\n", |
| 24 | + "# Get the path object pointing to the ``notebooks`` directory that contains *.csv files \n", |
| 25 | + "data_dir = next(Path(\"/home\").rglob(\"*/notebooks/*.csv\")).parent" |
| 26 | + ] |
| 27 | + }, |
| 28 | + { |
| 29 | + "cell_type": "markdown", |
| 30 | + "id": "baaccca0-7b1b-443d-a900-466082b7524b", |
| 31 | + "metadata": {}, |
| 32 | + "source": [ |
| 33 | + "# <span style=\"color:#1E90FF\">1. Read from CSV file</span>" |
| 34 | + ] |
| 35 | + }, |
| 36 | + { |
| 37 | + "cell_type": "code", |
| 38 | + "execution_count": 10, |
| 39 | + "id": "2687b49d-f2a7-42ff-b2f2-6621dce892ea", |
| 40 | + "metadata": {}, |
| 41 | + "outputs": [ |
| 42 | + { |
| 43 | + "name": "stdout", |
| 44 | + "output_type": "stream", |
| 45 | + "text": [ |
| 46 | + " city country date.utc location parameter value unit\n", |
| 47 | + " <str> <str> <str> <str> <str> <float64> <str>\n", |
| 48 | + "0 Paris FR 2019-06-21 00:00:00+00:00 FR04014 no2 20.0 µg/m³\n", |
| 49 | + "1 Paris FR 2019-06-20 23:00:00+00:00 FR04014 no2 21.8 µg/m³\n", |
| 50 | + "2 Paris FR 2019-06-20 22:00:00+00:00 FR04014 no2 26.5 µg/m³\n", |
| 51 | + "3 Paris FR 2019-06-20 21:00:00+00:00 FR04014 no2 24.9 µg/m³\n", |
| 52 | + "4 Paris FR 2019-06-20 20:00:00+00:00 FR04014 no2 21.4 µg/m³\n" |
| 53 | + ] |
| 54 | + } |
| 55 | + ], |
| 56 | + "source": [ |
| 57 | + "tb_csv = dr.tibble(pd.read_csv(data_dir/'air_quality_no2_long.csv'))\n", |
| 58 | + "\n", |
| 59 | + "print(tb_csv.head())" |
| 60 | + ] |
| 61 | + }, |
| 62 | + { |
| 63 | + "cell_type": "markdown", |
| 64 | + "id": "38da252b-759a-43a5-9366-79c9980272a3", |
| 65 | + "metadata": {}, |
| 66 | + "source": [ |
| 67 | + "# <span style=\"color:#1E90FF\">2. Read from Excel file</span>" |
| 68 | + ] |
| 69 | + }, |
| 70 | + { |
| 71 | + "cell_type": "code", |
| 72 | + "execution_count": 11, |
| 73 | + "id": "38269bf3-1beb-4456-a3b2-8b52629d06de", |
| 74 | + "metadata": {}, |
| 75 | + "outputs": [ |
| 76 | + { |
| 77 | + "name": "stdout", |
| 78 | + "output_type": "stream", |
| 79 | + "text": [ |
| 80 | + " id name salary start_date dept\n", |
| 81 | + " <object> <str> <float64> <datetime64[us]> <str>\n", |
| 82 | + "0 1 Rick 623.30 2012-01-01 IT\n", |
| 83 | + "1 2 Dan 515.20 2013-09-23 Operations\n", |
| 84 | + "2 3 Michelle 611.00 2014-11-15 IT\n", |
| 85 | + "3 4 Ryan 729.00 2014-05-11 HR\n", |
| 86 | + "4 Gary 843.25 2015-03-27 Finance\n", |
| 87 | + "5 6 Nina 578.00 2013-05-21 IT\n", |
| 88 | + "6 7 Simon 632.80 2013-07-30 Operations\n", |
| 89 | + "7 8 Guru 722.50 2014-06-17 Finance\n" |
| 90 | + ] |
| 91 | + } |
| 92 | + ], |
| 93 | + "source": [ |
| 94 | + "tb_excel = dr.tibble(pd.read_excel(data_dir/\"emp_sheetname.xlsx\", sheet_name='emp'))\n", |
| 95 | + "\n", |
| 96 | + "print(tb_excel)" |
| 97 | + ] |
| 98 | + }, |
| 99 | + { |
| 100 | + "cell_type": "markdown", |
| 101 | + "id": "9751df45-8f06-4198-817f-8349b15e24e1", |
| 102 | + "metadata": {}, |
| 103 | + "source": [ |
| 104 | + "# <span style=\"color:#1E90FF\">3 .Read with more options</span>" |
| 105 | + ] |
| 106 | + }, |
| 107 | + { |
| 108 | + "cell_type": "code", |
| 109 | + "execution_count": 14, |
| 110 | + "id": "665be6eb-2499-4db1-a67f-317c2695d9f9", |
| 111 | + "metadata": {}, |
| 112 | + "outputs": [ |
| 113 | + { |
| 114 | + "name": "stdout", |
| 115 | + "output_type": "stream", |
| 116 | + "text": [ |
| 117 | + " Name Type_1 Type_2 Total HP Attack Defense Sp_Atk Sp_Def Speed Generation Legendary\n", |
| 118 | + " <str> <category> <category> <int64> <int64> <int64> <int64> <int64> <int64> <int64> <category> <bool>\n", |
| 119 | + "0 Bulbasaur Grass Poison 318 45 49 49 65 65 45 1 False\n", |
| 120 | + "1 Ivysaur Grass Poison 405 60 62 63 80 80 60 1 False\n", |
| 121 | + "2 Venusaur Grass Poison 525 80 82 83 100 100 80 1 False\n", |
| 122 | + "3 VenusaurMega Venusaur Grass Poison 625 80 100 123 122 120 80 1 False\n", |
| 123 | + "4 Charmander Fire NaN 309 39 52 43 60 50 65 1 False\n" |
| 124 | + ] |
| 125 | + } |
| 126 | + ], |
| 127 | + "source": [ |
| 128 | + "tb_pokemon = dr.tibble(\n", |
| 129 | + " pd.read_csv(\n", |
| 130 | + " filepath_or_buffer=data_dir/\"pokemon.csv\",\n", |
| 131 | + " dtype={\n", |
| 132 | + " \"Type 1\": \"category\",\n", |
| 133 | + " \"Type 2\": \"category\",\n", |
| 134 | + " \"Generation\": \"category\",\n", |
| 135 | + " \"Legendary\": \"bool\"\n", |
| 136 | + " }\n", |
| 137 | + " )\n", |
| 138 | + " .drop(columns=[\"#\"])\n", |
| 139 | + " .pipe(lambda f: f.set_axis(f.columns.str.strip().str.replace(r\"\\s+\", \"_\", regex=True).str.replace(\".\", \"\"), axis=1))\n", |
| 140 | + " .assign(Generation = pd.col('Generation').cat.as_ordered())\n", |
| 141 | + ")\n", |
| 142 | + "\n", |
| 143 | + "print(\n", |
| 144 | + " tb_pokemon\n", |
| 145 | + " >> dr.slice_head(n=5)\n", |
| 146 | + ")" |
| 147 | + ] |
| 148 | + } |
| 149 | + ], |
| 150 | + "metadata": { |
| 151 | + "kernelspec": { |
| 152 | + "display_name": "Python 3 (ipykernel)", |
| 153 | + "language": "python", |
| 154 | + "name": "python3" |
| 155 | + }, |
| 156 | + "language_info": { |
| 157 | + "codemirror_mode": { |
| 158 | + "name": "ipython", |
| 159 | + "version": 3 |
| 160 | + }, |
| 161 | + "file_extension": ".py", |
| 162 | + "mimetype": "text/x-python", |
| 163 | + "name": "python", |
| 164 | + "nbconvert_exporter": "python", |
| 165 | + "pygments_lexer": "ipython3", |
| 166 | + "version": "3.13.13" |
| 167 | + } |
| 168 | + }, |
| 169 | + "nbformat": 4, |
| 170 | + "nbformat_minor": 5 |
| 171 | +} |
0 commit comments