Skip to content

Commit 0f55452

Browse files
eyrei123bzaczynski
andauthored
Materials for Narwhals (#719)
* Initial Commit * Line length issues * line length * Fix aggregate_function in pivot calls Reversal of `values and `aggregate_function` parameters * Update universal_processing.py Missing comma * Update the README file * Update code * Update README --------- Co-authored-by: Bartosz Zaczyński <[email protected]>
1 parent 2309431 commit 0f55452

File tree

6 files changed

+84
-0
lines changed

6 files changed

+84
-0
lines changed

narwhals-python/README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Writing DataFrame-Agnostic Python Code With Narwhals
2+
3+
This folder contains the downloadable code from the Real Python tutorial: [Writing DataFrame-Agnostic Python Code With Narwhals](https://realpython.com/narwhals-python/).
4+
5+
| File | Description |
6+
|---------------------------|-----------------------------------------------------|
7+
| `presidents.parquet` | Data for main sections of the tutorial |
8+
| `universal_processing.py` | Functions used in the main sections of the tutorial |
9+
| `authors.parquet` | Data for the self study section of the tutorial |
10+
| `books.parquet` | Data for the self study section of the tutorial |
11+
| `exercise_solution.py` | A possible solution to the self study exercise |

narwhals-python/authors.parquet

2.73 KB
Binary file not shown.

narwhals-python/books.parquet

4.13 KB
Binary file not shown.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import narwhals as nw
2+
from narwhals.typing import IntoFrameT
3+
4+
5+
def rowling_books(df: IntoFrameT, lf: IntoFrameT) -> IntoFrameT:
6+
return (
7+
nw.from_native(df)
8+
.join(
9+
nw.from_native(lf)
10+
.filter(nw.col("last_name").str.contains("Rowling"))
11+
.collect(),
12+
on="author_id",
13+
)
14+
.select(["book_title", "year_published", "first_name", "last_name"])
15+
.sort("year_published")
16+
.to_pandas()
17+
)

narwhals-python/presidents.parquet

3.32 KB
Binary file not shown.
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import narwhals as nw
2+
from narwhals.typing import FrameT, IntoFrameT
3+
4+
5+
def universal_groupby_v1(df: IntoFrameT) -> IntoFrameT:
6+
return (
7+
nw.from_native(df)
8+
.group_by("party_name")
9+
.agg(nw.col("last_name").count())
10+
.sort("party_name")
11+
.to_native()
12+
)
13+
14+
15+
@nw.narwhalify
16+
def universal_groupby_v2(df: FrameT) -> FrameT:
17+
return (
18+
df.group_by("party_name")
19+
.agg(nw.col("last_name").count())
20+
.sort("party_name")
21+
)
22+
23+
24+
def universal_groupby_v3(df: IntoFrameT) -> IntoFrameT:
25+
return (
26+
nw.from_native(df)
27+
.group_by("party_name")
28+
.agg(nw.col("last_name").count())
29+
.sort("party_name")
30+
.to_polars()
31+
)
32+
33+
34+
def universal_pivot_v1(df: IntoFrameT) -> IntoFrameT:
35+
return (
36+
nw.from_native(df)
37+
.pivot(
38+
on="party_name",
39+
index="century",
40+
values="last_name",
41+
aggregate_function="count",
42+
)
43+
.to_native()
44+
)
45+
46+
47+
def universal_pivot_v2(df: IntoFrameT) -> IntoFrameT:
48+
df = nw.from_native(df)
49+
if isinstance(df, nw.LazyFrame):
50+
df = df.collect()
51+
return df.pivot(
52+
on="party_name",
53+
index="century",
54+
values="last_name",
55+
aggregate_function="count",
56+
).to_native()

0 commit comments

Comments
 (0)