Skip to content

Commit 6d05830

Browse files
committed
Initial Commit
1 parent bc04c40 commit 6d05830

File tree

6 files changed

+89
-0
lines changed

6 files changed

+89
-0
lines changed

narwhals-python/README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
This file contains the downloadable code from the RealPython tutorial: \[Writing DataFrame Agnostic Python Code With Narwhals](https://realpython.com/dataframe-agnostic-code-with-narwhals-python/)
2+
3+
4+
5+
authors.parquet - File of data for main sections of the tutorial.
6+
7+
books.parquet - File of data for the self study section of the tutorial.
8+
9+
presidents.parquet - File of data for the self study section of the tutorial.
10+
11+
12+
13+
exercise\_solution.py - File containing a possible solution to the self study exercise.
14+
15+
universal\_processing.py - File containing functions used in the main sections of the tutorial.
16+
17+
18+
19+
20+

narwhals-python/authors.parquet

2.73 KB
Binary file not shown.

narwhals-python/books.parquet

4.13 KB
Binary file not shown.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
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(nw.from_native(lf).filter(nw.col("last_name").str.contains("Rowling")).collect(), on="author_id")
9+
.select(["book_title", "year_published", "first_name", "last_name"])
10+
.sort("year_published")
11+
.to_pandas()
12+
)

narwhals-python/presidents.parquet

3.32 KB
Binary file not shown.
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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+
@nw.narwhalify
15+
def universal_groupby_v2(df: FrameT) -> FrameT:
16+
return (
17+
df
18+
.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+
aggregate_function="count",
41+
values="last_name"
42+
).to_native()
43+
)
44+
45+
46+
def universal_pivot_v2(df: IntoFrameT) -> IntoFrameT:
47+
if isinstance(nw.from_native(df), nw.LazyFrame):
48+
df = nw.from_native(df).collect()
49+
return (
50+
nw.from_native(df)
51+
.pivot(
52+
on="party_name",
53+
index="century",
54+
aggregate_function="count",
55+
values="last_name"
56+
).to_native()
57+
)

0 commit comments

Comments
 (0)