我正在尝试产生类似于K-top示例的内容。
除了要筛选出并显示相同的汇总字段数据外,我要:
我已经创建了一个可观察的笔记本here来构建测试用例,这就是我能走多远了。
{
"$schema": "https://vega.github.io/schema/vega-lite/v4.json",
"data": {"url": "data/seattle-weather.csv"},
"transform": [
{"timeUnit": "month", "field": "date", "as": "month_date"},
{
"joinaggregate": [
{"op": "mean", "field": "precipitation", "as": "mean_precipitation"},
{"op": "max", "field": "precipitation", "as": "max_precipitation"}
],
"groupby": ["month_date"]
},
{
"aggregate": [
{"as": "aggregation", "field": "precipitation", "op": "mean"}
],
"groupby": ["month_date"]
},
{"window": [{"op": "row_number", "as": "rank"}]},
{"calculate": "datum.rank <= 100? datum.month_date : null", "as": "dates"},
{"filter": "datum.dates != null"}
],
"encoding": {
"x": {"field": "dates", "type": "ordinal", "timeUnit": "month"}
},
"layer": [
{
"mark": {"type": "bar"},
"encoding": {
"y": {
"aggregate": "max",
"field": "precipitation",
"type": "quantitative"
}
}
},
{
"mark": "tick",
"encoding": {
"y": {
"aggregate": "mean",
"field": "precipitation",
"type": "quantitative"
},
"color": {"value": "red"},
"size": {"value": 15}
}
}
]
}
我觉得我缺少从GroupBy.ngroup
到pandas.DataFrame
的链接
答案 0 :(得分:1)
您可以按照Vega-Lite的Filtering Top-K Items示例进行此操作,并进行额外的聚合转换。这是一个从上方(vega editor)修改规格的示例:
{
"$schema": "https://vega.github.io/schema/vega-lite/v4.json",
"title": "Top Months by Mean Precipitation",
"data": {"url": "data/seattle-weather.csv"},
"transform": [
{"timeUnit": "month", "field": "date", "as": "month_date"},
{
"aggregate": [
{"op": "mean", "field": "precipitation", "as": "mean_precipitation"},
{"op": "max", "field": "precipitation", "as": "max_precipitation"}
],
"groupby": ["month_date"]
},
{
"window": [{"op": "row_number", "as": "rank"}],
"sort": [{"field": "mean_precipitation", "order": "descending"}]
},
{"filter": "datum.rank < 10"}
],
"encoding": {
"x": {
"field": "month_date",
"type": "ordinal",
"timeUnit": "month",
"title": "month (descending by max precip)",
"sort": {
"field": "max_precipitation",
"op": "average",
"order": "descending"
}
}
},
"layer": [
{
"mark": {"type": "bar"},
"encoding": {
"y": {
"field": "mean_precipitation",
"type": "quantitative",
"title": "precipitation (mean & max)"
}
}
},
{
"mark": "tick",
"encoding": {
"y": {"field": "max_precipitation", "type": "quantitative"},
"color": {"value": "red"},
"size": {"value": 15}
}
}
]
}