Merged Variables
derive_vars_merged
admiralpy.derive_vars_merged.derive_vars_merged(dataset, dataset_add, by_vars, new_vars=None, filter_add=None, order=None, mode=None, relationship='many-to-one')
Add variable(s) from an additional dataset to the input dataset.
For each observation in dataset, the matching record(s) in
dataset_add are identified using by_vars. If order and
mode are specified, only the first or last matching record is used.
The variables listed in new_vars are then merged into dataset.
Mirrors derive_vars_merged() from the admiral R package.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dataset
|
DataFrame
|
Input dataset. |
required |
dataset_add
|
DataFrame
|
Additional dataset providing the new variables. Must contain all
columns listed in |
required |
by_vars
|
list of str
|
Variables used to match observations across the two datasets. |
required |
new_vars
|
dict
|
Mapping of If |
None
|
filter_add
|
str or callable
|
Condition applied to
|
None
|
order
|
list of str
|
Column names used to sort |
None
|
mode
|
(first, last)
|
When |
"first"
|
relationship
|
str
|
Merge cardinality. Kept for API compatibility. Default is
|
'many-to-one'
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
Input dataset with the new variable(s) appended. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Examples:
>>> import pandas as pd
>>> from admiralpy import derive_vars_merged
>>> adsl = pd.DataFrame({"USUBJID": ["P01", "P02"], "AGE": [30, 40]})
>>> ex = pd.DataFrame({
... "USUBJID": ["P01", "P01", "P02"],
... "EXSTDTC": ["2020-01-01", "2020-02-01", "2020-01-15"],
... "EXSEQ": [1, 2, 1],
... })
>>> derive_vars_merged(
... adsl, ex,
... by_vars=["USUBJID"],
... new_vars={"TRTSTDTC": "EXSTDTC"},
... order=["EXSEQ"],
... mode="first",
... )
Source code in admiralpy/derive_vars_merged.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 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 | |