import random
class Game:
def __init__(self):
self.lower_limit = 0
self.upper_limit = 200
self.number = random.randrange(self.lower_limit, self.upper_limit)
self.is_debug = False
self.running = True
def debug(self):
self.is_debug = not self.is_debug
def guess(self, num):
if num == 'd':
self.debug()
return
if self.is_debug:
print("Hidden number {}. Your guess is {}".format(self.number, num))
if num < self.number:
print("Too small")
elif num > self.number:
print("Too big")
else:
print("Bingo")
self.running = False
g = Game()
g.guess('d')
try:
g.guess('z')
except Exception as e:
print(e)
try:
g.guess('201')
except Exception as e:
print(e)
try:
g.guess('-1')
except Exception as e:
print(e)
Hidden number 137. Your guess is z
Not a Number z
Hidden number 137. Your guess is 201
Number 201 is too big
Hidden number 137. Your guess is -1
Number -1 is too small
import random
class SpaceShipError(Exception):
def __init__(self, inp):
self.inp = inp
class NumberTooBigError(SpaceShipError):
def __str__(self):
return "Number {} is too big".format(self.inp)
class NumberTooSmallError(SpaceShipError):
def __str__(self):
return "Number {} is too small".format(self.inp)
class NotANumberError(SpaceShipError):
def __str__(self):
return "Not a Number {}".format(self.inp)
class Game:
def __init__(self):
self.lower_limit = 0
self.upper_limit = 200
self.number = random.randrange(self.lower_limit, self.upper_limit)
self.is_debug = False
self.running = True
def debug(self):
self.is_debug = not self.is_debug
def guess(self, num):
if num == 'd':
self.debug()
return
if self.is_debug:
print("Hidden number {}. Your guess is {}".format(self.number, num))
try:
num = int(num)
except Exception:
raise NotANumberError(num)
if num > self.upper_limit:
raise NumberTooBigError(num)
if num < self.upper_limit:
raise NumberTooSmallError(num)
if num < self.number:
print("Too small")
elif num > self.number:
print("Too big")
else:
print("Bingo")
self.running = False
g = Game()
g.guess('d')
try:
g.guess('z')
except Exception as e:
print(e)
try:
g.guess('201')
except Exception as e:
print(e)
try:
g.guess('-1')
except Exception as e:
print(e)
#while g.running:
# guess = input("Please type in your guess: ")
# g.guess(int(guess))
This will run if there was no exception at all
Always executes. 6/2 ended.
Always executes. 6/0 ended.
Always executes. 6/a ended.
Traceback (most recent call last):
File "try.py", line 22, in <module>
main()
File "try.py", line 9, in main
divide(cnt, num)
File "try.py", line 3, in divide
return x/y
TypeError: unsupported operand type(s) for /: 'int' and 'str'
def divide(x, y):
return x/y
def main():
cnt = 6
for num in [2, 0, 'a']:
try:
divide(cnt, num)
except ZeroDivisionError:
pass
except (IOError, MemoryError) as err:
print(err)
else:
print("This will run if there was no exception at all")
finally:
print("Always executes. {}/{} ended.".format(cnt, num))
print("done")
main()
1
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz
11
Fizz
13
14
FizzBuzz
16
17
Fizz
19
Buzz
Fizz
22
23
Fizz
Buzz
26
Fizz
28
29
FizzBuzz
31
32
Fizz
34
Buzz
Fizz
37
38
Fizz
Buzz
41
Fizz
43
44
FizzBuzz
46
47
Fizz
49
Buzz
Fizz
52
53
Fizz
Buzz
56
Fizz
58
59
FizzBuzz
61
62
Fizz
64
Buzz
Fizz
67
68
Fizz
Buzz
71
Fizz
73
74
FizzBuzz
76
77
Fizz
79
Buzz
Fizz
82
83
Fizz
Buzz
86
Fizz
88
89
FizzBuzz
91
92
Fizz
94
Buzz
Fizz
97
98
Fizz
Buzz
def fizzbuzz():
for i in range(1, 101):
if i % 15 == 0:
print("FizzBuzz")
continue
if i % 3 == 0:
print("Fizz")
continue
if i % 5 == 0:
print("Buzz")
continue
print(i)
if __name__ == "__main__":
fizzbuzz()
import fb
def test_fb(capsys):
fb.fizzbuzz()
out, err = capsys.readouterr()
assert err == ''
with open('expected.txt') as fh:
expected = fh.read()
assert out == expected
import sys
import os
import time
if len(sys.argv) != 3:
exit(f"Usage: {sys.argv[0]} FILENAME count")
filename, count = sys.argv[1:]
print(f"start {os.getpid()}")
time.sleep(1)
for _ in range(int(count)):
try:
if not os.path.exists(filename):
with open(filename, 'w') as fh:
fh.write("0\n")
with open(filename, 'r') as fh:
number = int(fh.readline())
number += 1
with open(filename, 'w') as fh:
#fh.seek(0,0)
fh.write(f"{number}\n")
except Exception:
pass
print(f"done {os.getpid()}")
import sys
import os
import time
if len(sys.argv) != 3:
exit(f"Usage: {sys.argv[0]} FILENAME count")
filename, count = sys.argv[1:]
print(f"start {os.getpid()}")
time.sleep(1)
for _ in range(int(count)):
#try:
if not os.path.exists(filename):
with open(filename, 'w') as fh:
fh.write("0\n")
with open(filename, 'r+') as fh:
number = int(fh.readline())
number += 1
fh.seek(0,0)
fh.write(f"{number}\n")
# with open(filename, 'w') as fh:
# fh.write(f"{number}\n")
#except Exception:
# pass
print(f"done {os.getpid()}")
import subprocess
import sys
if len(sys.argv) != 4:
exit(f"Usage: {sys.argv[0]} FILENAME count processes")
filename, count, process_count = sys.argv[1:]
command = [sys.executable, 'count.py', filename, count]
processes = []
for _ in range(int(process_count)):
processes.append(subprocess.Popen(command))
print('Started')
for proc in processes:
proc.communicate()
print('Done')
name,birthdate,weight,height
Alice Archer,1997-01-10,57.9,1.56
Ben Brown,1985-02-15,72.5,1.77
Chloe Cooper,1983-03-22,53.6,1.65
Daniel Donovan,1981-04-30,83.1,1.75
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"id": "bc5909c3-0e34-46b7-af44-d8b59bbd1817",
"metadata": {},
"outputs": [],
"source": [
"!pip install polars"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "0422d14b-b3d5-407b-8594-31633a056594",
"metadata": {},
"outputs": [],
"source": [
"import polars as pl\n",
"import datetime as dt"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "ef584243-839b-47a3-a34a-52b5d8d5d4c2",
"metadata": {},
"outputs": [],
"source": [
"df = pl.DataFrame(\n",
" {\n",
" \"name\": [\"Alice Archer\", \"Ben Brown\", \"Chloe Cooper\", \"Daniel Donovan\"],\n",
" \"birthdate\": [\n",
" dt.date(1997, 1, 10),\n",
" dt.date(1985, 2, 15),\n",
" dt.date(1983, 3, 22),\n",
" dt.date(1981, 4, 30),\n",
" ],\n",
" \"weight\": [57.9, 72.5, 53.6, 83.1], # (kg)\n",
" \"height\": [1.56, 1.77, 1.65, 1.75], # (m)\n",
" }\n",
")\n",
"\n",
"print(df)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "2943f6c6-2989-4b66-ac3f-37f218d578bb",
"metadata": {},
"outputs": [],
"source": [
"df"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "7904a22a-aced-4618-951c-80afaeaf7ba5",
"metadata": {},
"outputs": [],
"source": [
"dir(df)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "6cb1363a-0706-4fb8-9ce1-284c2ce14720",
"metadata": {},
"outputs": [],
"source": [
"df.head(2)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "832f56a2-a543-4db5-ab41-be2c8cfa989a",
"metadata": {},
"outputs": [],
"source": [
"df.tail(2)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a0ffa702-8f7e-488d-9175-82a2ae9c8738",
"metadata": {},
"outputs": [],
"source": [
"df.describe()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "362dc935-407a-44be-bbc9-1a7111b852ba",
"metadata": {},
"outputs": [],
"source": [
"df.write_csv(\"getting_started.csv\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "7bfde105-95dc-4b3f-9763-1f2e132b9b0e",
"metadata": {},
"outputs": [],
"source": [
"df_csv = pl.read_csv(\"getting_started.csv\", try_parse_dates=True)\n",
"print(df_csv)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "70a7e566-33e1-471d-a71d-f7e645787355",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "58e1ecba-46f7-47f6-ad33-f708f07ee28d",
"metadata": {},
"outputs": [],
"source": [
"result = df.select(\n",
" pl.col(\"name\"),\n",
" (pl.col(\"weight\", \"height\") * 0.95).round(2).name.suffix(\"-5%\"),\n",
")\n",
"print(result)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "39d7eecf-dea3-409d-b904-5b260de8ca7d",
"metadata": {},
"outputs": [],
"source": [
"result = df.with_columns(\n",
" birth_year=pl.col(\"birthdate\").dt.year(),\n",
" bmi=pl.col(\"weight\") / (pl.col(\"height\") ** 2),\n",
")\n",
"print(result)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "dbfce594-67a9-47d4-bdc1-5488f532163d",
"metadata": {},
"outputs": [],
"source": [
"result = df.filter(pl.col(\"birthdate\").dt.year() < 1990)\n",
"print(result)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "1870d98c-d320-4fd3-8f03-4f43c8958a7c",
"metadata": {},
"outputs": [],
"source": [
"result = df.filter(\n",
" pl.col(\"birthdate\").is_between(dt.date(1982, 12, 31), dt.date(1996, 1, 1)),\n",
" pl.col(\"height\") < 1.7,\n",
")\n",
"print(result)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "ac8411e7-5f8a-46ad-aca4-83892af2b80b",
"metadata": {},
"outputs": [],
"source": [
"result = df.group_by(\n",
" (pl.col(\"birthdate\").dt.year() // 10 * 10).alias(\"decade\"),\n",
" maintain_order=True,\n",
").len()\n",
"print(result)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "be3c0d55-c16c-4cda-8d04-c0fb56007833",
"metadata": {},
"outputs": [],
"source": [
"result = df.group_by(\n",
" (pl.col(\"birthdate\").dt.year() // 10 * 10).alias(\"decade\"),\n",
" maintain_order=True,\n",
").agg(\n",
" pl.len().alias(\"sample_size\"),\n",
" pl.col(\"weight\").mean().round(2).alias(\"avg_weight\"),\n",
" pl.col(\"height\").max().alias(\"tallest\"),\n",
")\n",
"print(result)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "29c60262-5dbf-49d6-b27f-fd81e11068fb",
"metadata": {},
"outputs": [],
"source": [
"result = (\n",
" df.with_columns(\n",
" (pl.col(\"birthdate\").dt.year() // 10 * 10).alias(\"decade\"),\n",
" pl.col(\"name\").str.split(by=\" \").list.first(),\n",
" )\n",
" .select(\n",
" pl.all().exclude(\"birthdate\"),\n",
" )\n",
" .group_by(\n",
" pl.col(\"decade\"),\n",
" maintain_order=True,\n",
" )\n",
" .agg(\n",
" pl.col(\"name\"),\n",
" pl.col(\"weight\", \"height\").mean().round(2).name.prefix(\"avg_\"),\n",
" )\n",
")\n",
"print(result)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "fe9d8375-97a1-40f1-8c17-7b4e2994059e",
"metadata": {},
"outputs": [],
"source": [
"df2 = pl.DataFrame(\n",
" {\n",
" \"name\": [\"Ben Brown\", \"Daniel Donovan\", \"Alice Archer\", \"Chloe Cooper\"],\n",
" \"parent\": [True, False, False, False],\n",
" \"siblings\": [1, 2, 3, 4],\n",
" }\n",
")\n",
"print(df2)\n",
"print(df.join(df2, on=\"name\", how=\"left\"))"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "477e99a9-a13a-457c-bcb2-2f45154a86f7",
"metadata": {},
"outputs": [],
"source": [
"df3 = pl.DataFrame(\n",
" {\n",
" \"name\": [\"Ethan Edwards\", \"Fiona Foster\", \"Grace Gibson\", \"Henry Harris\"],\n",
" \"birthdate\": [\n",
" dt.date(1977, 5, 10),\n",
" dt.date(1975, 6, 23),\n",
" dt.date(1973, 7, 22),\n",
" dt.date(1971, 8, 3),\n",
" ],\n",
" \"weight\": [67.9, 72.5, 57.6, 93.1], # (kg)\n",
" \"height\": [1.76, 1.6, 1.66, 1.8], # (m)\n",
" }\n",
")\n",
"print(df3)\n",
"print(pl.concat([df, df3], how=\"vertical\"))"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.7"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Id,SepalLengthCm,SepalWidthCm,PetalLengthCm,PetalWidthCm,Species
1,5.1,3.5,1.4,0.2,Iris-setosa
2,4.9,3.0,1.4,0.2,Iris-setosa
3,4.7,3.2,1.3,0.2,Iris-setosa
4,4.6,3.1,1.5,0.2,Iris-setosa
5,5.0,3.6,1.4,0.2,Iris-setosa
6,5.4,3.9,1.7,0.4,Iris-setosa
7,4.6,3.4,1.4,0.3,Iris-setosa
8,5.0,3.4,1.5,0.2,Iris-setosa
9,4.4,2.9,1.4,0.2,Iris-setosa
10,4.9,3.1,1.5,0.1,Iris-setosa
11,5.4,3.7,1.5,0.2,Iris-setosa
12,4.8,3.4,1.6,0.2,Iris-setosa
13,4.8,3.0,1.4,0.1,Iris-setosa
14,4.3,3.0,1.1,0.1,Iris-setosa
15,5.8,4.0,1.2,0.2,Iris-setosa
16,5.7,4.4,1.5,0.4,Iris-setosa
17,5.4,3.9,1.3,0.4,Iris-setosa
18,5.1,3.5,1.4,0.3,Iris-setosa
19,5.7,3.8,1.7,0.3,Iris-setosa
20,5.1,3.8,1.5,0.3,Iris-setosa
21,5.4,3.4,1.7,0.2,Iris-setosa
22,5.1,3.7,1.5,0.4,Iris-setosa
23,4.6,3.6,1.0,0.2,Iris-setosa
24,5.1,3.3,1.7,0.5,Iris-setosa
25,4.8,3.4,1.9,0.2,Iris-setosa
26,5.0,3.0,1.6,0.2,Iris-setosa
27,5.0,3.4,1.6,0.4,Iris-setosa
28,5.2,3.5,1.5,0.2,Iris-setosa
29,5.2,3.4,1.4,0.2,Iris-setosa
30,4.7,3.2,1.6,0.2,Iris-setosa
31,4.8,3.1,1.6,0.2,Iris-setosa
32,5.4,3.4,1.5,0.4,Iris-setosa
33,5.2,4.1,1.5,0.1,Iris-setosa
34,5.5,4.2,1.4,0.2,Iris-setosa
35,4.9,3.1,1.5,0.1,Iris-setosa
36,5.0,3.2,1.2,0.2,Iris-setosa
37,5.5,3.5,1.3,0.2,Iris-setosa
38,4.9,3.1,1.5,0.1,Iris-setosa
39,4.4,3.0,1.3,0.2,Iris-setosa
40,5.1,3.4,1.5,0.2,Iris-setosa
41,5.0,3.5,1.3,0.3,Iris-setosa
42,4.5,2.3,1.3,0.3,Iris-setosa
43,4.4,3.2,1.3,0.2,Iris-setosa
44,5.0,3.5,1.6,0.6,Iris-setosa
45,5.1,3.8,1.9,0.4,Iris-setosa
46,4.8,3.0,1.4,0.3,Iris-setosa
47,5.1,3.8,1.6,0.2,Iris-setosa
48,4.6,3.2,1.4,0.2,Iris-setosa
49,5.3,3.7,1.5,0.2,Iris-setosa
50,5.0,3.3,1.4,0.2,Iris-setosa
51,7.0,3.2,4.7,1.4,Iris-versicolor
52,6.4,3.2,4.5,1.5,Iris-versicolor
53,6.9,3.1,4.9,1.5,Iris-versicolor
54,5.5,2.3,4.0,1.3,Iris-versicolor
55,6.5,2.8,4.6,1.5,Iris-versicolor
56,5.7,2.8,4.5,1.3,Iris-versicolor
57,6.3,3.3,4.7,1.6,Iris-versicolor
58,4.9,2.4,3.3,1.0,Iris-versicolor
59,6.6,2.9,4.6,1.3,Iris-versicolor
60,5.2,2.7,3.9,1.4,Iris-versicolor
61,5.0,2.0,3.5,1.0,Iris-versicolor
62,5.9,3.0,4.2,1.5,Iris-versicolor
63,6.0,2.2,4.0,1.0,Iris-versicolor
64,6.1,2.9,4.7,1.4,Iris-versicolor
65,5.6,2.9,3.6,1.3,Iris-versicolor
66,6.7,3.1,4.4,1.4,Iris-versicolor
67,5.6,3.0,4.5,1.5,Iris-versicolor
68,5.8,2.7,4.1,1.0,Iris-versicolor
69,6.2,2.2,4.5,1.5,Iris-versicolor
70,5.6,2.5,3.9,1.1,Iris-versicolor
71,5.9,3.2,4.8,1.8,Iris-versicolor
72,6.1,2.8,4.0,1.3,Iris-versicolor
73,6.3,2.5,4.9,1.5,Iris-versicolor
74,6.1,2.8,4.7,1.2,Iris-versicolor
75,6.4,2.9,4.3,1.3,Iris-versicolor
76,6.6,3.0,4.4,1.4,Iris-versicolor
77,6.8,2.8,4.8,1.4,Iris-versicolor
78,6.7,3.0,5.0,1.7,Iris-versicolor
79,6.0,2.9,4.5,1.5,Iris-versicolor
80,5.7,2.6,3.5,1.0,Iris-versicolor
81,5.5,2.4,3.8,1.1,Iris-versicolor
82,5.5,2.4,3.7,1.0,Iris-versicolor
83,5.8,2.7,3.9,1.2,Iris-versicolor
84,6.0,2.7,5.1,1.6,Iris-versicolor
85,5.4,3.0,4.5,1.5,Iris-versicolor
86,6.0,3.4,4.5,1.6,Iris-versicolor
87,6.7,3.1,4.7,1.5,Iris-versicolor
88,6.3,2.3,4.4,1.3,Iris-versicolor
89,5.6,3.0,4.1,1.3,Iris-versicolor
90,5.5,2.5,4.0,1.3,Iris-versicolor
91,5.5,2.6,4.4,1.2,Iris-versicolor
92,6.1,3.0,4.6,1.4,Iris-versicolor
93,5.8,2.6,4.0,1.2,Iris-versicolor
94,5.0,2.3,3.3,1.0,Iris-versicolor
95,5.6,2.7,4.2,1.3,Iris-versicolor
96,5.7,3.0,4.2,1.2,Iris-versicolor
97,5.7,2.9,4.2,1.3,Iris-versicolor
98,6.2,2.9,4.3,1.3,Iris-versicolor
99,5.1,2.5,3.0,1.1,Iris-versicolor
100,5.7,2.8,4.1,1.3,Iris-versicolor
101,6.3,3.3,6.0,2.5,Iris-virginica
102,5.8,2.7,5.1,1.9,Iris-virginica
103,7.1,3.0,5.9,2.1,Iris-virginica
104,6.3,2.9,5.6,1.8,Iris-virginica
105,6.5,3.0,5.8,2.2,Iris-virginica
106,7.6,3.0,6.6,2.1,Iris-virginica
107,4.9,2.5,4.5,1.7,Iris-virginica
108,7.3,2.9,6.3,1.8,Iris-virginica
109,6.7,2.5,5.8,1.8,Iris-virginica
110,7.2,3.6,6.1,2.5,Iris-virginica
111,6.5,3.2,5.1,2.0,Iris-virginica
112,6.4,2.7,5.3,1.9,Iris-virginica
113,6.8,3.0,5.5,2.1,Iris-virginica
114,5.7,2.5,5.0,2.0,Iris-virginica
115,5.8,2.8,5.1,2.4,Iris-virginica
116,6.4,3.2,5.3,2.3,Iris-virginica
117,6.5,3.0,5.5,1.8,Iris-virginica
118,7.7,3.8,6.7,2.2,Iris-virginica
119,7.7,2.6,6.9,2.3,Iris-virginica
120,6.0,2.2,5.0,1.5,Iris-virginica
121,6.9,3.2,5.7,2.3,Iris-virginica
122,5.6,2.8,4.9,2.0,Iris-virginica
123,7.7,2.8,6.7,2.0,Iris-virginica
124,6.3,2.7,4.9,1.8,Iris-virginica
125,6.7,3.3,5.7,2.1,Iris-virginica
126,7.2,3.2,6.0,1.8,Iris-virginica
127,6.2,2.8,4.8,1.8,Iris-virginica
128,6.1,3.0,4.9,1.8,Iris-virginica
129,6.4,2.8,5.6,2.1,Iris-virginica
130,7.2,3.0,5.8,1.6,Iris-virginica
131,7.4,2.8,6.1,1.9,Iris-virginica
132,7.9,3.8,6.4,2.0,Iris-virginica
133,6.4,2.8,5.6,2.2,Iris-virginica
134,6.3,2.8,5.1,1.5,Iris-virginica
135,6.1,2.6,5.6,1.4,Iris-virginica
136,7.7,3.0,6.1,2.3,Iris-virginica
137,6.3,3.4,5.6,2.4,Iris-virginica
138,6.4,3.1,5.5,1.8,Iris-virginica
139,6.0,3.0,4.8,1.8,Iris-virginica
140,6.9,3.1,5.4,2.1,Iris-virginica
141,6.7,3.1,5.6,2.4,Iris-virginica
142,6.9,3.1,5.1,2.3,Iris-virginica
143,5.8,2.7,5.1,1.9,Iris-virginica
144,6.8,3.2,5.9,2.3,Iris-virginica
145,6.7,3.3,5.7,2.5,Iris-virginica
146,6.7,3.0,5.2,2.3,Iris-virginica
147,6.3,2.5,5.0,1.9,Iris-virginica
148,6.5,3.0,5.2,2.0,Iris-virginica
149,6.2,3.4,5.4,2.3,Iris-virginica
150,5.9,3.0,5.1,1.8,Iris-virginica
{
"cells": [
{
"cell_type": "markdown",
"id": "f90cdfdc-bc05-4cdc-b900-022517b33b41",
"metadata": {},
"source": [
"[Iris flower data set](https://en.wikipedia.org/wiki/Iris_flower_data_set) with [Polars](https://pola.rs/)"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "fd4ec3fc-c2d8-4a50-bc9c-99cafefb9647",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Collecting polars\n",
" Downloading polars-1.20.0-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.metadata (14 kB)\n",
"Downloading polars-1.20.0-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (32.9 MB)\n",
"\u001b[2K \u001b[38;2;114;156;31m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m32.9/32.9 MB\u001b[0m \u001b[31m37.2 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0mm eta \u001b[36m0:00:01\u001b[0m[36m0:00:01\u001b[0m\n",
"\u001b[?25hInstalling collected packages: polars\n",
"Successfully installed polars-1.20.0\n"
]
}
],
"source": [
"!pip install polars"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "2f2387a7-a2aa-4c66-812d-aff6814fc4e9",
"metadata": {},
"outputs": [],
"source": [
"import polars as pl"
]
},
{
"cell_type": "code",
"execution_count": 13,
"id": "dfdd0b3a-ae63-49cb-80a0-a1a4fab76366",
"metadata": {},
"outputs": [],
"source": [
"df = pl.scan_csv(\"iris.csv\").collect()"
]
},
{
"cell_type": "code",
"execution_count": 14,
"id": "070e59b6-6d06-43d9-b2b6-1edff05059d6",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div><style>\n",
".dataframe > thead > tr,\n",
".dataframe > tbody > tr {\n",
" text-align: right;\n",
" white-space: pre-wrap;\n",
"}\n",
"</style>\n",
"<small>shape: (150, 6)</small><table border=\"1\" class=\"dataframe\"><thead><tr><th>Id</th><th>SepalLengthCm</th><th>SepalWidthCm</th><th>PetalLengthCm</th><th>PetalWidthCm</th><th>Species</th></tr><tr><td>i64</td><td>f64</td><td>f64</td><td>f64</td><td>f64</td><td>str</td></tr></thead><tbody><tr><td>1</td><td>5.1</td><td>3.5</td><td>1.4</td><td>0.2</td><td>"Iris-setosa"</td></tr><tr><td>2</td><td>4.9</td><td>3.0</td><td>1.4</td><td>0.2</td><td>"Iris-setosa"</td></tr><tr><td>3</td><td>4.7</td><td>3.2</td><td>1.3</td><td>0.2</td><td>"Iris-setosa"</td></tr><tr><td>4</td><td>4.6</td><td>3.1</td><td>1.5</td><td>0.2</td><td>"Iris-setosa"</td></tr><tr><td>5</td><td>5.0</td><td>3.6</td><td>1.4</td><td>0.2</td><td>"Iris-setosa"</td></tr><tr><td>…</td><td>…</td><td>…</td><td>…</td><td>…</td><td>…</td></tr><tr><td>146</td><td>6.7</td><td>3.0</td><td>5.2</td><td>2.3</td><td>"Iris-virginica"</td></tr><tr><td>147</td><td>6.3</td><td>2.5</td><td>5.0</td><td>1.9</td><td>"Iris-virginica"</td></tr><tr><td>148</td><td>6.5</td><td>3.0</td><td>5.2</td><td>2.0</td><td>"Iris-virginica"</td></tr><tr><td>149</td><td>6.2</td><td>3.4</td><td>5.4</td><td>2.3</td><td>"Iris-virginica"</td></tr><tr><td>150</td><td>5.9</td><td>3.0</td><td>5.1</td><td>1.8</td><td>"Iris-virginica"</td></tr></tbody></table></div>"
],
"text/plain": [
"shape: (150, 6)\n",
"┌─────┬───────────────┬──────────────┬───────────────┬──────────────┬────────────────┐\n",
"│ Id ┆ SepalLengthCm ┆ SepalWidthCm ┆ PetalLengthCm ┆ PetalWidthCm ┆ Species │\n",
"│ --- ┆ --- ┆ --- ┆ --- ┆ --- ┆ --- │\n",
"│ i64 ┆ f64 ┆ f64 ┆ f64 ┆ f64 ┆ str │\n",
"╞═════╪═══════════════╪══════════════╪═══════════════╪══════════════╪════════════════╡\n",
"│ 1 ┆ 5.1 ┆ 3.5 ┆ 1.4 ┆ 0.2 ┆ Iris-setosa │\n",
"│ 2 ┆ 4.9 ┆ 3.0 ┆ 1.4 ┆ 0.2 ┆ Iris-setosa │\n",
"│ 3 ┆ 4.7 ┆ 3.2 ┆ 1.3 ┆ 0.2 ┆ Iris-setosa │\n",
"│ 4 ┆ 4.6 ┆ 3.1 ┆ 1.5 ┆ 0.2 ┆ Iris-setosa │\n",
"│ 5 ┆ 5.0 ┆ 3.6 ┆ 1.4 ┆ 0.2 ┆ Iris-setosa │\n",
"│ … ┆ … ┆ … ┆ … ┆ … ┆ … │\n",
"│ 146 ┆ 6.7 ┆ 3.0 ┆ 5.2 ┆ 2.3 ┆ Iris-virginica │\n",
"│ 147 ┆ 6.3 ┆ 2.5 ┆ 5.0 ┆ 1.9 ┆ Iris-virginica │\n",
"│ 148 ┆ 6.5 ┆ 3.0 ┆ 5.2 ┆ 2.0 ┆ Iris-virginica │\n",
"│ 149 ┆ 6.2 ┆ 3.4 ┆ 5.4 ┆ 2.3 ┆ Iris-virginica │\n",
"│ 150 ┆ 5.9 ┆ 3.0 ┆ 5.1 ┆ 1.8 ┆ Iris-virginica │\n",
"└─────┴───────────────┴──────────────┴───────────────┴──────────────┴────────────────┘"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df"
]
},
{
"cell_type": "code",
"execution_count": 15,
"id": "d72c9b12-0a65-4aa3-9490-646018d49940",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"polars.dataframe.frame.DataFrame"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"type(df)"
]
},
{
"cell_type": "code",
"execution_count": 16,
"id": "13aad9e3-76fb-4b26-9c29-e30b0dbc6410",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['__add__',\n",
" '__annotations__',\n",
" '__array__',\n",
" '__arrow_c_stream__',\n",
" '__bool__',\n",
" '__class__',\n",
" '__contains__',\n",
" '__copy__',\n",
" '__dataframe__',\n",
" '__deepcopy__',\n",
" '__delattr__',\n",
" '__dict__',\n",
" '__dir__',\n",
" '__doc__',\n",
" '__eq__',\n",
" '__floordiv__',\n",
" '__format__',\n",
" '__ge__',\n",
" '__getattribute__',\n",
" '__getitem__',\n",
" '__getstate__',\n",
" '__gt__',\n",
" '__hash__',\n",
" '__init__',\n",
" '__init_subclass__',\n",
" '__iter__',\n",
" '__le__',\n",
" '__len__',\n",
" '__lt__',\n",
" '__mod__',\n",
" '__module__',\n",
" '__mul__',\n",
" '__ne__',\n",
" '__new__',\n",
" '__radd__',\n",
" '__reduce__',\n",
" '__reduce_ex__',\n",
" '__repr__',\n",
" '__reversed__',\n",
" '__rmul__',\n",
" '__setattr__',\n",
" '__setitem__',\n",
" '__setstate__',\n",
" '__sizeof__',\n",
" '__str__',\n",
" '__sub__',\n",
" '__subclasshook__',\n",
" '__truediv__',\n",
" '__weakref__',\n",
" '_accessors',\n",
" '_cast_all_from_to',\n",
" '_comp',\n",
" '_compare_to_non_df',\n",
" '_compare_to_other_df',\n",
" '_df',\n",
" '_div',\n",
" '_from_arrow',\n",
" '_from_pandas',\n",
" '_from_pydf',\n",
" '_ipython_key_completions_',\n",
" '_replace',\n",
" '_repr_html_',\n",
" '_row_encode',\n",
" '_to_metadata',\n",
" '_to_pandas_with_object_columns',\n",
" '_to_pandas_without_object_columns',\n",
" 'approx_n_unique',\n",
" 'bottom_k',\n",
" 'cast',\n",
" 'clear',\n",
" 'clone',\n",
" 'collect_schema',\n",
" 'columns',\n",
" 'corr',\n",
" 'count',\n",
" 'describe',\n",
" 'deserialize',\n",
" 'drop',\n",
" 'drop_in_place',\n",
" 'drop_nans',\n",
" 'drop_nulls',\n",
" 'dtypes',\n",
" 'equals',\n",
" 'estimated_size',\n",
" 'explode',\n",
" 'extend',\n",
" 'fill_nan',\n",
" 'fill_null',\n",
" 'filter',\n",
" 'flags',\n",
" 'fold',\n",
" 'gather_every',\n",
" 'get_column',\n",
" 'get_column_index',\n",
" 'get_columns',\n",
" 'glimpse',\n",
" 'group_by',\n",
" 'group_by_dynamic',\n",
" 'hash_rows',\n",
" 'head',\n",
" 'height',\n",
" 'hstack',\n",
" 'insert_column',\n",
" 'interpolate',\n",
" 'is_duplicated',\n",
" 'is_empty',\n",
" 'is_unique',\n",
" 'item',\n",
" 'iter_columns',\n",
" 'iter_rows',\n",
" 'iter_slices',\n",
" 'join',\n",
" 'join_asof',\n",
" 'join_where',\n",
" 'lazy',\n",
" 'limit',\n",
" 'map_rows',\n",
" 'max',\n",
" 'max_horizontal',\n",
" 'mean',\n",
" 'mean_horizontal',\n",
" 'median',\n",
" 'melt',\n",
" 'merge_sorted',\n",
" 'min',\n",
" 'min_horizontal',\n",
" 'n_chunks',\n",
" 'n_unique',\n",
" 'null_count',\n",
" 'partition_by',\n",
" 'pipe',\n",
" 'pivot',\n",
" 'plot',\n",
" 'product',\n",
" 'quantile',\n",
" 'rechunk',\n",
" 'rename',\n",
" 'replace_column',\n",
" 'reverse',\n",
" 'rolling',\n",
" 'row',\n",
" 'rows',\n",
" 'rows_by_key',\n",
" 'sample',\n",
" 'schema',\n",
" 'select',\n",
" 'select_seq',\n",
" 'serialize',\n",
" 'set_sorted',\n",
" 'shape',\n",
" 'shift',\n",
" 'shrink_to_fit',\n",
" 'slice',\n",
" 'sort',\n",
" 'sql',\n",
" 'std',\n",
" 'style',\n",
" 'sum',\n",
" 'sum_horizontal',\n",
" 'tail',\n",
" 'to_arrow',\n",
" 'to_dict',\n",
" 'to_dicts',\n",
" 'to_dummies',\n",
" 'to_init_repr',\n",
" 'to_jax',\n",
" 'to_numpy',\n",
" 'to_pandas',\n",
" 'to_series',\n",
" 'to_struct',\n",
" 'to_torch',\n",
" 'top_k',\n",
" 'transpose',\n",
" 'unique',\n",
" 'unnest',\n",
" 'unpivot',\n",
" 'unstack',\n",
" 'update',\n",
" 'upsample',\n",
" 'var',\n",
" 'vstack',\n",
" 'width',\n",
" 'with_columns',\n",
" 'with_columns_seq',\n",
" 'with_row_count',\n",
" 'with_row_index',\n",
" 'write_avro',\n",
" 'write_clipboard',\n",
" 'write_csv',\n",
" 'write_database',\n",
" 'write_delta',\n",
" 'write_excel',\n",
" 'write_ipc',\n",
" 'write_ipc_stream',\n",
" 'write_json',\n",
" 'write_ndjson',\n",
" 'write_parquet']"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"dir(df)"
]
},
{
"cell_type": "code",
"execution_count": 19,
"id": "8c126b37-4a9b-4582-b8e9-5c7fe45afedc",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['Id',\n",
" 'SepalLengthCm',\n",
" 'SepalWidthCm',\n",
" 'PetalLengthCm',\n",
" 'PetalWidthCm',\n",
" 'Species']"
]
},
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.columns"
]
},
{
"cell_type": "code",
"execution_count": 21,
"id": "f0e0d26b-5b26-4127-809e-3d132a7c5cdd",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div><style>\n",
".dataframe > thead > tr,\n",
".dataframe > tbody > tr {\n",
" text-align: right;\n",
" white-space: pre-wrap;\n",
"}\n",
"</style>\n",
"<small>shape: (3, 6)</small><table border=\"1\" class=\"dataframe\"><thead><tr><th>Id</th><th>SepalLengthCm</th><th>SepalWidthCm</th><th>PetalLengthCm</th><th>PetalWidthCm</th><th>Species</th></tr><tr><td>i64</td><td>f64</td><td>f64</td><td>f64</td><td>f64</td><td>str</td></tr></thead><tbody><tr><td>1</td><td>5.1</td><td>3.5</td><td>1.4</td><td>0.2</td><td>"Iris-setosa"</td></tr><tr><td>2</td><td>4.9</td><td>3.0</td><td>1.4</td><td>0.2</td><td>"Iris-setosa"</td></tr><tr><td>3</td><td>4.7</td><td>3.2</td><td>1.3</td><td>0.2</td><td>"Iris-setosa"</td></tr></tbody></table></div>"
],
"text/plain": [
"shape: (3, 6)\n",
"┌─────┬───────────────┬──────────────┬───────────────┬──────────────┬─────────────┐\n",
"│ Id ┆ SepalLengthCm ┆ SepalWidthCm ┆ PetalLengthCm ┆ PetalWidthCm ┆ Species │\n",
"│ --- ┆ --- ┆ --- ┆ --- ┆ --- ┆ --- │\n",
"│ i64 ┆ f64 ┆ f64 ┆ f64 ┆ f64 ┆ str │\n",
"╞═════╪═══════════════╪══════════════╪═══════════════╪══════════════╪═════════════╡\n",
"│ 1 ┆ 5.1 ┆ 3.5 ┆ 1.4 ┆ 0.2 ┆ Iris-setosa │\n",
"│ 2 ┆ 4.9 ┆ 3.0 ┆ 1.4 ┆ 0.2 ┆ Iris-setosa │\n",
"│ 3 ┆ 4.7 ┆ 3.2 ┆ 1.3 ┆ 0.2 ┆ Iris-setosa │\n",
"└─────┴───────────────┴──────────────┴───────────────┴──────────────┴─────────────┘"
]
},
"execution_count": 21,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.head(3)"
]
},
{
"cell_type": "code",
"execution_count": 22,
"id": "2991a8d6-52cd-4ae0-b66c-6ae5f5f0476c",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div><style>\n",
".dataframe > thead > tr,\n",
".dataframe > tbody > tr {\n",
" text-align: right;\n",
" white-space: pre-wrap;\n",
"}\n",
"</style>\n",
"<small>shape: (2, 6)</small><table border=\"1\" class=\"dataframe\"><thead><tr><th>Id</th><th>SepalLengthCm</th><th>SepalWidthCm</th><th>PetalLengthCm</th><th>PetalWidthCm</th><th>Species</th></tr><tr><td>i64</td><td>f64</td><td>f64</td><td>f64</td><td>f64</td><td>str</td></tr></thead><tbody><tr><td>149</td><td>6.2</td><td>3.4</td><td>5.4</td><td>2.3</td><td>"Iris-virginica"</td></tr><tr><td>150</td><td>5.9</td><td>3.0</td><td>5.1</td><td>1.8</td><td>"Iris-virginica"</td></tr></tbody></table></div>"
],
"text/plain": [
"shape: (2, 6)\n",
"┌─────┬───────────────┬──────────────┬───────────────┬──────────────┬────────────────┐\n",
"│ Id ┆ SepalLengthCm ┆ SepalWidthCm ┆ PetalLengthCm ┆ PetalWidthCm ┆ Species │\n",
"│ --- ┆ --- ┆ --- ┆ --- ┆ --- ┆ --- │\n",
"│ i64 ┆ f64 ┆ f64 ┆ f64 ┆ f64 ┆ str │\n",
"╞═════╪═══════════════╪══════════════╪═══════════════╪══════════════╪════════════════╡\n",
"│ 149 ┆ 6.2 ┆ 3.4 ┆ 5.4 ┆ 2.3 ┆ Iris-virginica │\n",
"│ 150 ┆ 5.9 ┆ 3.0 ┆ 5.1 ┆ 1.8 ┆ Iris-virginica │\n",
"└─────┴───────────────┴──────────────┴───────────────┴──────────────┴────────────────┘"
]
},
"execution_count": 22,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.tail(2)"
]
},
{
"cell_type": "code",
"execution_count": 25,
"id": "91776e1e-1355-47de-b08a-fc76204ba086",
"metadata": {},
"outputs": [],
"source": [
"df = pl.scan_csv(\"iris.csv\").filter(pl.col(\"SepalLengthCm\") > 5).group_by(\"Species\").agg(pl.all().sum()).collect()\n"
]
},
{
"cell_type": "code",
"execution_count": 26,
"id": "75a2a13e-91bc-4005-8347-4244a8669911",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div><style>\n",
".dataframe > thead > tr,\n",
".dataframe > tbody > tr {\n",
" text-align: right;\n",
" white-space: pre-wrap;\n",
"}\n",
"</style>\n",
"<small>shape: (3, 6)</small><table border=\"1\" class=\"dataframe\"><thead><tr><th>Species</th><th>Id</th><th>SepalLengthCm</th><th>SepalWidthCm</th><th>PetalLengthCm</th><th>PetalWidthCm</th></tr><tr><td>str</td><td>i64</td><td>f64</td><td>f64</td><td>f64</td><td>f64</td></tr></thead><tbody><tr><td>"Iris-setosa"</td><td>564</td><td>116.9</td><td>81.7</td><td>33.2</td><td>6.1</td></tr><tr><td>"Iris-versicolor"</td><td>3562</td><td>281.9</td><td>131.8</td><td>202.9</td><td>63.3</td></tr><tr><td>"Iris-virginica"</td><td>6168</td><td>324.5</td><td>146.2</td><td>273.1</td><td>99.6</td></tr></tbody></table></div>"
],
"text/plain": [
"shape: (3, 6)\n",
"┌─────────────────┬──────┬───────────────┬──────────────┬───────────────┬──────────────┐\n",
"│ Species ┆ Id ┆ SepalLengthCm ┆ SepalWidthCm ┆ PetalLengthCm ┆ PetalWidthCm │\n",
"│ --- ┆ --- ┆ --- ┆ --- ┆ --- ┆ --- │\n",
"│ str ┆ i64 ┆ f64 ┆ f64 ┆ f64 ┆ f64 │\n",
"╞═════════════════╪══════╪═══════════════╪══════════════╪═══════════════╪══════════════╡\n",
"│ Iris-setosa ┆ 564 ┆ 116.9 ┆ 81.7 ┆ 33.2 ┆ 6.1 │\n",
"│ Iris-versicolor ┆ 3562 ┆ 281.9 ┆ 131.8 ┆ 202.9 ┆ 63.3 │\n",
"│ Iris-virginica ┆ 6168 ┆ 324.5 ┆ 146.2 ┆ 273.1 ┆ 99.6 │\n",
"└─────────────────┴──────┴───────────────┴──────────────┴───────────────┴──────────────┘"
]
},
"execution_count": 26,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.7"
}
},
"nbformat": 4,
"nbformat_minor": 5
}