Polars-readme-2.py
· 1.2 KiB · Python
原始文件
>>> df = pl.scan_csv("docs/assets/data/iris.csv")
>>> ## OPTION 1
>>> # run SQL queries on frame-level
>>> df.sql("""
... SELECT species,
... AVG(sepal_length) AS avg_sepal_length
... FROM self
... GROUP BY species
... """).collect()
shape: (3, 2)
┌────────────┬──────────────────┐
│ species ┆ avg_sepal_length │
│ --- ┆ --- │
│ str ┆ f64 │
╞════════════╪══════════════════╡
│ Virginica ┆ 6.588 │
│ Versicolor ┆ 5.936 │
│ Setosa ┆ 5.006 │
└────────────┴──────────────────┘
>>> ## OPTION 2
>>> # use pl.sql() to operate on the global context
>>> df2 = pl.LazyFrame({
... "species": ["Setosa", "Versicolor", "Virginica"],
... "blooming_season": ["Spring", "Summer", "Fall"]
...})
>>> pl.sql("""
... SELECT df.species,
... AVG(df.sepal_length) AS avg_sepal_length,
... df2.blooming_season
... FROM df
... LEFT JOIN df2 ON df.species = df2.species
... GROUP BY df.species, df2.blooming_season
... """).collect()
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() |
10 | shape: (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() |