Skip to content

Instantly share code, notes, and snippets.

View pandas_read_csv_parse.py
>>> df = pd.read_csv("dates_text.csv", parse_dates=["date"])
>>> df.dtypes
date datetime64[ns]
category object
balance int64
dtype: object
View pandas_dates_read_csv.py
>>> import pandas as pd
>>> df = pd.read_csv("dates_text.csv")
>>> df.dtypes
date object
category object
balance int64
dtype: object
View pywebio_input_examples.py
from pywebio import input as pin
from pywebio import output as pout
pout.put_code("""pin.input("What's your favorite fruit?")""")
pin.input("What's your favorite fruit?")
pout.put_code("""pin.slider("How old are you?", min_value=0, max_value=120)""")
pin.slider("How old are you?", min_value=0, max_value=120)
pout.put_code("""pin.checkbox("What toppings for pizza?", options=["pepperoni", "sausage", "chicken"])""")
View sns_set_theme.py
# what you run:
sns.set_theme()
# the actual function signature:
set_theme(context="notebook", style="darkgrid", palette="deep",
font="sans-serif", font_scale=1, color_codes=True, rc=None)
View for_loop_break.py
def any_like_fruit_break(fruits, liked_fruit):
liked = False
for fruit in fruits:
print(f"Checking {fruit} likable")
if fruit == liked_fruit:
liked = True
break
print("Found liked")
return liked
View for_loop_no_break.py
def any_liked_fruit(fruits, liked_fruit):
liked = False
for fruit in fruits:
print(f"Checking {fruit} likable")
if not liked and fruit == liked_fruit:
liked = True
print("Found liked")
return liked
View pandas_combine.py
>>> def taking_larger_square(s1, s2):
... return s1 * s1 if s1.sum() > s2.sum() else s2 * s2
>>> df0.combine(df1.rename(columns={"c": "a", "d": "b"}), taking_larger_square)
a b
0 4 25
1 9 36
2 16 49
View pandas_data_dfs.py
>>> import pandas as pd
>>> df0 = pd.DataFrame({"a": [1, 2, 3], "b": [4, 5, 6]})
>>> df0
a b
0 1 4
1 2 5
2 3 6
>>> df1 = pd.DataFrame({"c": [2, 3, 4], "d": [5, 6, 7]})
>>> df1
c d
View function_async_used.py
>>> import asyncio
>>> async def login_user_async():
... await asyncio.sleep(2)
...
>>> async def load_posts_async():
... await asyncio.sleep(3)
...
>>> async def launch_app_async():
... print("Open App:", time.asctime())
... await asyncio.gather(
View async_function_not_used.py
>>> import time
>>> def login_user():
... time.sleep(2)
...
>>> def load_posts():
... time.sleep(3)
...
>>> def launch_app():
... print("Open App:", time.asctime())
... login_user()