altair logo altair: recipe

1 2 3 4 5 6 7 8 9

Don't forget to first have your dataframe ready when you make charts with altair.

import pathlib
import pandas as pd
import altair as alt

df = pd.read_csv("content/data/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)
            .drop(columns=['Unnamed: 0'])
            .groupby(['date', 'wday', 'yday'])
            .agg(births = ('births', 'sum'), month=('month', 'first'))

plot_df = df.pipe(clean_dataset)

The code for the plot is listed below;

  .encode(x='yday', y='births')
  .properties(width=600, height=300)