Altair 数据聚合

Altair data aggregation

我正在尝试在 Altair 上绘制以下内容: x = 每日时间戳 y = 记录数,因为数据是名义上的而非定量的

当我尝试按月或季度汇总 x 时,是否可以将 y 显示为每天的平均记录数而不是总和?

chart = alt.Chart(data).mark_bar().encode(
    x = alt.X('Date:T'),
    y = alt.Y('count(Name):Q'),
    color = alt.Color('descriptor:N')
)
chart

当我尝试做的时候

chart = alt.Chart(data).mark_bar().encode(
    x = alt.X('month(Date):T'),
    y = alt.Y('count(Name):Q'),
    color = alt.Color('descriptor:N')
)
chart

它是按月的总数汇总的,是否可以对每个月的计数进行平均?

您可以使用 Altair 的 transform 语法应用多个聚合。对于您的图表,您可以这样做:

alt.Chart(data).transform_aggregate(
    daily_count = 'count(Name)',
    groupby=['Date', 'descriptor']
).mark_bar().encode(
    x = alt.X('month(Date):O'),
    y = alt.Y('mean(daily_count):Q'),
    color = alt.Color('descriptor:N')
)