{
"cells": [
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"# Dplyr's across: Replicating within Polars"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"[dplyr](https://dplyr.tidyverse.org/index.html) has the [across](https://dplyr.tidyverse.org/reference/across.html) function, which is meant to make column wise processing easy. \n",
"This article aims to replicate solutions in the dplyr [column-wise operations](https://dplyr.tidyverse.org/articles/colwise.html) vignette with Polars."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let's load in the relevant libraries"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" polars version : 0.20.31 \n",
" python version : 3.12.4 | packaged by conda-forge | (main, Jun 17 2024, 10:13:44) [Clang 16.0.6 ]\n"
]
}
],
"source": [
"import polars as pl\n",
"import polars.selectors as cs\n",
"import sys\n",
"\n",
"print(\" polars version :\", pl.__version__, \"\\n\", \"python version :\", sys.version)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"
\n",
"
shape: (5, 14)name | height | mass | hair_color | skin_color | eye_color | birth_year | sex | gender | homeworld | species | films | vehicles | starships |
---|
str | f64 | f64 | str | str | str | f64 | str | str | str | str | str | str | str |
"Luke Skywalker" | 172.0 | 77.0 | "blond" | "fair" | "blue" | 19.0 | "male" | "masculine" | "Tatooine" | "Human" | "A New Hope, The Empire Strikes… | "Snowspeeder, Imperial Speeder … | "X-wing, Imperial shuttle" |
"C-3PO" | 167.0 | 75.0 | null | "gold" | "yellow" | 112.0 | "none" | "masculine" | "Tatooine" | "Droid" | "A New Hope, The Empire Strikes… | null | null |
"R2-D2" | 96.0 | 32.0 | null | "white, blue" | "red" | 33.0 | "none" | "masculine" | "Naboo" | "Droid" | "A New Hope, The Empire Strikes… | null | null |
"Darth Vader" | 202.0 | 136.0 | "none" | "white" | "yellow" | 41.9 | "male" | "masculine" | "Tatooine" | "Human" | "A New Hope, The Empire Strikes… | null | "TIE Advanced x1" |
"Leia Organa" | 150.0 | 49.0 | "brown" | "light" | "brown" | 19.0 | "female" | "feminine" | "Alderaan" | "Human" | "A New Hope, The Empire Strikes… | "Imperial Speeder Bike" | null |
"
],
"text/plain": [
"shape: (5, 14)\n",
"┌─────────────┬────────┬───────┬────────────┬───┬─────────┬─────────────┬─────────────┬────────────┐\n",
"│ name ┆ height ┆ mass ┆ hair_color ┆ … ┆ species ┆ films ┆ vehicles ┆ starships │\n",
"│ --- ┆ --- ┆ --- ┆ --- ┆ ┆ --- ┆ --- ┆ --- ┆ --- │\n",
"│ str ┆ f64 ┆ f64 ┆ str ┆ ┆ str ┆ str ┆ str ┆ str │\n",
"╞═════════════╪════════╪═══════╪════════════╪═══╪═════════╪═════════════╪═════════════╪════════════╡\n",
"│ Luke ┆ 172.0 ┆ 77.0 ┆ blond ┆ … ┆ Human ┆ A New Hope, ┆ Snowspeeder ┆ X-wing, │\n",
"│ Skywalker ┆ ┆ ┆ ┆ ┆ ┆ The Empire ┆ , Imperial ┆ Imperial │\n",
"│ ┆ ┆ ┆ ┆ ┆ ┆ Strikes… ┆ Speeder … ┆ shuttle │\n",
"│ C-3PO ┆ 167.0 ┆ 75.0 ┆ null ┆ … ┆ Droid ┆ A New Hope, ┆ null ┆ null │\n",
"│ ┆ ┆ ┆ ┆ ┆ ┆ The Empire ┆ ┆ │\n",
"│ ┆ ┆ ┆ ┆ ┆ ┆ Strikes… ┆ ┆ │\n",
"│ R2-D2 ┆ 96.0 ┆ 32.0 ┆ null ┆ … ┆ Droid ┆ A New Hope, ┆ null ┆ null │\n",
"│ ┆ ┆ ┆ ┆ ┆ ┆ The Empire ┆ ┆ │\n",
"│ ┆ ┆ ┆ ┆ ┆ ┆ Strikes… ┆ ┆ │\n",
"│ Darth Vader ┆ 202.0 ┆ 136.0 ┆ none ┆ … ┆ Human ┆ A New Hope, ┆ null ┆ TIE │\n",
"│ ┆ ┆ ┆ ┆ ┆ ┆ The Empire ┆ ┆ Advanced │\n",
"│ ┆ ┆ ┆ ┆ ┆ ┆ Strikes… ┆ ┆ x1 │\n",
"│ Leia Organa ┆ 150.0 ┆ 49.0 ┆ brown ┆ … ┆ Human ┆ A New Hope, ┆ Imperial ┆ null │\n",
"│ ┆ ┆ ┆ ┆ ┆ ┆ The Empire ┆ Speeder ┆ │\n",
"│ ┆ ┆ ┆ ┆ ┆ ┆ Strikes… ┆ Bike ┆ │\n",
"└─────────────┴────────┴───────┴────────────┴───┴─────────┴─────────────┴─────────────┴────────────┘"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# https://raw.githubusercontent.com/tidyverse/dplyr/main/data-raw/starwars.csv\n",
"starwars = pl.read_csv('Data_files/starwars.csv')\n",
"starwars.head()"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"
shape: (1, 11)name | hair_color | skin_color | eye_color | sex | gender | homeworld | species | films | vehicles | starships |
---|
u32 | u32 | u32 | u32 | u32 | u32 | u32 | u32 | u32 | u32 | u32 |
87 | 12 | 31 | 15 | 5 | 3 | 49 | 38 | 24 | 11 | 16 |
"
],
"text/plain": [
"shape: (1, 11)\n",
"┌──────┬────────────┬────────────┬───────────┬───┬─────────┬───────┬──────────┬───────────┐\n",
"│ name ┆ hair_color ┆ skin_color ┆ eye_color ┆ … ┆ species ┆ films ┆ vehicles ┆ starships │\n",
"│ --- ┆ --- ┆ --- ┆ --- ┆ ┆ --- ┆ --- ┆ --- ┆ --- │\n",
"│ u32 ┆ u32 ┆ u32 ┆ u32 ┆ ┆ u32 ┆ u32 ┆ u32 ┆ u32 │\n",
"╞══════╪════════════╪════════════╪═══════════╪═══╪═════════╪═══════╪══════════╪═══════════╡\n",
"│ 87 ┆ 12 ┆ 31 ┆ 15 ┆ … ┆ 38 ┆ 24 ┆ 11 ┆ 16 │\n",
"└──────┴────────────┴────────────┴───────────┴───┴─────────┴───────┴──────────┴───────────┘"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# dplyr :\n",
"# starwars %>%\n",
"# summarise(across(where(is.character), n_distinct))\n",
"starwars.select(cs.string().n_unique())"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"
shape: (9, 4)species | sex | gender | homeworld |
---|
str | u32 | u32 | u32 |
"Twi'lek" | 2 | 2 | 1 |
"Mirialan" | 1 | 1 | 1 |
"Droid" | 1 | 2 | 3 |
"Human" | 2 | 2 | 15 |
null | 1 | 1 | 3 |
"Zabrak" | 1 | 1 | 2 |
"Gungan" | 1 | 1 | 1 |
"Wookiee" | 1 | 1 | 1 |
"Kaminoan" | 2 | 2 | 1 |
"
],
"text/plain": [
"shape: (9, 4)\n",
"┌──────────┬─────┬────────┬───────────┐\n",
"│ species ┆ sex ┆ gender ┆ homeworld │\n",
"│ --- ┆ --- ┆ --- ┆ --- │\n",
"│ str ┆ u32 ┆ u32 ┆ u32 │\n",
"╞══════════╪═════╪════════╪═══════════╡\n",
"│ Twi'lek ┆ 2 ┆ 2 ┆ 1 │\n",
"│ Mirialan ┆ 1 ┆ 1 ┆ 1 │\n",
"│ Droid ┆ 1 ┆ 2 ┆ 3 │\n",
"│ Human ┆ 2 ┆ 2 ┆ 15 │\n",
"│ null ┆ 1 ┆ 1 ┆ 3 │\n",
"│ Zabrak ┆ 1 ┆ 1 ┆ 2 │\n",
"│ Gungan ┆ 1 ┆ 1 ┆ 1 │\n",
"│ Wookiee ┆ 1 ┆ 1 ┆ 1 │\n",
"│ Kaminoan ┆ 2 ┆ 2 ┆ 1 │\n",
"└──────────┴─────┴────────┴───────────┘"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# dplyr :\n",
"# starwars %>%\n",
"# group_by(species) %>%\n",
"# filter(n() > 1) %>%\n",
"# summarise(across(c(sex, gender, homeworld), n_distinct))\n",
"(starwars\n",
" .group_by(\"species\")\n",
" .agg(pl.n_unique(\"sex\", \"gender\", \"homeworld\"), \n",
" pl.len().alias(\"n\")\n",
")\n",
".filter(pl.col(\"n\").gt(1))\n",
".select(pl.exclude(\"n\"))\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"
shape: (10, 4)homeworld | height | mass | birth_year |
---|
str | f64 | f64 | f64 |
"Mirial" | 168.0 | 53.1 | 49.0 |
"Alderaan" | 176.333333 | 64.0 | 43.0 |
null | 138.75 | 82.0 | 334.333333 |
"Ryloth" | 179.0 | 55.0 | 48.0 |
"Tatooine" | 169.8 | 85.375 | 54.644444 |
"Kamino" | 208.333333 | 83.1 | 31.5 |
"Corellia" | 175.0 | 78.5 | 25.0 |
"Kashyyyk" | 231.0 | 124.0 | 200.0 |
"Coruscant" | 173.666667 | 50.0 | 91.0 |
"Naboo" | 177.272727 | 64.166667 | 55.0 |
"
],
"text/plain": [
"shape: (10, 4)\n",
"┌───────────┬────────────┬───────────┬────────────┐\n",
"│ homeworld ┆ height ┆ mass ┆ birth_year │\n",
"│ --- ┆ --- ┆ --- ┆ --- │\n",
"│ str ┆ f64 ┆ f64 ┆ f64 │\n",
"╞═══════════╪════════════╪═══════════╪════════════╡\n",
"│ Mirial ┆ 168.0 ┆ 53.1 ┆ 49.0 │\n",
"│ Alderaan ┆ 176.333333 ┆ 64.0 ┆ 43.0 │\n",
"│ null ┆ 138.75 ┆ 82.0 ┆ 334.333333 │\n",
"│ Ryloth ┆ 179.0 ┆ 55.0 ┆ 48.0 │\n",
"│ Tatooine ┆ 169.8 ┆ 85.375 ┆ 54.644444 │\n",
"│ Kamino ┆ 208.333333 ┆ 83.1 ┆ 31.5 │\n",
"│ Corellia ┆ 175.0 ┆ 78.5 ┆ 25.0 │\n",
"│ Kashyyyk ┆ 231.0 ┆ 124.0 ┆ 200.0 │\n",
"│ Coruscant ┆ 173.666667 ┆ 50.0 ┆ 91.0 │\n",
"│ Naboo ┆ 177.272727 ┆ 64.166667 ┆ 55.0 │\n",
"└───────────┴────────────┴───────────┴────────────┘"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# dplyr :\n",
"# starwars %>%\n",
"# group_by(homeworld) %>%\n",
"# filter(n() > 1) %>%\n",
"# summarise(across(where(is.numeric), ~ mean(.x, na.rm = TRUE)))\n",
"(starwars\n",
" .group_by(\"homeworld\")\n",
" .agg(cs.numeric().mean(), pl.len().alias(\"n\"))\n",
" .filter(pl.col(\"n\").gt(1))\n",
".select(pl.exclude(\"n\"))\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"
shape: (67, 3)hair_color | skin_color | eye_color |
---|
str | str | str |
"none" | "grey" | "orange" |
null | "green-tan, brown" | "orange" |
"none" | "metal" | "red" |
"none" | "green" | "yellow" |
"none" | "green, grey" | "unknown" |
… | … | … |
"brown" | "fair" | "blue" |
"brown" | "unknown" | "blue" |
"none" | "blue" | "hazel" |
"brown" | "light" | "brown" |
"white" | "fair" | "blue" |
"
],
"text/plain": [
"shape: (67, 3)\n",
"┌────────────┬──────────────────┬───────────┐\n",
"│ hair_color ┆ skin_color ┆ eye_color │\n",
"│ --- ┆ --- ┆ --- │\n",
"│ str ┆ str ┆ str │\n",
"╞════════════╪══════════════════╪═══════════╡\n",
"│ none ┆ grey ┆ orange │\n",
"│ null ┆ green-tan, brown ┆ orange │\n",
"│ none ┆ metal ┆ red │\n",
"│ none ┆ green ┆ yellow │\n",
"│ none ┆ green, grey ┆ unknown │\n",
"│ … ┆ … ┆ … │\n",
"│ brown ┆ fair ┆ blue │\n",
"│ brown ┆ unknown ┆ blue │\n",
"│ none ┆ blue ┆ hazel │\n",
"│ brown ┆ light ┆ brown │\n",
"│ white ┆ fair ┆ blue │\n",
"└────────────┴──────────────────┴───────────┘"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# dplyr:\n",
"# starwars %>% distinct(across(contains(\"color\")))\n",
"starwars.select(cs.ends_with('color')).unique()"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"
shape: (67, 4)hair_color | skin_color | eye_color | count |
---|
str | str | str | u32 |
"none" | "dark" | "blue" | 1 |
"none" | "blue, grey" | "yellow" | 1 |
"none" | "pale" | "pink" | 1 |
null | "gold" | "yellow" | 1 |
"none" | "blue" | "hazel" | 1 |
… | … | … | … |
"black" | "blue, grey" | "yellow" | 1 |
"none" | "none" | "unknown" | 1 |
"black" | "fair" | "brown" | 2 |
"brown" | "light" | "blue" | 1 |
"white" | "pale" | "yellow" | 1 |
"
],
"text/plain": [
"shape: (67, 4)\n",
"┌────────────┬────────────┬───────────┬───────┐\n",
"│ hair_color ┆ skin_color ┆ eye_color ┆ count │\n",
"│ --- ┆ --- ┆ --- ┆ --- │\n",
"│ str ┆ str ┆ str ┆ u32 │\n",
"╞════════════╪════════════╪═══════════╪═══════╡\n",
"│ none ┆ dark ┆ blue ┆ 1 │\n",
"│ none ┆ blue, grey ┆ yellow ┆ 1 │\n",
"│ none ┆ pale ┆ pink ┆ 1 │\n",
"│ null ┆ gold ┆ yellow ┆ 1 │\n",
"│ none ┆ blue ┆ hazel ┆ 1 │\n",
"│ … ┆ … ┆ … ┆ … │\n",
"│ black ┆ blue, grey ┆ yellow ┆ 1 │\n",
"│ none ┆ none ┆ unknown ┆ 1 │\n",
"│ black ┆ fair ┆ brown ┆ 2 │\n",
"│ brown ┆ light ┆ blue ┆ 1 │\n",
"│ white ┆ pale ┆ yellow ┆ 1 │\n",
"└────────────┴────────────┴───────────┴───────┘"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# dplyr\n",
"# starwars %>% count(across(contains(\"color\")), sort = TRUE)\n",
"(starwars\n",
" .select(pl.struct(cs.ends_with('color')).value_counts())\n",
" .unnest('hair_color')\n",
" .unnest('hair_color')\n",
" )"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"
shape: (87, 14)name | height | mass | hair_color | skin_color | eye_color | birth_year | sex | gender | homeworld | species | films | vehicles | starships |
---|
str | f64 | f64 | str | str | str | f64 | str | str | str | str | str | str | str |
"Luke Skywalker" | 172.0 | 77.0 | "blond" | "fair" | "blue" | 19.0 | "male" | "masculine" | "Tatooine" | "Human" | "A New Hope, The Empire Strikes… | "Snowspeeder, Imperial Speeder … | "X-wing, Imperial shuttle" |
"C-3PO" | 167.0 | 75.0 | null | "gold" | "yellow" | 112.0 | "none" | "masculine" | "Tatooine" | "Droid" | "A New Hope, The Empire Strikes… | null | null |
"R2-D2" | 96.0 | 32.0 | null | "white, blue" | "red" | 33.0 | "none" | "masculine" | "Naboo" | "Droid" | "A New Hope, The Empire Strikes… | null | null |
"Darth Vader" | 202.0 | 136.0 | "none" | "white" | "yellow" | 41.9 | "male" | "masculine" | "Tatooine" | "Human" | "A New Hope, The Empire Strikes… | null | "TIE Advanced x1" |
"Leia Organa" | 150.0 | 49.0 | "brown" | "light" | "brown" | 19.0 | "female" | "feminine" | "Alderaan" | "Human" | "A New Hope, The Empire Strikes… | "Imperial Speeder Bike" | null |
… | … | … | … | … | … | … | … | … | … | … | … | … | … |
"Finn" | null | null | "black" | "dark" | "dark" | null | "male" | "masculine" | null | "Human" | "The Force Awakens" | null | null |
"Rey" | null | null | "brown" | "light" | "hazel" | null | "female" | "feminine" | null | "Human" | "The Force Awakens" | null | null |
"Poe Dameron" | null | null | "brown" | "light" | "brown" | null | "male" | "masculine" | null | "Human" | "The Force Awakens" | null | "X-wing" |
"BB8" | null | null | "none" | "none" | "black" | null | "none" | "masculine" | null | "Droid" | "The Force Awakens" | null | null |
"Captain Phasma" | null | null | "none" | "none" | "unknown" | null | "female" | "feminine" | null | "Human" | "The Force Awakens" | null | null |
"
],
"text/plain": [
"shape: (87, 14)\n",
"┌─────────────┬────────┬───────┬────────────┬───┬─────────┬─────────────┬─────────────┬────────────┐\n",
"│ name ┆ height ┆ mass ┆ hair_color ┆ … ┆ species ┆ films ┆ vehicles ┆ starships │\n",
"│ --- ┆ --- ┆ --- ┆ --- ┆ ┆ --- ┆ --- ┆ --- ┆ --- │\n",
"│ str ┆ f64 ┆ f64 ┆ str ┆ ┆ str ┆ str ┆ str ┆ str │\n",
"╞═════════════╪════════╪═══════╪════════════╪═══╪═════════╪═════════════╪═════════════╪════════════╡\n",
"│ Luke ┆ 172.0 ┆ 77.0 ┆ blond ┆ … ┆ Human ┆ A New Hope, ┆ Snowspeeder ┆ X-wing, │\n",
"│ Skywalker ┆ ┆ ┆ ┆ ┆ ┆ The Empire ┆ , Imperial ┆ Imperial │\n",
"│ ┆ ┆ ┆ ┆ ┆ ┆ Strikes… ┆ Speeder … ┆ shuttle │\n",
"│ C-3PO ┆ 167.0 ┆ 75.0 ┆ null ┆ … ┆ Droid ┆ A New Hope, ┆ null ┆ null │\n",
"│ ┆ ┆ ┆ ┆ ┆ ┆ The Empire ┆ ┆ │\n",
"│ ┆ ┆ ┆ ┆ ┆ ┆ Strikes… ┆ ┆ │\n",
"│ R2-D2 ┆ 96.0 ┆ 32.0 ┆ null ┆ … ┆ Droid ┆ A New Hope, ┆ null ┆ null │\n",
"│ ┆ ┆ ┆ ┆ ┆ ┆ The Empire ┆ ┆ │\n",
"│ ┆ ┆ ┆ ┆ ┆ ┆ Strikes… ┆ ┆ │\n",
"│ Darth Vader ┆ 202.0 ┆ 136.0 ┆ none ┆ … ┆ Human ┆ A New Hope, ┆ null ┆ TIE │\n",
"│ ┆ ┆ ┆ ┆ ┆ ┆ The Empire ┆ ┆ Advanced │\n",
"│ ┆ ┆ ┆ ┆ ┆ ┆ Strikes… ┆ ┆ x1 │\n",
"│ Leia Organa ┆ 150.0 ┆ 49.0 ┆ brown ┆ … ┆ Human ┆ A New Hope, ┆ Imperial ┆ null │\n",
"│ ┆ ┆ ┆ ┆ ┆ ┆ The Empire ┆ Speeder ┆ │\n",
"│ ┆ ┆ ┆ ┆ ┆ ┆ Strikes… ┆ Bike ┆ │\n",
"│ … ┆ … ┆ … ┆ … ┆ … ┆ … ┆ … ┆ … ┆ … │\n",
"│ Finn ┆ null ┆ null ┆ black ┆ … ┆ Human ┆ The Force ┆ null ┆ null │\n",
"│ ┆ ┆ ┆ ┆ ┆ ┆ Awakens ┆ ┆ │\n",
"│ Rey ┆ null ┆ null ┆ brown ┆ … ┆ Human ┆ The Force ┆ null ┆ null │\n",
"│ ┆ ┆ ┆ ┆ ┆ ┆ Awakens ┆ ┆ │\n",
"│ Poe Dameron ┆ null ┆ null ┆ brown ┆ … ┆ Human ┆ The Force ┆ null ┆ X-wing │\n",
"│ ┆ ┆ ┆ ┆ ┆ ┆ Awakens ┆ ┆ │\n",
"│ BB8 ┆ null ┆ null ┆ none ┆ … ┆ Droid ┆ The Force ┆ null ┆ null │\n",
"│ ┆ ┆ ┆ ┆ ┆ ┆ Awakens ┆ ┆ │\n",
"│ Captain ┆ null ┆ null ┆ none ┆ … ┆ Human ┆ The Force ┆ null ┆ null │\n",
"│ Phasma ┆ ┆ ┆ ┆ ┆ ┆ Awakens ┆ ┆ │\n",
"└─────────────┴────────┴───────┴────────────┴───┴─────────┴─────────────┴─────────────┴────────────┘"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# starwars %>%\n",
"# filter(if_any(everything(), ~ !is.na(.x)))\n",
"starwars.filter(pl.any_horizontal(pl.all().is_not_null()))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let's look at another solution, from [Stack Overflow](https://stackoverflow.com/questions/63200530/python-pandas-equivalent-to-dplyr-1-0-0-summarizeacross):"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"
shape: (5, 12)model | mpg | cyl | disp | hp | drat | wt | qsec | vs | am | gear | carb |
---|
str | f64 | i64 | f64 | i64 | f64 | f64 | f64 | i64 | i64 | i64 | i64 |
"Mazda RX4" | 21.0 | 6 | 160.0 | 110 | 3.9 | 2.62 | 16.46 | 0 | 1 | 4 | 4 |
"Mazda RX4 Wag" | 21.0 | 6 | 160.0 | 110 | 3.9 | 2.875 | 17.02 | 0 | 1 | 4 | 4 |
"Datsun 710" | 22.8 | 4 | 108.0 | 93 | 3.85 | 2.32 | 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.44 | 17.02 | 0 | 0 | 3 | 2 |
"
],
"text/plain": [
"shape: (5, 12)\n",
"┌───────────────────┬──────┬─────┬───────┬───┬─────┬─────┬──────┬──────┐\n",
"│ model ┆ mpg ┆ cyl ┆ disp ┆ … ┆ vs ┆ am ┆ gear ┆ carb │\n",
"│ --- ┆ --- ┆ --- ┆ --- ┆ ┆ --- ┆ --- ┆ --- ┆ --- │\n",
"│ str ┆ f64 ┆ i64 ┆ f64 ┆ ┆ i64 ┆ i64 ┆ i64 ┆ i64 │\n",
"╞═══════════════════╪══════╪═════╪═══════╪═══╪═════╪═════╪══════╪══════╡\n",
"│ Mazda RX4 ┆ 21.0 ┆ 6 ┆ 160.0 ┆ … ┆ 0 ┆ 1 ┆ 4 ┆ 4 │\n",
"│ Mazda RX4 Wag ┆ 21.0 ┆ 6 ┆ 160.0 ┆ … ┆ 0 ┆ 1 ┆ 4 ┆ 4 │\n",
"│ Datsun 710 ┆ 22.8 ┆ 4 ┆ 108.0 ┆ … ┆ 1 ┆ 1 ┆ 4 ┆ 1 │\n",
"│ Hornet 4 Drive ┆ 21.4 ┆ 6 ┆ 258.0 ┆ … ┆ 1 ┆ 0 ┆ 3 ┆ 1 │\n",
"│ Hornet Sportabout ┆ 18.7 ┆ 8 ┆ 360.0 ┆ … ┆ 0 ┆ 0 ┆ 3 ┆ 2 │\n",
"└───────────────────┴──────┴─────┴───────┴───┴─────┴─────┴──────┴──────┘"
]
},
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# \"https://gist.githubusercontent.com/seankross/a412dfbd88b3db70b74b/raw/5f23f993cd87c283ce766e7ac6b329ee7cc2e1d1/mtcars.csv\"\n",
"cars = pl.read_csv('Data_files/cars.csv')\n",
"cars.head()"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"
shape: (3, 5)cyl | disp | hp | drat | wt |
---|
i64 | f64 | f64 | f64 | f64 |
6 | 183.314286 | 122.285714 | 25.1 | 21.82 |
4 | 105.136364 | 82.636364 | 44.78 | 25.143 |
8 | 353.1 | 209.214286 | 45.21 | 55.989 |
"
],
"text/plain": [
"shape: (3, 5)\n",
"┌─────┬────────────┬────────────┬───────┬────────┐\n",
"│ cyl ┆ disp ┆ hp ┆ drat ┆ wt │\n",
"│ --- ┆ --- ┆ --- ┆ --- ┆ --- │\n",
"│ i64 ┆ f64 ┆ f64 ┆ f64 ┆ f64 │\n",
"╞═════╪════════════╪════════════╪═══════╪════════╡\n",
"│ 6 ┆ 183.314286 ┆ 122.285714 ┆ 25.1 ┆ 21.82 │\n",
"│ 4 ┆ 105.136364 ┆ 82.636364 ┆ 44.78 ┆ 25.143 │\n",
"│ 8 ┆ 353.1 ┆ 209.214286 ┆ 45.21 ┆ 55.989 │\n",
"└─────┴────────────┴────────────┴───────┴────────┘"
]
},
"execution_count": 20,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# dplyr:\n",
"# dat <- group_by(mtcars, cyl)\n",
"# summarize(dat, across(ends_with('p'), sum), across(ends_with('t'), mean))\n",
"cars.group_by('cyl').agg(cs.ends_with('p').mean(), cs.ends_with('t').sum())"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Comments\n",
""
]
}
],
"metadata": {
"jupytext": {
"formats": "ipynb,md"
},
"kernelspec": {
"display_name": "blogger",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.14"
}
},
"nbformat": 4,
"nbformat_minor": 4
}