Composite Rank
Composite ranking across multiple metrics.
Ranks each metric column (ascending or descending per column), then aggregates the
per-column ranks — not the raw values — into a composite_rank column (lower is
better overall).
CompositeRank
Creates composite rankings by aggregating per-metric ranks.
Ranks are 1-based with rank 1 = best in the given sort direction, so a lower
composite_rank is better overall. Tied values share the lowest rank (1, 2, 2, 4). Nulls
rank last, except in an "asc" column on BigQuery, which ranks them first (best).
composite_rank aggregates the per-column ranks, not the raw values:
mean = average rank (a float), sum = total rank, min = best single-metric rank, max =
worst single-metric rank. Rows come back in no particular order; sort on
composite_rank yourself. .table is a lazy ibis Table; .df materializes on first
access and is cached.
Source code in openretailscience/analysis/composite_rank.py
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 | |
df
cached
property
Materialized ranked data (computed lazily on first access, then cached).
Returns:
| Type | Description |
|---|---|
DataFrame
|
pd.DataFrame: The input metrics plus one |
__init__(df, rank_cols, agg_func, ignore_ties=False, group_col=None)
Initialize a CompositeRank over df.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
DataFrame | Table
|
Data to rank. |
required |
rank_cols
|
list[tuple[str, str] | str]
|
Metrics to rank with their sort direction. Each entry is a column name (defaults to "asc") or a (column, sort_order) tuple; sort_order is "asc", "ascending", "desc", or "descending" (case-insensitive). |
required |
agg_func
|
str
|
How to combine the per-column ranks: "mean", "sum", "min" or "max". |
required |
ignore_ties
|
bool
|
If True, every row gets a unique rank and ties are broken arbitrarily, so tied rows can swap between runs. Defaults to False. |
False
|
group_col
|
str | list[str]
|
Column(s) to partition the ranking by. None (default) ranks globally; otherwise ranks are computed independently within each group. |
None
|
Raises:
| Type | Description |
|---|---|
TypeError
|
If |
ValueError
|
If |
ValueError
|
If a specified metric is not in the data or the sort order is invalid. |
ValueError
|
If the aggregation function is not supported. |
ValueError
|
If |
Examples:
>>> # Group-based ranking: each product competes with its own category
>>> ranker = CompositeRank(
... df=product_data,
... rank_cols=[("weekly_sales", "desc"), ("margin_percentage", "desc"), ("stock_cover_days", "asc")],
... agg_func="mean",
... group_col="product_category",
... )