Code
Sys.setenv(RETICULATE_PYTHON = "/Library/Frameworks/Python.framework/Versions/3.11/bin/python3.11")
library(reticulate)
use_python("/Library/Frameworks/Python.framework/Versions/3.11/bin/python3.11")
Tony Duan
Using python 3.11 for siuba pacakege
if (file.exists("my-db.duckdb")) {file.remove("my-db.duckdb")
}
#con <- dbConnect(duckdb(), dbdir = "my-db.duckdb", read_only = TRUE)
con <- dbConnect(duckdb(), dbdir = "my-db.duckdb", read_only = FALSE)
dbWriteTable(con, "iris_table", iris,overwrite = TRUE)
dbWriteTable(con, "mtcars_table", mtcars,overwrite = TRUE)
There are table iris_table
and mtcars_table
in duckdb my-db.duckdb
There are table iris
and mtcars
R environment
There are table iris
and mtcars
Python environment
database | schema | name | column_names | column_types | temporary |
---|---|---|---|---|---|
my-db | main | iris_table | Sepal.Length, Sepal.Width , Petal.Length, Petal.Width , Species | DOUBLE , DOUBLE , DOUBLE , DOUBLE , ENUM(‘setosa’, ‘versicolor’, ‘virginica’) | FALSE |
my-db | main | mtcars_table | model_name, mpg , cyl , disp , hp , drat , wt , qsec , vs , am , gear , carb | VARCHAR, DOUBLE , DOUBLE , DOUBLE , DOUBLE , DOUBLE , DOUBLE , DOUBLE , DOUBLE , DOUBLE , DOUBLE , DOUBLE | FALSE |
column_name | column_type | null | key | default | extra |
---|---|---|---|---|---|
model_name | VARCHAR | YES | NA | NA | NA |
mpg | DOUBLE | YES | NA | NA | NA |
cyl | DOUBLE | YES | NA | NA | NA |
disp | DOUBLE | YES | NA | NA | NA |
hp | DOUBLE | YES | NA | NA | NA |
drat | DOUBLE | YES | NA | NA | NA |
wt | DOUBLE | YES | NA | NA | NA |
qsec | DOUBLE | YES | NA | NA | NA |
vs | DOUBLE | YES | NA | NA | NA |
am | DOUBLE | YES | NA | NA | NA |
Name | mtcars |
Number of rows | 10 |
Number of columns | 12 |
_______________________ | |
Column type frequency: | |
character | 1 |
numeric | 11 |
________________________ | |
Group variables | None |
Variable type: character
skim_variable | n_missing | complete_rate | min | max | empty | n_unique | whitespace |
---|---|---|---|---|---|---|---|
model_name | 0 | 1 | 7 | 17 | 0 | 10 | 0 |
Variable type: numeric
skim_variable | n_missing | complete_rate | mean | sd | p0 | p25 | p50 | p75 | p100 | hist |
---|---|---|---|---|---|---|---|---|---|---|
mpg | 0 | 1 | 20.37 | 2.91 | 14.30 | 18.82 | 21.00 | 22.45 | 24.40 | ▂▂▅▇▇ |
cyl | 0 | 1 | 5.80 | 1.48 | 4.00 | 4.50 | 6.00 | 6.00 | 8.00 | ▅▁▇▁▃ |
disp | 0 | 1 | 208.61 | 90.37 | 108.00 | 150.02 | 163.80 | 249.75 | 360.00 | ▇▇▅▁▅ |
hp | 0 | 1 | 122.80 | 51.45 | 62.00 | 97.50 | 110.00 | 119.75 | 245.00 | ▅▇▁▂▂ |
drat | 0 | 1 | 3.54 | 0.44 | 2.76 | 3.16 | 3.77 | 3.90 | 3.92 | ▁▃▁▁▇ |
wt | 0 | 1 | 3.13 | 0.41 | 2.32 | 2.94 | 3.20 | 3.44 | 3.57 | ▂▂▂▆▇ |
qsec | 0 | 1 | 18.58 | 2.14 | 15.84 | 17.02 | 18.45 | 19.86 | 22.90 | ▇▃▃▂▂ |
vs | 0 | 1 | 0.60 | 0.52 | 0.00 | 0.00 | 1.00 | 1.00 | 1.00 | ▅▁▁▁▇ |
am | 0 | 1 | 0.30 | 0.48 | 0.00 | 0.00 | 0.00 | 0.75 | 1.00 | ▇▁▁▁▃ |
gear | 0 | 1 | 3.60 | 0.52 | 3.00 | 3.00 | 4.00 | 4.00 | 4.00 | ▅▁▁▁▇ |
carb | 0 | 1 | 2.50 | 1.35 | 1.00 | 1.25 | 2.00 | 4.00 | 4.00 | ▆▆▁▁▇ |
model_name mpg cyl ... am gear carb
count 10 10.000000 10.00000 ... 10.000000 10.000000 10.000000
unique 10 NaN NaN ... NaN NaN NaN
top Mazda RX4 NaN NaN ... NaN NaN NaN
freq 1 NaN NaN ... NaN NaN NaN
mean NaN 20.370000 5.80000 ... 0.300000 3.600000 2.500000
std NaN 2.907099 1.47573 ... 0.483046 0.516398 1.354006
min NaN 14.300000 4.00000 ... 0.000000 3.000000 1.000000
25% NaN 18.825000 4.50000 ... 0.000000 3.000000 1.250000
50% NaN 21.000000 6.00000 ... 0.000000 4.000000 2.000000
75% NaN 22.450000 6.00000 ... 0.750000 4.000000 4.000000
max NaN 24.400000 8.00000 ... 1.000000 4.000000 4.000000
[11 rows x 12 columns]
cid | name | type | notnull | dflt_value | pk |
---|---|---|---|---|---|
0 | model_name | VARCHAR | FALSE | NA | FALSE |
1 | mpg | DOUBLE | FALSE | NA | FALSE |
2 | cyl | DOUBLE | FALSE | NA | FALSE |
3 | disp | DOUBLE | FALSE | NA | FALSE |
4 | hp | DOUBLE | FALSE | NA | FALSE |
5 | drat | DOUBLE | FALSE | NA | FALSE |
6 | wt | DOUBLE | FALSE | NA | FALSE |
7 | qsec | DOUBLE | FALSE | NA | FALSE |
8 | vs | DOUBLE | FALSE | NA | FALSE |
9 | am | DOUBLE | FALSE | NA | FALSE |
model mpg cyl disp ... vs am gear carb
Mazda RX4 Mazda RX4 21.0 6.0 160.0 ... 0.0 1.0 4.0 4.0
Mazda RX4 Wag Mazda RX4 Wag 21.0 6.0 160.0 ... 0.0 1.0 4.0 4.0
Datsun 710 Datsun 710 22.8 4.0 108.0 ... 1.0 1.0 4.0 1.0
Hornet 4 Drive Hornet 4 Drive 21.4 6.0 258.0 ... 1.0 0.0 3.0 1.0
Hornet Sportabout Hornet Sportabout 18.7 8.0 360.0 ... 0.0 0.0 3.0 2.0
Valiant Valiant 18.1 6.0 225.0 ... 1.0 0.0 3.0 1.0
Duster 360 Duster 360 14.3 8.0 360.0 ... 0.0 0.0 3.0 4.0
Merc 240D Merc 240D 24.4 4.0 146.7 ... 1.0 0.0 4.0 2.0
Merc 230 Merc 230 22.8 4.0 140.8 ... 1.0 0.0 4.0 2.0
Merc 280 Merc 280 19.2 6.0 167.6 ... 1.0 0.0 4.0 4.0
[10 rows x 12 columns]
model mpg cyl
Mazda RX4 Mazda RX4 21.0 6.0
Mazda RX4 Wag Mazda RX4 Wag 21.0 6.0
Datsun 710 Datsun 710 22.8 4.0
Hornet 4 Drive Hornet 4 Drive 21.4 6.0
Hornet Sportabout Hornet Sportabout 18.7 8.0
Valiant Valiant 18.1 6.0
all distinct record of mpg and cyl
row_number |
---|
10 |
model_name | mpg | cyl | disp | hp | drat | wt | qsec | vs | am | gear | carb | new_mpg |
---|---|---|---|---|---|---|---|---|---|---|---|---|
Mazda RX4 | 21.0 | 6 | 160.0 | 110 | 3.90 | 2.620 | 16.46 | 0 | 1 | 4 | 4 | 22.0 |
Mazda RX4 Wag | 21.0 | 6 | 160.0 | 110 | 3.90 | 2.875 | 17.02 | 0 | 1 | 4 | 4 | 22.0 |
Datsun 710 | 22.8 | 4 | 108.0 | 93 | 3.85 | 2.320 | 18.61 | 1 | 1 | 4 | 1 | 23.8 |
Hornet 4 Drive | 21.4 | 6 | 258.0 | 110 | 3.08 | 3.215 | 19.44 | 1 | 0 | 3 | 1 | 22.4 |
Hornet Sportabout | 18.7 | 8 | 360.0 | 175 | 3.15 | 3.440 | 17.02 | 0 | 0 | 3 | 2 | 19.7 |
Valiant | 18.1 | 6 | 225.0 | 105 | 2.76 | 3.460 | 20.22 | 1 | 0 | 3 | 1 | 19.1 |
Duster 360 | 14.3 | 8 | 360.0 | 245 | 3.21 | 3.570 | 15.84 | 0 | 0 | 3 | 4 | 15.3 |
Merc 240D | 24.4 | 4 | 146.7 | 62 | 3.69 | 3.190 | 20.00 | 1 | 0 | 4 | 2 | 25.4 |
Merc 230 | 22.8 | 4 | 140.8 | 95 | 3.92 | 3.150 | 22.90 | 1 | 0 | 4 | 2 | 23.8 |
Merc 280 | 19.2 | 6 | 167.6 | 123 | 3.92 | 3.440 | 18.30 | 1 | 0 | 4 | 4 | 20.2 |
model_name mpg cyl disp hp drat wt qsec vs am
Mazda RX4 Mazda RX4 21.0 6 160.0 110 3.90 2.620 16.46 0 1
Mazda RX4 Wag Mazda RX4 Wag 21.0 6 160.0 110 3.90 2.875 17.02 0 1
Datsun 710 Datsun 710 22.8 4 108.0 93 3.85 2.320 18.61 1 1
Hornet 4 Drive Hornet 4 Drive 21.4 6 258.0 110 3.08 3.215 19.44 1 0
Hornet Sportabout Hornet Sportabout 18.7 8 360.0 175 3.15 3.440 17.02 0 0
Valiant Valiant 18.1 6 225.0 105 2.76 3.460 20.22 1 0
Duster 360 Duster 360 14.3 8 360.0 245 3.21 3.570 15.84 0 0
Merc 240D Merc 240D 24.4 4 146.7 62 3.69 3.190 20.00 1 0
Merc 230 Merc 230 22.8 4 140.8 95 3.92 3.150 22.90 1 0
Merc 280 Merc 280 19.2 6 167.6 123 3.92 3.440 18.30 1 0
gear carb new_mpg
Mazda RX4 4 4 22.0
Mazda RX4 Wag 4 4 22.0
Datsun 710 4 1 23.8
Hornet 4 Drive 3 1 22.4
Hornet Sportabout 3 2 19.7
Valiant 3 1 19.1
Duster 360 3 4 15.3
Merc 240D 4 2 25.4
Merc 230 4 2 23.8
Merc 280 4 4 20.2
model_name mpg cyl ... gear carb new_mpg
Mazda RX4 Mazda RX4 21.0 6.0 ... 4.0 4.0 22.0
Mazda RX4 Wag Mazda RX4 Wag 21.0 6.0 ... 4.0 4.0 22.0
Datsun 710 Datsun 710 22.8 4.0 ... 4.0 1.0 23.8
Hornet 4 Drive Hornet 4 Drive 21.4 6.0 ... 3.0 1.0 22.4
Hornet Sportabout Hornet Sportabout 18.7 8.0 ... 3.0 2.0 19.7
Valiant Valiant 18.1 6.0 ... 3.0 1.0 19.1
Duster 360 Duster 360 14.3 8.0 ... 3.0 4.0 15.3
Merc 240D Merc 240D 24.4 4.0 ... 4.0 2.0 25.4
Merc 230 Merc 230 22.8 4.0 ... 4.0 2.0 23.8
Merc 280 Merc 280 19.2 6.0 ... 4.0 4.0 20.2
[10 rows x 13 columns]
model_name mpg cyl ... gear carb new_mpg
Mazda RX4 Mazda RX4 21.0 6.0 ... 4.0 4.0 22.0
Mazda RX4 Wag Mazda RX4 Wag 21.0 6.0 ... 4.0 4.0 22.0
Datsun 710 Datsun 710 22.8 4.0 ... 4.0 1.0 23.8
Hornet 4 Drive Hornet 4 Drive 21.4 6.0 ... 3.0 1.0 22.4
Hornet Sportabout Hornet Sportabout 18.7 8.0 ... 3.0 2.0 19.7
Valiant Valiant 18.1 6.0 ... 3.0 1.0 19.1
Duster 360 Duster 360 14.3 8.0 ... 3.0 4.0 15.3
Merc 240D Merc 240D 24.4 4.0 ... 4.0 2.0 25.4
Merc 230 Merc 230 22.8 4.0 ... 4.0 2.0 23.8
Merc 280 Merc 280 19.2 6.0 ... 4.0 4.0 20.2
[10 rows x 13 columns]
model_name | mpg | cyl | disp | hp | drat | wt | qsec | vs | am | gear | carb |
---|---|---|---|---|---|---|---|---|---|---|---|
Mazda RX4 | 21 | 6 | 160 | 110 | 3.9 | 2.620 | 16.46 | 0 | 1 | 4 | 4 |
Mazda RX4 Wag | 21 | 6 | 160 | 110 | 3.9 | 2.875 | 17.02 | 0 | 1 | 4 | 4 |
model_name | mpg | cyl | disp | hp | drat | wt | qsec | vs | am | gear | carb |
---|---|---|---|---|---|---|---|---|---|---|---|
Mazda RX4 | 21.0 | 6 | 160.0 | 110 | 3.90 | 2.620 | 16.46 | 0 | 1 | 4 | 4 |
Mazda RX4 Wag | 21.0 | 6 | 160.0 | 110 | 3.90 | 2.875 | 17.02 | 0 | 1 | 4 | 4 |
Hornet 4 Drive | 21.4 | 6 | 258.0 | 110 | 3.08 | 3.215 | 19.44 | 1 | 0 | 3 | 1 |
Valiant | 18.1 | 6 | 225.0 | 105 | 2.76 | 3.460 | 20.22 | 1 | 0 | 3 | 1 |
Merc 280 | 19.2 | 6 | 167.6 | 123 | 3.92 | 3.440 | 18.30 | 1 | 0 | 4 | 4 |
model_name mpg cyl disp hp drat wt qsec vs am gear carb
Mazda RX4 Mazda RX4 21 6 160 110 3.9 2.620 16.46 0 1 4 4
Mazda RX4 Wag Mazda RX4 Wag 21 6 160 110 3.9 2.875 17.02 0 1 4 4
model_name mpg cyl disp hp drat wt qsec vs am gear
Mazda RX4 Mazda RX4 21.0 6 160.0 110 3.90 2.620 16.46 0 1 4
Mazda RX4 Wag Mazda RX4 Wag 21.0 6 160.0 110 3.90 2.875 17.02 0 1 4
Hornet 4 Drive Hornet 4 Drive 21.4 6 258.0 110 3.08 3.215 19.44 1 0 3
Valiant Valiant 18.1 6 225.0 105 2.76 3.460 20.22 1 0 3
Merc 280 Merc 280 19.2 6 167.6 123 3.92 3.440 18.30 1 0 4
carb
Mazda RX4 4
Mazda RX4 Wag 4
Hornet 4 Drive 1
Valiant 1
Merc 280 4
model_name mpg cyl disp ... am gear carb new_mpg
Mazda RX4 Mazda RX4 21.0 6.0 160.0 ... 1.0 4.0 4.0 22.0
Mazda RX4 Wag Mazda RX4 Wag 21.0 6.0 160.0 ... 1.0 4.0 4.0 22.0
[2 rows x 13 columns]
model_name mpg cyl disp ... am gear carb new_mpg
Mazda RX4 Mazda RX4 21.0 6.0 160.0 ... 1.0 4.0 4.0 22.0
Mazda RX4 Wag Mazda RX4 Wag 21.0 6.0 160.0 ... 1.0 4.0 4.0 22.0
Hornet 4 Drive Hornet 4 Drive 21.4 6.0 258.0 ... 0.0 3.0 1.0 22.4
Valiant Valiant 18.1 6.0 225.0 ... 0.0 3.0 1.0 19.1
Merc 280 Merc 280 19.2 6.0 167.6 ... 0.0 4.0 4.0 20.2
[5 rows x 13 columns]
model_name mpg cyl disp ... am gear carb new_mpg
Mazda RX4 Mazda RX4 21.0 6.0 160.0 ... 1.0 4.0 4.0 22.0
Mazda RX4 Wag Mazda RX4 Wag 21.0 6.0 160.0 ... 1.0 4.0 4.0 22.0
[2 rows x 13 columns]
model_name mpg cyl disp ... am gear carb new_mpg
Mazda RX4 Mazda RX4 21.0 6.0 160.0 ... 1.0 4.0 4.0 22.0
Mazda RX4 Wag Mazda RX4 Wag 21.0 6.0 160.0 ... 1.0 4.0 4.0 22.0
Hornet 4 Drive Hornet 4 Drive 21.4 6.0 258.0 ... 0.0 3.0 1.0 22.4
Valiant Valiant 18.1 6.0 225.0 ... 0.0 3.0 1.0 19.1
Merc 280 Merc 280 19.2 6.0 167.6 ... 0.0 4.0 4.0 20.2
[5 rows x 13 columns]
allows a new table to be created or for an existing table to be overwritten by the new table. This is shorthand for dropping the existing table and then creating the new one.
will only proceed with the creation of the table if it does not already exist. If the table already exists, no action will be taken and the existing table will remain in the database.
model_name | total_mpg | cyl_mean |
---|---|---|
Valiant | 18.1 | 6 |
Merc 240D | 24.4 | 4 |
Datsun 710 | 22.8 | 4 |
Duster 360 | 14.3 | 8 |
Merc 280 | 19.2 | 6 |
Hornet Sportabout | 18.7 | 8 |
Merc 230 | 22.8 | 4 |
Mazda RX4 | 21.0 | 6 |
Mazda RX4 Wag | 21.0 | 6 |
Hornet 4 Drive | 21.4 | 6 |
model_name | mpg | cyl | model_name | total_mpg | cyl_mean |
---|---|---|---|---|---|
Mazda RX4 | 21.0 | 6 | Mazda RX4 | 21.0 | 6 |
Mazda RX4 Wag | 21.0 | 6 | Mazda RX4 Wag | 21.0 | 6 |
Datsun 710 | 22.8 | 4 | Datsun 710 | 22.8 | 4 |
Hornet 4 Drive | 21.4 | 6 | Hornet 4 Drive | 21.4 | 6 |
Hornet Sportabout | 18.7 | 8 | Hornet Sportabout | 18.7 | 8 |
model_name mpg.x cyl.x disp.x hp.x drat.x wt.x qsec.x vs.x am.x
1 Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1
2 Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1
3 Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1
4 Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0
5 Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0
gear.x carb.x mpg.y cyl.y disp.y hp.y drat.y wt.y qsec.y vs.y am.y gear.y
1 4 4 21.0 6 160 110 3.90 2.620 16.46 0 1 4
2 4 4 21.0 6 160 110 3.90 2.875 17.02 0 1 4
3 4 1 22.8 4 108 93 3.85 2.320 18.61 1 1 4
4 3 1 21.4 6 258 110 3.08 3.215 19.44 1 0 3
5 3 2 18.7 8 360 175 3.15 3.440 17.02 0 0 3
carb.y
1 4
2 4
3 1
4 1
5 2
model_name mpg_x cyl_x disp_x ... am_y gear_y carb_y new_mpg_y
0 Mazda RX4 21.0 6.0 160.0 ... 1.0 4.0 4.0 22.0
1 Mazda RX4 Wag 21.0 6.0 160.0 ... 1.0 4.0 4.0 22.0
2 Datsun 710 22.8 4.0 108.0 ... 1.0 4.0 1.0 23.8
3 Hornet 4 Drive 21.4 6.0 258.0 ... 0.0 3.0 1.0 22.4
4 Hornet Sportabout 18.7 8.0 360.0 ... 0.0 3.0 2.0 19.7
[5 rows x 25 columns]
model_name mpg_x cyl_x disp_x ... am_y gear_y carb_y new_mpg_y
0 Mazda RX4 21.0 6.0 160.0 ... 1.0 4.0 4.0 22.0
1 Mazda RX4 Wag 21.0 6.0 160.0 ... 1.0 4.0 4.0 22.0
2 Datsun 710 22.8 4.0 108.0 ... 1.0 4.0 1.0 23.8
3 Hornet 4 Drive 21.4 6.0 258.0 ... 0.0 3.0 1.0 22.4
4 Hornet Sportabout 18.7 8.0 360.0 ... 0.0 3.0 2.0 19.7
[5 rows x 25 columns]
model_name | mpg | cyl | model_name | total_mpg | cyl_mean |
---|---|---|---|---|---|
Datsun 710 | 22.8 | 4 | Datsun 710 | 22.8 | 4 |
Valiant | 18.1 | 6 | Valiant | 18.1 | 6 |
Duster 360 | 14.3 | 8 | Duster 360 | 14.3 | 8 |
Merc 240D | 24.4 | 4 | Merc 240D | 24.4 | 4 |
Merc 280 | 19.2 | 6 | Merc 280 | 19.2 | 6 |
model_name mpg.x cyl.x disp.x hp.x drat.x wt.x qsec.x vs.x am.x
1 Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1
2 Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1
3 Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1
4 Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0
5 Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0
gear.x carb.x mpg.y cyl.y disp.y hp.y drat.y wt.y qsec.y vs.y am.y gear.y
1 4 4 21.0 6 160 110 3.90 2.620 16.46 0 1 4
2 4 4 21.0 6 160 110 3.90 2.875 17.02 0 1 4
3 4 1 22.8 4 108 93 3.85 2.320 18.61 1 1 4
4 3 1 21.4 6 258 110 3.08 3.215 19.44 1 0 3
5 3 2 18.7 8 360 175 3.15 3.440 17.02 0 0 3
carb.y
1 4
2 4
3 1
4 1
5 2
model_name mpg_x cyl_x disp_x ... am_y gear_y carb_y new_mpg_y
0 Mazda RX4 21.0 6.0 160.0 ... 1.0 4.0 4.0 22.0
1 Mazda RX4 Wag 21.0 6.0 160.0 ... 1.0 4.0 4.0 22.0
2 Datsun 710 22.8 4.0 108.0 ... 1.0 4.0 1.0 23.8
3 Hornet 4 Drive 21.4 6.0 258.0 ... 0.0 3.0 1.0 22.4
4 Hornet Sportabout 18.7 8.0 360.0 ... 0.0 3.0 2.0 19.7
[5 rows x 25 columns]
model_name mpg_x cyl_x disp_x ... am_y gear_y carb_y new_mpg_y
0 Mazda RX4 21.0 6.0 160.0 ... 1.0 4.0 4.0 22.0
1 Mazda RX4 Wag 21.0 6.0 160.0 ... 1.0 4.0 4.0 22.0
2 Datsun 710 22.8 4.0 108.0 ... 1.0 4.0 1.0 23.8
3 Hornet 4 Drive 21.4 6.0 258.0 ... 0.0 3.0 1.0 22.4
4 Hornet Sportabout 18.7 8.0 360.0 ... 0.0 3.0 2.0 19.7
[5 rows x 25 columns]
union all
model_name mpg cyl disp hp drat wt qsec vs
Mazda RX4...1 Mazda RX4 21.0 6 160.0 110 3.90 2.620 16.46 0
Mazda RX4 Wag...2 Mazda RX4 Wag 21.0 6 160.0 110 3.90 2.875 17.02 0
Datsun 710...3 Datsun 710 22.8 4 108.0 93 3.85 2.320 18.61 1
Hornet 4 Drive...4 Hornet 4 Drive 21.4 6 258.0 110 3.08 3.215 19.44 1
Hornet Sportabout...5 Hornet Sportabout 18.7 8 360.0 175 3.15 3.440 17.02 0
Valiant...6 Valiant 18.1 6 225.0 105 2.76 3.460 20.22 1
Duster 360...7 Duster 360 14.3 8 360.0 245 3.21 3.570 15.84 0
Merc 240D...8 Merc 240D 24.4 4 146.7 62 3.69 3.190 20.00 1
Merc 230...9 Merc 230 22.8 4 140.8 95 3.92 3.150 22.90 1
Merc 280...10 Merc 280 19.2 6 167.6 123 3.92 3.440 18.30 1
Mazda RX4...11 Mazda RX4 21.0 6 160.0 110 3.90 2.620 16.46 0
Mazda RX4 Wag...12 Mazda RX4 Wag 21.0 6 160.0 110 3.90 2.875 17.02 0
Datsun 710...13 Datsun 710 22.8 4 108.0 93 3.85 2.320 18.61 1
Hornet 4 Drive...14 Hornet 4 Drive 21.4 6 258.0 110 3.08 3.215 19.44 1
Hornet Sportabout...15 Hornet Sportabout 18.7 8 360.0 175 3.15 3.440 17.02 0
Valiant...16 Valiant 18.1 6 225.0 105 2.76 3.460 20.22 1
Duster 360...17 Duster 360 14.3 8 360.0 245 3.21 3.570 15.84 0
Merc 240D...18 Merc 240D 24.4 4 146.7 62 3.69 3.190 20.00 1
Merc 230...19 Merc 230 22.8 4 140.8 95 3.92 3.150 22.90 1
Merc 280...20 Merc 280 19.2 6 167.6 123 3.92 3.440 18.30 1
am gear carb
Mazda RX4...1 1 4 4
Mazda RX4 Wag...2 1 4 4
Datsun 710...3 1 4 1
Hornet 4 Drive...4 0 3 1
Hornet Sportabout...5 0 3 2
Valiant...6 0 3 1
Duster 360...7 0 3 4
Merc 240D...8 0 4 2
Merc 230...9 0 4 2
Merc 280...10 0 4 4
Mazda RX4...11 1 4 4
Mazda RX4 Wag...12 1 4 4
Datsun 710...13 1 4 1
Hornet 4 Drive...14 0 3 1
Hornet Sportabout...15 0 3 2
Valiant...16 0 3 1
Duster 360...17 0 3 4
Merc 240D...18 0 4 2
Merc 230...19 0 4 2
Merc 280...20 0 4 4
model_name mpg cyl disp hp ... vs am gear carb new_mpg
0 Mazda RX4 21.0 6.0 160.0 110.0 ... 0.0 1.0 4.0 4.0 22.0
1 Mazda RX4 Wag 21.0 6.0 160.0 110.0 ... 0.0 1.0 4.0 4.0 22.0
2 Datsun 710 22.8 4.0 108.0 93.0 ... 1.0 1.0 4.0 1.0 23.8
3 Hornet 4 Drive 21.4 6.0 258.0 110.0 ... 1.0 0.0 3.0 1.0 22.4
4 Hornet Sportabout 18.7 8.0 360.0 175.0 ... 0.0 0.0 3.0 2.0 19.7
5 Valiant 18.1 6.0 225.0 105.0 ... 1.0 0.0 3.0 1.0 19.1
6 Duster 360 14.3 8.0 360.0 245.0 ... 0.0 0.0 3.0 4.0 15.3
7 Merc 240D 24.4 4.0 146.7 62.0 ... 1.0 0.0 4.0 2.0 25.4
8 Merc 230 22.8 4.0 140.8 95.0 ... 1.0 0.0 4.0 2.0 23.8
9 Merc 280 19.2 6.0 167.6 123.0 ... 1.0 0.0 4.0 4.0 20.2
10 Mazda RX4 21.0 6.0 160.0 110.0 ... 0.0 1.0 4.0 4.0 22.0
11 Mazda RX4 Wag 21.0 6.0 160.0 110.0 ... 0.0 1.0 4.0 4.0 22.0
12 Datsun 710 22.8 4.0 108.0 93.0 ... 1.0 1.0 4.0 1.0 23.8
13 Hornet 4 Drive 21.4 6.0 258.0 110.0 ... 1.0 0.0 3.0 1.0 22.4
14 Hornet Sportabout 18.7 8.0 360.0 175.0 ... 0.0 0.0 3.0 2.0 19.7
15 Valiant 18.1 6.0 225.0 105.0 ... 1.0 0.0 3.0 1.0 19.1
16 Duster 360 14.3 8.0 360.0 245.0 ... 0.0 0.0 3.0 4.0 15.3
17 Merc 240D 24.4 4.0 146.7 62.0 ... 1.0 0.0 4.0 2.0 25.4
18 Merc 230 22.8 4.0 140.8 95.0 ... 1.0 0.0 4.0 2.0 23.8
19 Merc 280 19.2 6.0 167.6 123.0 ... 1.0 0.0 4.0 4.0 20.2
[20 rows x 13 columns]
union
model_name mpg cyl disp hp drat wt qsec vs am
Mazda RX4 Mazda RX4 21.0 6 160.0 110 3.90 2.620 16.46 0 1
Mazda RX4 Wag Mazda RX4 Wag 21.0 6 160.0 110 3.90 2.875 17.02 0 1
Datsun 710 Datsun 710 22.8 4 108.0 93 3.85 2.320 18.61 1 1
Hornet 4 Drive Hornet 4 Drive 21.4 6 258.0 110 3.08 3.215 19.44 1 0
Hornet Sportabout Hornet Sportabout 18.7 8 360.0 175 3.15 3.440 17.02 0 0
Valiant Valiant 18.1 6 225.0 105 2.76 3.460 20.22 1 0
Duster 360 Duster 360 14.3 8 360.0 245 3.21 3.570 15.84 0 0
Merc 240D Merc 240D 24.4 4 146.7 62 3.69 3.190 20.00 1 0
Merc 230 Merc 230 22.8 4 140.8 95 3.92 3.150 22.90 1 0
Merc 280 Merc 280 19.2 6 167.6 123 3.92 3.440 18.30 1 0
gear carb
Mazda RX4 4 4
Mazda RX4 Wag 4 4
Datsun 710 4 1
Hornet 4 Drive 3 1
Hornet Sportabout 3 2
Valiant 3 1
Duster 360 3 4
Merc 240D 4 2
Merc 230 4 2
Merc 280 4 4
model_name mpg cyl disp hp ... vs am gear carb new_mpg
0 Mazda RX4 21.0 6.0 160.0 110.0 ... 0.0 1.0 4.0 4.0 22.0
1 Mazda RX4 Wag 21.0 6.0 160.0 110.0 ... 0.0 1.0 4.0 4.0 22.0
2 Datsun 710 22.8 4.0 108.0 93.0 ... 1.0 1.0 4.0 1.0 23.8
3 Hornet 4 Drive 21.4 6.0 258.0 110.0 ... 1.0 0.0 3.0 1.0 22.4
4 Hornet Sportabout 18.7 8.0 360.0 175.0 ... 0.0 0.0 3.0 2.0 19.7
5 Valiant 18.1 6.0 225.0 105.0 ... 1.0 0.0 3.0 1.0 19.1
6 Duster 360 14.3 8.0 360.0 245.0 ... 0.0 0.0 3.0 4.0 15.3
7 Merc 240D 24.4 4.0 146.7 62.0 ... 1.0 0.0 4.0 2.0 25.4
8 Merc 230 22.8 4.0 140.8 95.0 ... 1.0 0.0 4.0 2.0 23.8
9 Merc 280 19.2 6.0 167.6 123.0 ... 1.0 0.0 4.0 4.0 20.2
[10 rows x 13 columns]
model_name mpg cyl disp hp drat wt qsec vs am
Mazda RX4 Wag Mazda RX4 Wag 21.0 6 160.0 110 3.90 2.875 17.02 0 1
Datsun 710 Datsun 710 22.8 4 108.0 93 3.85 2.320 18.61 1 1
Hornet 4 Drive Hornet 4 Drive 21.4 6 258.0 110 3.08 3.215 19.44 1 0
Hornet Sportabout Hornet Sportabout 18.7 8 360.0 175 3.15 3.440 17.02 0 0
Valiant Valiant 18.1 6 225.0 105 2.76 3.460 20.22 1 0
Duster 360 Duster 360 14.3 8 360.0 245 3.21 3.570 15.84 0 0
Merc 240D Merc 240D 24.4 4 146.7 62 3.69 3.190 20.00 1 0
Merc 230 Merc 230 22.8 4 140.8 95 3.92 3.150 22.90 1 0
Merc 280 Merc 280 19.2 6 167.6 123 3.92 3.440 18.30 1 0
gear carb
Mazda RX4 Wag 4 4
Datsun 710 4 1
Hornet 4 Drive 3 1
Hornet Sportabout 3 2
Valiant 3 1
Duster 360 3 4
Merc 240D 4 2
Merc 230 4 2
Merc 280 4 4
model_name mpg cyl ... gear carb new_mpg
Mazda RX4 Wag Mazda RX4 Wag 21.0 6.0 ... 4.0 4.0 22.0
Datsun 710 Datsun 710 22.8 4.0 ... 4.0 1.0 23.8
Hornet 4 Drive Hornet 4 Drive 21.4 6.0 ... 3.0 1.0 22.4
Hornet Sportabout Hornet Sportabout 18.7 8.0 ... 3.0 2.0 19.7
Valiant Valiant 18.1 6.0 ... 3.0 1.0 19.1
Duster 360 Duster 360 14.3 8.0 ... 3.0 4.0 15.3
Merc 240D Merc 240D 24.4 4.0 ... 4.0 2.0 25.4
Merc 230 Merc 230 22.8 4.0 ... 4.0 2.0 23.8
Merc 280 Merc 280 19.2 6.0 ... 4.0 4.0 20.2
[9 rows x 13 columns]
model_name mpg cyl ... gear carb new_mpg
Mazda RX4 Wag Mazda RX4 Wag 21.0 6.0 ... 4.0 4.0 22.0
Datsun 710 Datsun 710 22.8 4.0 ... 4.0 1.0 23.8
Hornet 4 Drive Hornet 4 Drive 21.4 6.0 ... 3.0 1.0 22.4
Hornet Sportabout Hornet Sportabout 18.7 8.0 ... 3.0 2.0 19.7
Valiant Valiant 18.1 6.0 ... 3.0 1.0 19.1
Duster 360 Duster 360 14.3 8.0 ... 3.0 4.0 15.3
Merc 240D Merc 240D 24.4 4.0 ... 4.0 2.0 25.4
Merc 230 Merc 230 22.8 4.0 ... 4.0 2.0 23.8
Merc 280 Merc 280 19.2 6.0 ... 4.0 4.0 20.2
[9 rows x 13 columns]
model_name | mpg |
---|
model_name mpg cyl ... gear carb new_mpg
Mazda RX4 Mazda RX4 21.0 6.0 ... 4.0 4.0 22.0
Mazda RX4 Wag Mazda RX4 Wag 999.0 6.0 ... 4.0 4.0 22.0
Datsun 710 Datsun 710 22.8 4.0 ... 4.0 1.0 23.8
Hornet 4 Drive Hornet 4 Drive 21.4 6.0 ... 3.0 1.0 22.4
Hornet Sportabout Hornet Sportabout 18.7 8.0 ... 3.0 2.0 19.7
Valiant Valiant 18.1 6.0 ... 3.0 1.0 19.1
Duster 360 Duster 360 14.3 8.0 ... 3.0 4.0 15.3
Merc 240D Merc 240D 24.4 4.0 ... 4.0 2.0 25.4
Merc 230 Merc 230 22.8 4.0 ... 4.0 2.0 23.8
Merc 280 Merc 280 19.2 6.0 ... 4.0 4.0 20.2
[10 rows x 13 columns]
Before drop
database | schema | name | column_names | column_types | temporary |
---|---|---|---|---|---|
my-db | main | iris_table | Sepal.Length, Sepal.Width , Petal.Length, Petal.Width , Species | DOUBLE , DOUBLE , DOUBLE , DOUBLE , ENUM(‘setosa’, ‘versicolor’, ‘virginica’) | FALSE |
my-db | main | mtcars_table | model_name, mpg , cyl , disp , hp , drat , wt , qsec , vs , am , gear , carb | VARCHAR, DOUBLE , DOUBLE , DOUBLE , DOUBLE , DOUBLE , DOUBLE , DOUBLE , DOUBLE , DOUBLE , DOUBLE , DOUBLE | FALSE |
my-db | main | new_mtcars_table_group | model_name, total_mpg , cyl_mean | VARCHAR, DOUBLE , DOUBLE | FALSE |
temp | main | double_mtcars_table | model_name, mpg , cyl , disp , hp , drat , wt , qsec , vs , am , gear , carb | VARCHAR, DOUBLE , DOUBLE , DOUBLE , DOUBLE , DOUBLE , DOUBLE , DOUBLE , DOUBLE , DOUBLE , DOUBLE , DOUBLE | TRUE |
temp | main | mtcars_table_group | model_name, total_mpg , cyl_mean | VARCHAR, DOUBLE , DOUBLE | TRUE |
dropping
after drop
database | schema | name | column_names | column_types | temporary |
---|---|---|---|---|---|
my-db | main | iris_table | Sepal.Length, Sepal.Width , Petal.Length, Petal.Width , Species | DOUBLE , DOUBLE , DOUBLE , DOUBLE , ENUM(‘setosa’, ‘versicolor’, ‘virginica’) | FALSE |
my-db | main | mtcars_table | model_name, mpg , cyl , disp , hp , drat , wt , qsec , vs , am , gear , carb | VARCHAR, DOUBLE , DOUBLE , DOUBLE , DOUBLE , DOUBLE , DOUBLE , DOUBLE , DOUBLE , DOUBLE , DOUBLE , DOUBLE | FALSE |
my-db | main | new_mtcars_table_group | model_name, total_mpg , cyl_mean | VARCHAR, DOUBLE , DOUBLE | FALSE |
temp | main | double_mtcars_table | model_name, mpg , cyl , disp , hp , drat , wt , qsec , vs , am , gear , carb | VARCHAR, DOUBLE , DOUBLE , DOUBLE , DOUBLE , DOUBLE , DOUBLE , DOUBLE , DOUBLE , DOUBLE , DOUBLE , DOUBLE | TRUE |
long to wide
DROP table IF EXISTS cities;
CREATE temp TABLE cities (
country VARCHAR, name VARCHAR, year INTEGER, population INTEGER
);
INSERT INTO cities VALUES
('NL', 'Amsterdam', 2000, 1005),
('NL', 'Amsterdam', 2010, 1065),
('NL', 'Amsterdam', 2020, 1158),
('US', 'Seattle', 2000, 564),
('US', 'Seattle', 2010, 608),
('US', 'Seattle', 2020, 738),
('US', 'New York City', 2000, 8015),
('US', 'New York City', 2010, 8175),
('US', 'New York City', 2020, 8772);
country | name | year | population |
---|---|---|---|
NL | Amsterdam | 2000 | 1005 |
NL | Amsterdam | 2010 | 1065 |
NL | Amsterdam | 2020 | 1158 |
US | Seattle | 2000 | 564 |
US | Seattle | 2010 | 608 |
US | Seattle | 2020 | 738 |
US | New York City | 2000 | 8015 |
US | New York City | 2010 | 8175 |
US | New York City | 2020 | 8772 |
wide to long
in R and Python using GT package
model_name | mpg | cyl | disp | hp | drat | wt | qsec | vs | am | gear | carb |
---|---|---|---|---|---|---|---|---|---|---|---|
Mazda RX4 | 21.0 | 6 | 160.0 | 110 | 3.90 | 2.620 | 16.46 | 0 | 1 | 4 | 4 |
Mazda RX4 Wag | 21.0 | 6 | 160.0 | 110 | 3.90 | 2.875 | 17.02 | 0 | 1 | 4 | 4 |
Datsun 710 | 22.8 | 4 | 108.0 | 93 | 3.85 | 2.320 | 18.61 | 1 | 1 | 4 | 1 |
Hornet 4 Drive | 21.4 | 6 | 258.0 | 110 | 3.08 | 3.215 | 19.44 | 1 | 0 | 3 | 1 |
Hornet Sportabout | 18.7 | 8 | 360.0 | 175 | 3.15 | 3.440 | 17.02 | 0 | 0 | 3 | 2 |
Valiant | 18.1 | 6 | 225.0 | 105 | 2.76 | 3.460 | 20.22 | 1 | 0 | 3 | 1 |
Duster 360 | 14.3 | 8 | 360.0 | 245 | 3.21 | 3.570 | 15.84 | 0 | 0 | 3 | 4 |
Merc 240D | 24.4 | 4 | 146.7 | 62 | 3.69 | 3.190 | 20.00 | 1 | 0 | 4 | 2 |
Merc 230 | 22.8 | 4 | 140.8 | 95 | 3.92 | 3.150 | 22.90 | 1 | 0 | 4 | 2 |
Merc 280 | 19.2 | 6 | 167.6 | 123 | 3.92 | 3.440 | 18.30 | 1 | 0 | 4 | 4 |
GT(_tbl_data= model_name mpg cyl ... gear carb new_mpg
Mazda RX4 Mazda RX4 21.0 6.0 ... 4.0 4.0 22.0
Mazda RX4 Wag Mazda RX4 Wag 999.0 6.0 ... 4.0 4.0 22.0
Datsun 710 Datsun 710 22.8 4.0 ... 4.0 1.0 23.8
Hornet 4 Drive Hornet 4 Drive 21.4 6.0 ... 3.0 1.0 22.4
Hornet Sportabout Hornet Sportabout 18.7 8.0 ... 3.0 2.0 19.7
Valiant Valiant 18.1 6.0 ... 3.0 1.0 19.1
Duster 360 Duster 360 14.3 8.0 ... 3.0 4.0 15.3
Merc 240D Merc 240D 24.4 4.0 ... 4.0 2.0 25.4
Merc 230 Merc 230 22.8 4.0 ... 4.0 2.0 23.8
Merc 280 Merc 280 19.2 6.0 ... 4.0 4.0 20.2
[10 rows x 13 columns], _body=<great_tables._gt_data.Body object at 0x16672a890>, _boxhead=Boxhead([ColInfo(var='model_name', type=<ColInfoTypeEnum.default: 1>, column_label='model_name', column_align='left', column_width=None), ColInfo(var='mpg', type=<ColInfoTypeEnum.default: 1>, column_label='mpg', column_align='right', column_width=None), ColInfo(var='cyl', type=<ColInfoTypeEnum.default: 1>, column_label='cyl', column_align='right', column_width=None), ColInfo(var='disp', type=<ColInfoTypeEnum.default: 1>, column_label='disp', column_align='right', column_width=None), ColInfo(var='hp', type=<ColInfoTypeEnum.default: 1>, column_label='hp', column_align='right', column_width=None), ColInfo(var='drat', type=<ColInfoTypeEnum.default: 1>, column_label='drat', column_align='right', column_width=None), ColInfo(var='wt', type=<ColInfoTypeEnum.default: 1>, column_label='wt', column_align='right', column_width=None), ColInfo(var='qsec', type=<ColInfoTypeEnum.default: 1>, column_label='qsec', column_align='right', column_width=None), ColInfo(var='vs', type=<ColInfoTypeEnum.default: 1>, column_label='vs', column_align='right', column_width=None), ColInfo(var='am', type=<ColInfoTypeEnum.default: 1>, column_label='am', column_align='right', column_width=None), ColInfo(var='gear', type=<ColInfoTypeEnum.default: 1>, column_label='gear', column_align='right', column_width=None), ColInfo(var='carb', type=<ColInfoTypeEnum.default: 1>, column_label='carb', column_align='right', column_width=None), ColInfo(var='new_mpg', type=<ColInfoTypeEnum.default: 1>, column_label='new_mpg', column_align='right', column_width=None)]), _stub=<great_tables._gt_data.Stub object at 0x167b9d050>, _spanners=Spanners([]), _heading=Heading(title=None, subtitle=None, preheader=None), _stubhead=None, _source_notes=[], _footnotes=[], _styles=[], _locale=<great_tables._gt_data.Locale object at 0x167b90450>, _formats=[], _substitutions=[], _options=Options(table_id=OptionsInfo(scss=False, category='table', type='value', value=None), table_caption=OptionsInfo(scss=False, category='table', type='value', value=None), table_width=OptionsInfo(scss=True, category='table', type='px', value='auto'), table_layout=OptionsInfo(scss=True, category='table', type='value', value='fixed'), table_margin_left=OptionsInfo(scss=True, category='table', type='px', value='auto'), table_margin_right=OptionsInfo(scss=True, category='table', type='px', value='auto'), table_background_color=OptionsInfo(scss=True, category='table', type='value', value='#FFFFFF'), table_additional_css=OptionsInfo(scss=False, category='table', type='values', value=[]), table_font_names=OptionsInfo(scss=False, category='table', type='values', value=['-apple-system', 'BlinkMacSystemFont', 'Segoe UI', 'Roboto', 'Oxygen', 'Ubuntu', 'Cantarell', 'Helvetica Neue', 'Fira Sans', 'Droid Sans', 'Arial', 'sans-serif']), table_font_size=OptionsInfo(scss=True, category='table', type='px', value='16px'), table_font_weight=OptionsInfo(scss=True, category='table', type='value', value='normal'), table_font_style=OptionsInfo(scss=True, category='table', type='value', value='normal'), table_font_color=OptionsInfo(scss=True, category='table', type='value', value='#333333'), table_font_color_light=OptionsInfo(scss=True, category='table', type='value', value='#FFFFFF'), table_border_top_include=OptionsInfo(scss=False, category='table', type='boolean', value=True), table_border_top_style=OptionsInfo(scss=True, category='table', type='value', value='solid'), table_border_top_width=OptionsInfo(scss=True, category='table', type='px', value='2px'), table_border_top_color=OptionsInfo(scss=True, category='table', type='value', value='#A8A8A8'), table_border_right_style=OptionsInfo(scss=True, category='table', type='value', value='none'), table_border_right_width=OptionsInfo(scss=True, category='table', type='px', value='2px'), table_border_right_color=OptionsInfo(scss=True, category='table', type='value', value='#D3D3D3'), table_border_bottom_include=OptionsInfo(scss=False, category='table', type='boolean', value=True), table_border_bottom_style=OptionsInfo(scss=True, category='table', type='value', value='solid'), table_border_bottom_width=OptionsInfo(scss=True, category='table', type='px', value='2px'), table_border_bottom_color=OptionsInfo(scss=True, category='table', type='value', value='#A8A8A8'), table_border_left_style=OptionsInfo(scss=True, category='table', type='value', value='none'), table_border_left_width=OptionsInfo(scss=True, category='table', type='px', value='2px'), table_border_left_color=OptionsInfo(scss=True, category='table', type='value', value='#D3D3D3'), heading_background_color=OptionsInfo(scss=True, category='heading', type='value', value=None), heading_align=OptionsInfo(scss=True, category='heading', type='value', value='center'), heading_title_font_size=OptionsInfo(scss=True, category='heading', type='px', value='125%'), heading_title_font_weight=OptionsInfo(scss=True, category='heading', type='value', value='initial'), heading_subtitle_font_size=OptionsInfo(scss=True, category='heading', type='px', value='85%'), heading_subtitle_font_weight=OptionsInfo(scss=True, category='heading', type='value', value='initial'), heading_padding=OptionsInfo(scss=True, category='heading', type='px', value='4px'), heading_padding_horizontal=OptionsInfo(scss=True, category='heading', type='px', value='5px'), heading_border_bottom_style=OptionsInfo(scss=True, category='heading', type='value', value='solid'), heading_border_bottom_width=OptionsInfo(scss=True, category='heading', type='px', value='2px'), heading_border_bottom_color=OptionsInfo(scss=True, category='heading', type='value', value='#D3D3D3'), heading_border_lr_style=OptionsInfo(scss=True, category='heading', type='value', value='none'), heading_border_lr_width=OptionsInfo(scss=True, category='heading', type='px', value='1px'), heading_border_lr_color=OptionsInfo(scss=True, category='heading', type='value', value='#D3D3D3'), column_labels_background_color=OptionsInfo(scss=True, category='column_labels', type='value', value=None), column_labels_font_size=OptionsInfo(scss=True, category='column_labels', type='px', value='100%'), column_labels_font_weight=OptionsInfo(scss=True, category='column_labels', type='value', value='normal'), column_labels_text_transform=OptionsInfo(scss=True, category='column_labels', type='value', value='inherit'), column_labels_padding=OptionsInfo(scss=True, category='column_labels', type='px', value='5px'), column_labels_padding_horizontal=OptionsInfo(scss=True, category='column_labels', type='px', value='5px'), column_labels_vlines_style=OptionsInfo(scss=True, category='table_body', type='value', value='none'), column_labels_vlines_width=OptionsInfo(scss=True, category='table_body', type='px', value='1px'), column_labels_vlines_color=OptionsInfo(scss=True, category='table_body', type='value', value='#D3D3D3'), column_labels_border_top_style=OptionsInfo(scss=True, category='column_labels', type='value', value='solid'), column_labels_border_top_width=OptionsInfo(scss=True, category='column_labels', type='px', value='2px'), column_labels_border_top_color=OptionsInfo(scss=True, category='column_labels', type='value', value='#D3D3D3'), column_labels_border_bottom_style=OptionsInfo(scss=True, category='column_labels', type='value', value='solid'), column_labels_border_bottom_width=OptionsInfo(scss=True, category='column_labels', type='px', value='2px'), column_labels_border_bottom_color=OptionsInfo(scss=True, category='column_labels', type='value', value='#D3D3D3'), column_labels_border_lr_style=OptionsInfo(scss=True, category='column_labels', type='value', value='none'), column_labels_border_lr_width=OptionsInfo(scss=True, category='column_labels', type='px', value='1px'), column_labels_border_lr_color=OptionsInfo(scss=True, category='column_labels', type='value', value='#D3D3D3'), column_labels_hidden=OptionsInfo(scss=False, category='column_labels', type='boolean', value=False), row_group_background_color=OptionsInfo(scss=True, category='row_group', type='value', value=None), row_group_font_size=OptionsInfo(scss=True, category='row_group', type='px', value='100%'), row_group_font_weight=OptionsInfo(scss=True, category='row_group', type='value', value='initial'), row_group_text_transform=OptionsInfo(scss=True, category='row_group', type='value', value='inherit'), row_group_padding=OptionsInfo(scss=True, category='row_group', type='px', value='8px'), row_group_padding_horizontal=OptionsInfo(scss=True, category='row_group', type='px', value='5px'), row_group_border_top_style=OptionsInfo(scss=True, category='row_group', type='value', value='solid'), row_group_border_top_width=OptionsInfo(scss=True, category='row_group', type='px', value='2px'), row_group_border_top_color=OptionsInfo(scss=True, category='row_group', type='value', value='#D3D3D3'), row_group_border_right_style=OptionsInfo(scss=True, category='row_group', type='value', value='none'), row_group_border_right_width=OptionsInfo(scss=True, category='row_group', type='px', value='1px'), row_group_border_right_color=OptionsInfo(scss=True, category='row_group', type='value', value='#D3D3D3'), row_group_border_bottom_style=OptionsInfo(scss=True, category='row_group', type='value', value='solid'), row_group_border_bottom_width=OptionsInfo(scss=True, category='row_group', type='px', value='2px'), row_group_border_bottom_color=OptionsInfo(scss=True, category='row_group', type='value', value='#D3D3D3'), row_group_border_left_style=OptionsInfo(scss=True, category='row_group', type='value', value='none'), row_group_border_left_width=OptionsInfo(scss=True, category='row_group', type='px', value='1px'), row_group_border_left_color=OptionsInfo(scss=True, category='row_group', type='value', value='#D3D3D3'), row_group_as_column=OptionsInfo(scss=False, category='row_group', type='boolean', value=False), table_body_hlines_style=OptionsInfo(scss=True, category='table_body', type='value', value='solid'), table_body_hlines_width=OptionsInfo(scss=True, category='table_body', type='px', value='1px'), table_body_hlines_color=OptionsInfo(scss=True, category='table_body', type='value', value='#D3D3D3'), table_body_vlines_style=OptionsInfo(scss=True, category='table_body', type='value', value='none'), table_body_vlines_width=OptionsInfo(scss=True, category='table_body', type='px', value='1px'), table_body_vlines_color=OptionsInfo(scss=True, category='table_body', type='value', value='#D3D3D3'), table_body_border_top_style=OptionsInfo(scss=True, category='table_body', type='value', value='solid'), table_body_border_top_width=OptionsInfo(scss=True, category='table_body', type='px', value='2px'), table_body_border_top_color=OptionsInfo(scss=True, category='table_body', type='value', value='#D3D3D3'), table_body_border_bottom_style=OptionsInfo(scss=True, category='table_body', type='value', value='solid'), table_body_border_bottom_width=OptionsInfo(scss=True, category='table_body', type='px', value='2px'), table_body_border_bottom_color=OptionsInfo(scss=True, category='table_body', type='value', value='#D3D3D3'), data_row_padding=OptionsInfo(scss=True, category='data_row', type='px', value='8px'), data_row_padding_horizontal=OptionsInfo(scss=True, category='data_row', type='px', value='5px'), stub_background_color=OptionsInfo(scss=True, category='stub', type='value', value=None), stub_font_size=OptionsInfo(scss=True, category='stub', type='px', value='100%'), stub_font_weight=OptionsInfo(scss=True, category='stub', type='value', value='initial'), stub_text_transform=OptionsInfo(scss=True, category='stub', type='value', value='inherit'), stub_border_style=OptionsInfo(scss=True, category='stub', type='value', value='solid'), stub_border_width=OptionsInfo(scss=True, category='stub', type='px', value='2px'), stub_border_color=OptionsInfo(scss=True, category='stub', type='value', value='#D3D3D3'), stub_row_group_background_color=OptionsInfo(scss=True, category='stub', type='value', value=None), stub_row_group_font_size=OptionsInfo(scss=True, category='stub', type='px', value='100%'), stub_row_group_font_weight=OptionsInfo(scss=True, category='stub', type='value', value='initial'), stub_row_group_text_transform=OptionsInfo(scss=True, category='stub', type='value', value='inherit'), stub_row_group_border_style=OptionsInfo(scss=True, category='stub', type='value', value='solid'), stub_row_group_border_width=OptionsInfo(scss=True, category='stub', type='px', value='2px'), stub_row_group_border_color=OptionsInfo(scss=True, category='stub', type='value', value='#D3D3D3'), source_notes_padding=OptionsInfo(scss=True, category='source_notes', type='px', value='4px'), source_notes_padding_horizontal=OptionsInfo(scss=True, category='source_notes', type='px', value='5px'), source_notes_background_color=OptionsInfo(scss=True, category='source_notes', type='value', value=None), source_notes_font_size=OptionsInfo(scss=True, category='source_notes', type='px', value='90%'), source_notes_border_bottom_style=OptionsInfo(scss=True, category='source_notes', type='value', value='none'), source_notes_border_bottom_width=OptionsInfo(scss=True, category='source_notes', type='px', value='2px'), source_notes_border_bottom_color=OptionsInfo(scss=True, category='source_notes', type='value', value='#D3D3D3'), source_notes_border_lr_style=OptionsInfo(scss=True, category='source_notes', type='value', value='none'), source_notes_border_lr_width=OptionsInfo(scss=True, category='source_notes', type='px', value='2px'), source_notes_border_lr_color=OptionsInfo(scss=True, category='source_notes', type='value', value='#D3D3D3'), source_notes_multiline=OptionsInfo(scss=False, category='source_notes', type='boolean', value=True), source_notes_sep=OptionsInfo(scss=False, category='source_notes', type='value', value=' '), row_striping_background_color=OptionsInfo(scss=True, category='row', type='value', value='rgba(128,128,128,0.05)'), row_striping_include_stub=OptionsInfo(scss=False, category='row', type='boolean', value=False), row_striping_include_table_body=OptionsInfo(scss=False, category='row', type='boolean', value=False), container_width=OptionsInfo(scss=False, category='container', type='px', value='auto'), container_height=OptionsInfo(scss=False, category='container', type='px', value='auto'), container_padding_x=OptionsInfo(scss=False, category='container', type='px', value='0px'), container_padding_y=OptionsInfo(scss=False, category='container', type='px', value='10px'), container_overflow_x=OptionsInfo(scss=False, category='container', type='overflow', value='auto'), container_overflow_y=OptionsInfo(scss=False, category='container', type='overflow', value='auto'), quarto_disable_processing=OptionsInfo(scss=False, category='quarto', type='logical', value=False), quarto_use_bootstrap=OptionsInfo(scss=False, category='quarto', type='logical', value=False)), _has_built=False)
https://duckdb.org/docs/stable/clients/r.html
https://duckdb.org/docs/stable/sql/statements
---
title: "Code"
author: "Tony Duan"
execute:
warning: false
error: false
format:
html:
toc: true
toc-location: right
code-fold: show
code-tools: true
number-sections: true
code-block-bg: true
code-block-border-left: "#31BAE9"
engine: knitr
---
```{r}
Sys.setenv(RETICULATE_PYTHON = "/Library/Frameworks/Python.framework/Versions/3.11/bin/python3.11")
library(reticulate)
use_python("/Library/Frameworks/Python.framework/Versions/3.11/bin/python3.11")
```
Using python 3.11 for siuba pacakege
```{python}
from platform import python_version
print(python_version())
```
```{python}
import os
os.system('pip show pandas')
```
```{python}
import os
os.system('pip show siuba')
```
```{r}
#| code-fold: true
#| output: false
# set up duckdb database
library(DBI)
library(tidyverse)
library(RSQLite)
library(connections)
library(duckdb)
```
```{r}
#| code-fold: true
#| output: false
# set up R data
rm(mtcars)
mtcars=cbind(model_name = rownames(mtcars), mtcars) |> head(10)
iris=iris|> head(10)
```
```{python}
#| code-fold: true
#| output: false
# set up Python data
mtcars=r.mtcars
iris=r.iris
```
```{r}
#| code-fold: true
#| output: false
if (file.exists("my-db.duckdb")) {file.remove("my-db.duckdb")
}
#con <- dbConnect(duckdb(), dbdir = "my-db.duckdb", read_only = TRUE)
con <- dbConnect(duckdb(), dbdir = "my-db.duckdb", read_only = FALSE)
dbWriteTable(con, "iris_table", iris,overwrite = TRUE)
dbWriteTable(con, "mtcars_table", mtcars,overwrite = TRUE)
```
- There are table `iris_table` and `mtcars_table` in duckdb `my-db.duckdb`
- There are table `iris` and `mtcars` R environment
- There are table `iris` and `mtcars` Python environment
# show all table
::: panel-tabset
## sql
```{sql}
#| connection: con
SHOW ALL TABLES;
```
## R
```{r}
dflist <- Filter(is.data.frame, as.list(.GlobalEnv))
names(dflist)
```
## Python
```{python}
import pandas as pd
alldfs = [var for var in dir() if isinstance(eval(var), pd.core.frame.DataFrame)]
print(alldfs)
```
:::
# Describe a table
::: panel-tabset
## sql
```{sql}
#| connection: con
DESCRIBE mtcars_table;
```
## R
```{r}
skimr::skim(mtcars)
```
## Python
```{python}
mtcars.describe(include='all')
```
:::
# show column name
::: panel-tabset
## sql
```{sql}
#| connection: con
PRAGMA table_info(mtcars_table);
```
## R
```{r}
names(mtcars)
```
## Python
```{python}
import pandas as pd
list(mtcars.columns.values)
```
:::
# select
## select top 6 and rename
::: panel-tabset
## sql
```{sql}
#| connection: con
SELECT model_name as model,mpg,cyl from mtcars_table limit 6 ;
```
## R
```{r}
head(mtcars,6) |> select(model=model_name,mpg,cyl)
```
## Python
```{python}
mtcars.rename(columns={'model_name': 'model'})
```
## Python siuba
```{python}
from siuba import *
mtcars >> rename(model=_.model_name) >> select(_.model, _.mpg, _.cyl) >> head(6)
```
:::
## select distinct
all distinct record of mpg and cyl
::: panel-tabset
## sql
```{sql}
#| connection: con
SELECT distinct mpg,cyl from mtcars_table;
```
## R
```{r}
mtcars |> distinct(mpg,cyl)
```
## Python
```{python}
df=mtcars[["mpg", "cyl"]]
print(df.drop_duplicates())
```
## Python siuba
```{python}
mtcars >> distinct(_.mpg, _.cyl)
```
:::
# check row number and column number
::: panel-tabset
## sql
```{sql}
#| connection: con
/*Gives number of rows*/
SELECT count(*) as row_number from mtcars_table;
```
```{sql}
#| connection: con
/*Gives number of columns*/
select count(*) as column_number from (DESCRIBE mtcars_table);
```
## R
```{r}
# Gives number of rows
nrow(mtcars)
```
```{r}
# Gives number of columns
ncol(mtcars)
```
## Python
```{python}
# Gives number of rows
mtcars.shape[0]
```
```{python}
# Gives number of columns
mtcars.shape[1]
```
:::
# create column
::: panel-tabset
## sql
```{sql}
#| connection: con
SELECT *, mpg+1 as new_mpg from mtcars_table;
```
## R
```{r}
mtcars |> mutate(new_mpg=mpg+1)
```
## Python
```{python}
mtcars["new_mpg"]=mtcars["mpg"]+1
mtcars
```
## Python siuba
```{python}
mtcars >> mutate(new_mpg=_.mpg+1)
```
:::
# Filter
::: panel-tabset
## sql
```{sql}
#| connection: con
/* and */
SELECT * from mtcars_table where mpg=21 and cyl=6;
```
```{sql}
#| connection: con
/* or */
SELECT * from mtcars_table where mpg=21 or cyl=6;
```
## R
```{r}
# and
mtcars |> filter(mpg==21&cyl==6)
```
```{r}
# or
mtcars |> filter(mpg==21|cyl==6)
```
## Python
```{python}
# and
mtcars.query('mpg==21 and cyl==6')
```
```{python}
# or
mtcars.query('mpg==21 or cyl==6')
```
## Python siuba
```{python}
# and
mtcars >> filter(_.mpg==21,_.cyl==6)
```
```{python}
# or
mtcars >> filter((_.mpg==21) |(_.cyl==6))
```
:::
# order
::: panel-tabset
## sql
```{sql}
#| connection: con
SELECT model_name as model,mpg,cyl from mtcars_table order by mpg desc limit 3;
```
## R
```{r}
mtcars |> select(model=model_name,mpg,cyl) |> arrange(desc(mpg)) |> head(3)
```
## Python
```{python}
mtcars[["model_name","mpg","cyl"]].sort_values(by='mpg',ascending=False).head(3)
```
## Python siuba
```{python}
mtcars >> select(_.model_name,_.mpg,_.cyl) >> arrange(-_.mpg) >> head(3)
```
:::
# group by
::: panel-tabset
## sql
```{sql}
#| connection: con
SELECT model_name, sum(mpg) as total_mpg,mean(cyl) as cyl_mean from mtcars_table
group by 1 limit 5
;
```
## R
```{r}
mtcars |> group_by(model_name) |> summarise(total_mpg=sum(mpg),cyl_mean=mean(cyl)) |> head(5)
```
## Python
```{python}
mtcars.groupby('model_name').agg({'mpg': 'sum', 'cyl': 'mean'}).head(5)
```
## Python siuba
```{python}
mtcars >> group_by(_.model_name) >> summarize(total_mpg=_.mpg.sum(),cyl_mean=_.cyl.mean()) >> head(5)
```
:::
# create table
## CREATE OR REPLACE
allows a new table to be created or for an existing table to be overwritten by the new table. This is shorthand for dropping the existing table and then creating the new one.
```{sql}
#| connection: con
CREATE OR REPLACE temp table mtcars_table_group as
SELECT model_name, sum(mpg) as total_mpg,mean(cyl) as cyl_mean from mtcars_table
group by 1
;
```
## CREATE TABLE IF NOT EXISTS
will only proceed with the creation of the table if it does not already exist. If the table already exists, no action will be taken and the existing table will remain in the database.
```{sql}
#| connection: con
CREATE TABLE IF NOT EXISTS new_mtcars_table_group as
SELECT model_name, sum(mpg) as total_mpg,mean(cyl) as cyl_mean from mtcars_table
group by 1
;
```
# unique
## check unique
```{sql}
#| connection: con
SELECT count(*),count(distinct model_name) from mtcars_table
;
```
# join
```{sql}
#| connection: con
SELECT * from mtcars_table_group t1 ;
```
## left join
::: panel-tabset
# sql
```{sql}
#| connection: con
SELECT t1.model_name,t1.mpg ,t1.cyl,t2.* from mtcars_table t1
left join mtcars_table_group t2 on t1.model_name = t2.model_name
limit 5
;
```
# R
```{r}
mtcars |> left_join(mtcars, by = join_by(model_name == model_name)) |> head(5)
```
# Python
```{python}
pd.merge(mtcars, mtcars, left_on='model_name', right_on='model_name', how='left').head(5)
```
# Python siuba
```{python}
mtcars >> left_join(_,mtcars, on = {"model_name": "model_name"}) >> head(5)
```
:::
## inter join
::: panel-tabset
# sql
```{sql}
#| connection: con
SELECT t1.model_name,t1.mpg ,t1.cyl,t2.* from mtcars_table t1
inner join (select * from mtcars_table_group limit 5) t2 on t1.model_name = t2.model_name
;
```
# R
```{r}
mtcars |> inner_join(mtcars, by = join_by(model_name == model_name)) |> head(5)
```
# Python
```{python}
pd.merge(mtcars, mtcars, left_on='model_name', right_on='model_name', how='inner').head(5)
```
# Python siuba
```{python}
mtcars >> inner_join(_,mtcars, on = {"model_name": "model_name"}) >> head(5)
```
:::
# append rows
## append without duplicate elimination `union all`
::: panel-tabset
### sql
```{sql}
#| connection: con
SELECT count(*) from mtcars_table
;
```
```{sql}
#| connection: con
CREATE temp table double_mtcars_table as
SELECT * from mtcars_table
union all
SELECT * from mtcars_table
;
```
```{sql}
#| connection: con
SELECT count(*) from double_mtcars_table
;
```
### R
```{r}
mtcars %>% bind_rows(mtcars)
```
### Python
```{python}
pd.concat([mtcars,mtcars],ignore_index=True)
```
:::
## append with duplicate elimination `union`
::: panel-tabset
### sql
```{sql}
#| connection: con
CREATE OR REPLACE temp table double_mtcars_table as
SELECT * from mtcars_table
union
SELECT * from mtcars_table
;
```
```{sql}
#| connection: con
SELECT count(*) from double_mtcars_table
;
```
```{sql}
#| connection: con
select count(*) from (
SELECT distinct * from double_mtcars_table
)
;
```
### R
```{r}
mtcars %>% bind_rows(mtcars) |> distinct()
```
### Python
```{python}
pd.concat([mtcars,mtcars],ignore_index=True).drop_duplicates()
```
:::
# delete rows
::: panel-tabset
## sql
```{sql}
#| connection: con
DELETE FROM mtcars_table WHERE model_name = 'Mazda RX4';
;
```
```{sql}
#| connection: con
select count(*) from mtcars_table
;
```
## R
```{r}
mtcars |> filter(model_name != 'Mazda RX4')
```
## Python
```{python}
mtcars[mtcars['model_name'] != 'Mazda RX4']
```
## Python siuba
```{python}
mtcars >> filter(_.model_name != 'Mazda RX4')
```
:::
# update rows
::: panel-tabset
## sql
```{sql}
#| connection: con
select model_name,mpg from mtcars_table where model_name= 'Mazda RX4'
;
```
```{sql}
#| connection: con
UPDATE mtcars_table
SET mpg = 999
WHERE model_name= 'Mazda RX4'
;
```
```{sql}
#| connection: con
select model_name,mpg from mtcars_table where model_name= 'Mazda RX4'
;
```
## R
```{r}
mtcars |> filter(model_name == 'Mazda RX4 Wag') |> mutate(mpg=999)
```
## Python
```{python}
mtcars.loc[mtcars['model_name'] == 'Mazda RX4 Wag', 'mpg'] = 999
mtcars
```
## Python siuba
```{python}
mtcars >> filter(_.model_name == 'Mazda RX4 Wag') >> mutate(mpg=999)
```
:::
# drop table
Before drop
```{sql}
#| connection: con
SHOW ALL TABLES;
```
dropping
```{sql}
#| connection: con
DROP table IF EXISTS mtcars_table_group;
```
after drop
```{sql}
#| connection: con
SHOW ALL TABLES;
```
# PIVOT
long to wide
```{sql}
#| connection: con
DROP table IF EXISTS cities;
CREATE temp TABLE cities (
country VARCHAR, name VARCHAR, year INTEGER, population INTEGER
);
INSERT INTO cities VALUES
('NL', 'Amsterdam', 2000, 1005),
('NL', 'Amsterdam', 2010, 1065),
('NL', 'Amsterdam', 2020, 1158),
('US', 'Seattle', 2000, 564),
('US', 'Seattle', 2010, 608),
('US', 'Seattle', 2020, 738),
('US', 'New York City', 2000, 8015),
('US', 'New York City', 2010, 8175),
('US', 'New York City', 2020, 8772);
```
```{sql}
#| connection: con
SELECT *
FROM cities;
```
## PIVOT on one column
```{sql}
#| connection: con
PIVOT cities
ON year
USING sum(population)
GROUP BY country;
```
## PIVOT on two column
```{sql}
#| connection: con
PIVOT cities
ON country, name
USING sum(population);
```
# unpivot
wide to long
```{sql}
#| connection: con
UNPIVOT
(
PIVOT cities
ON year
USING sum(population)
GROUP BY country
)
ON COLUMNS(* EXCLUDE (country))
INTO
NAME year
VALUE population;
;
```
# EXPLAIN
```{sql}
#| eval: false
#| connection: con
EXPLAIN
SELECT t1.model_name,t1.mpg ,t1.cyl,t2.* from mtcars_table t1
inner join (select * from mtcars_table limit 5) t2 on t1.model_name = t2.model_name
;
```
```{sql}
#| eval: false
#| connection: con
EXPLAIN ANALYZE
SELECT t1.model_name,t1.mpg ,t1.cyl,t2.* from mtcars_table t1
inner join (select * from mtcars_table limit 5) t2 on t1.model_name = t2.model_name
;
```
```{r}
dbDisconnect(con, shutdown=TRUE)
```
# Display beautiful table
in R and Python using GT package
::: panel-tabset
## R
```{r}
library(gt)
mtcars |> gt()
```
## Python
```{python}
from great_tables import GT
GT(mtcars)
```
:::
# reference:
https://duckdb.org/docs/stable/clients/r.html
https://duckdb.org/docs/stable/sql/statements