Skip to content

Commit b4874c0

Browse files
authored
A more streamlined and comprehensive tutorial for the repo (#225)
1 parent 490b3a7 commit b4874c0

41 files changed

Lines changed: 24442 additions & 0 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"id": "d472b698-5268-4b39-a61a-a8da2e9654e2",
6+
"metadata": {},
7+
"source": [
8+
"## ```tibble()``` function from DataR library creates a DataFrame in a more user-friendly way compared to the traditional pandas DataFrame constructor.\n",
9+
"\n",
10+
"--------------------------------------------------------------------------------\n",
11+
"\n",
12+
"1. Create dataframe using ```datar.tibble()```\n",
13+
"\n",
14+
"2. Convert Pandas DataFrame to DataR Tibble"
15+
]
16+
},
17+
{
18+
"cell_type": "code",
19+
"execution_count": 1,
20+
"id": "a3c93850-ae78-4cfd-95c4-8f6c39c114af",
21+
"metadata": {},
22+
"outputs": [],
23+
"source": [
24+
"import datar.all as dr\n",
25+
"import pandas as pd"
26+
]
27+
},
28+
{
29+
"cell_type": "markdown",
30+
"id": "e7a93db8-e292-40c0-bb06-e9e0d2420a42",
31+
"metadata": {},
32+
"source": [
33+
"# <span style=\"color:#1E90FF\">1. Create dataframe using datar.tibble()</span>"
34+
]
35+
},
36+
{
37+
"cell_type": "code",
38+
"execution_count": 5,
39+
"id": "cff07e1a-af2f-4414-9e94-e835faef677d",
40+
"metadata": {},
41+
"outputs": [
42+
{
43+
"name": "stdout",
44+
"output_type": "stream",
45+
"text": [
46+
"<class 'datar_pandas.tibble.Tibble'>\n"
47+
]
48+
}
49+
],
50+
"source": [
51+
"tb = dr.tibble(\n",
52+
" x = [1, 2, 3],\n",
53+
" y = [\"a\", \"b\", \"c\"],\n",
54+
" z = [True, False, True]\n",
55+
")\n",
56+
"\n",
57+
"print(type(tb))"
58+
]
59+
},
60+
{
61+
"cell_type": "code",
62+
"execution_count": 6,
63+
"id": "8f88479d-9fca-459d-81d9-5b43bb50fa5c",
64+
"metadata": {},
65+
"outputs": [
66+
{
67+
"name": "stdout",
68+
"output_type": "stream",
69+
"text": [
70+
" x y z\n",
71+
" <int64> <str> <bool>\n",
72+
"0 1 a True\n",
73+
"1 2 b False\n",
74+
"2 3 c True\n"
75+
]
76+
}
77+
],
78+
"source": [
79+
"print(tb)"
80+
]
81+
},
82+
{
83+
"cell_type": "markdown",
84+
"id": "ded89271-7fcf-4968-a157-0e506e506e35",
85+
"metadata": {},
86+
"source": [
87+
"# <span style=\"color:#1E90FF\">2. Convert Pandas DataFrame to DataR Tibble</span>"
88+
]
89+
},
90+
{
91+
"cell_type": "code",
92+
"execution_count": 7,
93+
"id": "f63b85d4-1043-459d-8a70-bc25d7d8f54e",
94+
"metadata": {},
95+
"outputs": [
96+
{
97+
"name": "stdout",
98+
"output_type": "stream",
99+
"text": [
100+
" A B C\n",
101+
" <int64> <str> <bool>\n",
102+
"0 1 x True\n",
103+
"1 2 y False\n",
104+
"2 3 z True\n"
105+
]
106+
}
107+
],
108+
"source": [
109+
"df = pd.DataFrame({\n",
110+
" 'A': [1, 2, 3],\n",
111+
" 'B': ['x', 'y', 'z'],\n",
112+
" 'C': [True, False, True]\n",
113+
"})\n",
114+
"\n",
115+
"# convert from Pan\n",
116+
"tb_from_df = dr.tibble(df)\n",
117+
"\n",
118+
"print(tb_from_df)"
119+
]
120+
}
121+
],
122+
"metadata": {
123+
"kernelspec": {
124+
"display_name": "Python 3 (ipykernel)",
125+
"language": "python",
126+
"name": "python3"
127+
},
128+
"language_info": {
129+
"codemirror_mode": {
130+
"name": "ipython",
131+
"version": 3
132+
},
133+
"file_extension": ".py",
134+
"mimetype": "text/x-python",
135+
"name": "python",
136+
"nbconvert_exporter": "python",
137+
"pygments_lexer": "ipython3",
138+
"version": "3.13.13"
139+
}
140+
},
141+
"nbformat": 4,
142+
"nbformat_minor": 5
143+
}
Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
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

Comments
 (0)