Filter Utilities
filter_extreme
admiralpy.filter_extreme.filter_extreme(dataset, order, mode, by_vars=None, check_type='warning')
Filter the first or last observation (per by group) from a dataset.
For each group defined by by_vars, the observations are sorted by
order and the first or last record is retained. If by_vars is
None, the entire dataset is treated as a single group.
Mirrors filter_extreme() from the admiral R package.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dataset
|
DataFrame
|
Input dataset. |
required |
order
|
list of str
|
Column names used for sorting within each group. Prefix a column
name with |
required |
mode
|
(first, last)
|
Whether to retain the |
"first"
|
by_vars
|
list of str
|
Grouping variables. If |
None
|
check_type
|
(none, warning, error)
|
Uniqueness check. If |
"none"
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
Dataset containing only the selected (first or last) records. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
ValueError
|
If any column in |
RuntimeError
|
If |
Examples:
>>> import pandas as pd
>>> from admiralpy import filter_extreme
>>> advs = pd.DataFrame({
... "USUBJID": ["P01", "P01", "P01"],
... "PARAMCD": ["MAP", "MAP", "MAP"],
... "AVISITN": [0, 2, 4],
... "AVAL": [93.0, 91.5, 94.0],
... })
>>> filter_extreme(
... advs,
... by_vars=["USUBJID"],
... order=["AVISITN", "PARAMCD"],
... mode="last",
... )
Source code in admiralpy/filter_extreme.py
12 13 14 15 16 17 18 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 | |