Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions narwhals-python/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Writing DataFrame-Agnostic Python Code With Narwhals

This folder contains the downloadable code from the Real Python tutorial: [Writing DataFrame-Agnostic Python Code With Narwhals](https://realpython.com/python-narwhals/).

| File | Description |
|--------------------------- |----------------------------------------------------- |
| `authors.parquet` | Data for main sections of the tutorial |
| `books.parquet` | Data for the self study section of the tutorial |
| `presidents.parquet` | Data for the self study section of the tutorial |
| `exercise_solution.py` | A possible solution to the self study exercise |
| `universal_processing.py` | Functions used in the main sections of the tutorial |





Binary file added narwhals-python/authors.parquet
Binary file not shown.
Binary file added narwhals-python/books.parquet
Binary file not shown.
17 changes: 17 additions & 0 deletions narwhals-python/exercise_solution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import narwhals as nw
from narwhals.typing import IntoFrameT


def rowling_books(df: IntoFrameT, lf: IntoFrameT) -> IntoFrameT:
return (
nw.from_native(df)
.join(
nw.from_native(lf)
.filter(nw.col("last_name").str.contains("Rowling"))
.collect(),
on="author_id",
)
.select(["book_title", "year_published", "first_name", "last_name"])
.sort("year_published")
.to_pandas()
)
Binary file added narwhals-python/presidents.parquet
Binary file not shown.
56 changes: 56 additions & 0 deletions narwhals-python/universal_processing.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import narwhals as nw
from narwhals.typing import FrameT, IntoFrameT


def universal_groupby_v1(df: IntoFrameT) -> IntoFrameT:
return (
nw.from_native(df)
.group_by("party_name")
.agg(nw.col("last_name").count())
.sort("party_name")
.to_native()
)


@nw.narwhalify
def universal_groupby_v2(df: FrameT) -> FrameT:
return (
df.group_by("party_name")
.agg(nw.col("last_name").count())
.sort("party_name")
)


def universal_groupby_v3(df: IntoFrameT) -> IntoFrameT:
return (
nw.from_native(df)
.group_by("party_name")
.agg(nw.col("last_name").count())
.sort("party_name")
.to_polars()
)


def universal_pivot_v1(df: IntoFrameT) -> IntoFrameT:
return (
nw.from_native(df)
.pivot(
on="party_name",
index="century",
values="last_name",
aggregate_function="count",
)
.to_native()
)


def universal_pivot_v2(df: IntoFrameT) -> IntoFrameT:
df = nw.from_native(df)
if isinstance(df, nw.LazyFrame):
df = df.collect()
return df.pivot(
on="party_name",
index="century",
values="last_name",
aggregate_function="count",
).to_native()