Keys
toolbox_pyspark.keys
🔗
Summary
The keys
module is used for creating new columns to act as keys (primary and foreign), to be used for joins with other tables, or to create relationships within downstream applications, like PowerBI.
add_key_from_columns
🔗
add_key_from_columns(
dataframe: psDataFrame,
columns: Union[str, str_collection],
join_character: Optional[str] = "_",
key_name: Optional[str] = None,
) -> psDataFrame
Summary
Using a list of column names, add a new column which is a combination of all of them.
Details
This is a combine key, and is especially important because PowerBI cannot handle joins on multiple columns.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dataframe
|
DataFrame
|
The table to be updated. |
required |
columns
|
Union[str, str_collection]
|
The columns to be combined. |
required |
join_character
|
Optional[str]
|
The character to use to combine the columns together. |
'_'
|
key_name
|
Optional[str]
|
The name of the column to be given to the key.
If not provided, it will form as the capitalised string of all the other column names, prefixed with |
None
|
Raises:
Type | Description |
---|---|
TypeError
|
If any of the inputs parsed to the parameters of this function are not the correct type. Uses the |
ColumnDoesNotExistError
|
If any of the |
Returns:
Type | Description |
---|---|
DataFrame
|
The updated |
Examples
Set up | |
---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
|
+---+---+---+---+
| a | b | c | d |
+---+---+---+---+
| 1 | a | 1 | 2 |
| 2 | b | 1 | 2 |
| 3 | c | 1 | 2 |
| 4 | d | 1 | 2 |
+---+---+---+---+
Example 1: Basic usage | |
---|---|
1 2 |
|
+---+---+---+---+---------+
| a | b | c | d | key_A_B |
+---+---+---+---+---------+
| 1 | a | 1 | 2 | 1_a |
| 2 | b | 1 | 2 | 2_b |
| 3 | c | 1 | 2 | 3_c |
| 4 | d | 1 | 2 | 4_d |
+---+---+---+---+---------+
Conclusion: Successfully added new key column to DataFrame.
Example 2: Single column | |
---|---|
1 2 |
|
+---+---+---+---+-------+
| a | b | c | d | key_A |
+---+---+---+---+-------+
| 1 | a | 1 | 2 | 1 |
| 2 | b | 1 | 2 | 2 |
| 3 | c | 1 | 2 | 3 |
| 4 | d | 1 | 2 | 4 |
+---+---+---+---+-------+
Conclusion: Successfully added new key column to DataFrame.
Example 3: New name | |
---|---|
1 2 |
|
+---+---+---+---+---------+
| a | b | c | d | new_key |
+---+---+---+---+---------+
| 1 | a | 1 | 2 | 1_a |
| 2 | b | 1 | 2 | 2_b |
| 3 | c | 1 | 2 | 3_c |
| 4 | d | 1 | 2 | 4_d |
+---+---+---+---+---------+
Conclusion: Successfully added new key column to DataFrame.
Example 4: Raise error | |
---|---|
1 |
|
Attribute Error: Columns ["x"] do not exist in "dataframe". Try one of: ["a", "b", "c", "d"].
Conclusion: Invalid column selection.
Source code in src/toolbox_pyspark/keys.py
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 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 |
|
add_keys_from_columns
🔗
add_keys_from_columns(
dataframe: psDataFrame,
collection_of_columns: Union[
tuple[Union[str, str_collection], ...],
list[Union[str, str_collection]],
dict[str, Union[str, str_collection]],
],
join_character: Optional[str] = "_",
) -> psDataFrame
Summary
Add multiple new keys, each of which are collections of other columns.
Details
There are a few reasons why this functionality would be needed:
- When you wanted to create a new single column to act as a combine key, derived from multiple other columns.
- When you're interacting with PowerBI, it will only allow you to create relationships on one single column, not a combination of multiple columns.
- When you're joining multiple tables together, each of them join on a different combination of different columns, and you want to make your
pyspark
joins cleaner, instead of usinglist
's of multipleF.col(...)
equality checks.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dataframe
|
DataFrame
|
The table to be updated. |
required |
collection_of_columns
|
Union[tuple[Union[str, str_collection], ...], [Union[str, str_collection]], dict[str, Union[str, str_collection]]]
|
The collection of columns to be combined together. |
required |
join_character
|
Optional[str]
|
The character to use to combine the columns together. |
'_'
|
Raises:
Type | Description |
---|---|
TypeError
|
If any of the inputs parsed to the parameters of this function are not the correct type. Uses the |
ColumnDoesNotExistError
|
If any of the |
Returns:
Type | Description |
---|---|
DataFrame
|
The updated |
Examples
Set up | |
---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
|
+---+---+---+---+
| a | b | c | d |
+---+---+---+---+
| 1 | a | 1 | 2 |
| 2 | b | 1 | 2 |
| 3 | c | 1 | 2 |
| 4 | d | 1 | 2 |
+---+---+---+---+
Example 1: Basic usage | |
---|---|
1 2 |
|
+---+---+---+---+---------+---------+
| a | b | c | d | key_A_B | key_B_C |
+---+---+---+---+---------+---------+
| 1 | a | 1 | 2 | 1_a | a_1 |
| 2 | b | 1 | 2 | 2_b | b_1 |
| 3 | c | 1 | 2 | 3_c | c_1 |
| 4 | d | 1 | 2 | 4_d | d_1 |
+---+---+---+---+---------+---------+
Conclusion: Successfully added two new key columns to DataFrame.
Example 2: Created from dict | |
---|---|
1 2 |
|
+---+---+---+---+-------+--------+
| a | b | c | d | first | second |
+---+---+---+---+-------+--------+
| 1 | a | 1 | 2 | 1_a | a_1 |
| 2 | b | 1 | 2 | 2_b | b_1 |
| 3 | c | 1 | 2 | 3_c | c_1 |
| 4 | d | 1 | 2 | 4_d | d_1 |
+---+---+---+---+-------+--------+
Conclusion: Successfully added two new key columns to DataFrame.
Source code in src/toolbox_pyspark/keys.py
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 |
|