Example logo altair: example

1 2 3 4 5 6 7 8 9

The code from the previous video is shown below.

import pathlib
import pandas as pd
import altair as alt

df = pd.read_csv("birthdays.csv")

def clean_dataset(dataf):
    return (dataf
            .assign(date = lambda d: pd.to_datetime(d['date']))
            .assign(yday = lambda d: d['date'].dt.dayofyear)
            .groupby(['date', 'wday', 'yday'])
            .agg(births = ('births', 'sum'), month=('month', 'first'))
            .reset_index()
            .head(1000))

plot_df = df.pipe(clean_dataset)

First Example of Altair

To turn this dataset into an interactive chart, run the example code below.

(alt.Chart(plot_df)
  .mark_point(color='black')
  .encode(x='yday', y='births')
  .properties(width=600, height=300)
  .interactive())