Ultima attività 1729150648

Polars-readme-2.py Raw
1>>> df = pl.scan_csv("docs/assets/data/iris.csv")
2>>> ## OPTION 1
3>>> # run SQL queries on frame-level
4>>> df.sql("""
5... SELECT species,
6... AVG(sepal_length) AS avg_sepal_length
7... FROM self
8... GROUP BY species
9... """).collect()
10shape: (3, 2)
11
12 species avg_sepal_length
13 --- ---
14 str f64
15
16 Virginica 6.588
17 Versicolor 5.936
18 Setosa 5.006
19
20>>> ## OPTION 2
21>>> # use pl.sql() to operate on the global context
22>>> df2 = pl.LazyFrame({
23... "species": ["Setosa", "Versicolor", "Virginica"],
24... "blooming_season": ["Spring", "Summer", "Fall"]
25...})
26>>> pl.sql("""
27... SELECT df.species,
28... AVG(df.sepal_length) AS avg_sepal_length,
29... df2.blooming_season
30... FROM df
31... LEFT JOIN df2 ON df.species = df2.species
32... GROUP BY df.species, df2.blooming_season
33... """).collect()