Duplication
toolbox_pyspark.duplication
🔗
Summary
The duplication
module is used for duplicating data from an existing dataframe
, or unioning multiple dataframe
's together.
duplicate_union_dataframe
🔗
duplicate_union_dataframe(
dataframe: psDataFrame,
by_list: str_list,
new_column_name: str,
) -> psDataFrame
Summary
The purpose here is to take a given table and duplicate it entirely multiple times from values in a list, then union them all together.
Details
There are sometimes instances where we need to duplicate an entire table multiple times, with no change to the underlying data. Sometimes this is to maintain the structure of the data, but duplicate it to match a different table structure. This function is designed to do just that.
The dataframe
is the table to be duplicated, the by_list
is the list of values to loop over, and the new_column_name
is the new column to hold the loop values.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dataframe
|
DataFrame
|
The table to be duplicated. |
required |
by_list
|
str_list
|
The list to loop over. |
required |
new_column_name
|
str
|
The new column to hold the loop values. |
required |
Raises:
Type | Description |
---|---|
TypeError
|
If any of the inputs parsed to the parameters of this function are not the correct type. Uses the |
AttributeError
|
If any given value in the |
Returns:
Type | Description |
---|---|
DataFrame
|
The updated DataFrame. |
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 23 |
|
+---+---+---+---+
| a | b | c | d |
+---+---+---+---+
| 1 | a | x | 2 |
| 2 | b | x | 2 |
| 3 | c | x | 2 |
| 4 | d | x | 2 |
+---+---+---+---+
Example 1: Column missing | |
---|---|
1 2 3 4 5 |
|
+---+---+---+---+---+
| a | b | c | d | n |
+---+---+---+---+---+
| 1 | a | x | 2 | x |
| 2 | b | x | 2 | x |
| 3 | c | x | 2 | x |
| 4 | d | x | 2 | x |
| 1 | a | x | 2 | y |
| 2 | b | x | 2 | y |
| 3 | c | x | 2 | y |
| 4 | d | x | 2 | y |
| 1 | a | x | 2 | z |
| 2 | b | x | 2 | z |
| 3 | c | x | 2 | z |
| 4 | d | x | 2 | z |
+---+---+---+---+---+
Conclusion: Successfully duplicated data frame multiple times.
Example 2: Column existing | |
---|---|
1 2 3 4 5 |
|
+---+---+---+---+
| a | b | c | d |
+---+---+---+---+
| 1 | a | x | 2 |
| 2 | b | x | 2 |
| 3 | c | x | 2 |
| 4 | d | x | 2 |
| 1 | a | y | 2 |
| 2 | b | y | 2 |
| 3 | c | y | 2 |
| 4 | d | y | 2 |
| 1 | a | z | 2 |
| 2 | b | z | 2 |
| 3 | c | z | 2 |
| 4 | d | z | 2 |
+---+---+---+---+
Conclusion: Successfully duplicated data frame multiple times.
Notes
- How the
union
is performed:- Currently this function uses the
loop
andappend
method. - It was written this way because it's a lot easier and more logical for humans to understand.
- However, there's probably a more computationally efficient method for doing this by using SQL Joins.
- More specifically, for creating a CARTESIAN PRODUCT (aka a 'Cross-Join') over the data set.
- This is probably one of the only times EVER that a developer would want to create a cartesian product.
- All other times a cartesian product is to be avoided at all costs...
- Currently this function uses the
- Whether or not the column
new_column_name
exists or not on thedataframe
:- The process is a little different for if the
new_column_name
is existing or not... - If it is existing, we need to:
- Extract the
distinct
values from that column, - Create a duplicate copy of the raw table,
- Loop through all values in
by_list
, - Check if that
value
fromby_list
is already existing in the extracted values from thenew_column_name
column, - If it is already existing, proceed to next iteration,
- If it is not existing, take the raw table, update
new_column_name
to be thevalue
from that iteration ofby_list
, thenunion
that to the copy of the raw table, - Continue to iterate through all values in
by_list
until they're allunion
'ed together.
- Extract the
- If it is not existing, we need to:
- Add a new column to
dataframe
that has the name fromnew_column_name
, and a single literal value from the zero'th index of theby_list
, - Then to go through the same process as if the column were existing.
- Add a new column to
- Having now achieved this, the final output
dataframe
will now have all the updated duplicate values that we require.
- The process is a little different for if the
Warning
Obviously, it's easy to see how this function will blow out the size of a table to tremendious sizes. So be careful!
Source code in src/toolbox_pyspark/duplication.py
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 223 224 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 |
|
union_all
🔗
union_all(dfs: list[psDataFrame]) -> psDataFrame
Summary
Take a list of dataframes
, and union them all together.
Details
If any columns are missing or added in any of the dataframes
within dfs
, then they will be automatically handled with the allowMissingColumns
parameter, and any of the other dataframes
will simply contain null
values for those columns which they are missing.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dfs
|
list[DataFrame]
|
The list of |
required |
Raises:
Type | Description |
---|---|
TypeError
|
If any of the inputs parsed to the parameters of this function are not the correct type. Uses the |
Returns:
Type | Description |
---|---|
DataFrame
|
A single |
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 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
|
+---+---+---+---+
| a | b | c | d |
+---+---+---+---+
| 1 | a | 1 | 2 |
| 2 | b | 1 | 2 |
| 3 | c | 1 | 2 |
| 4 | d | 1 | 2 |
+---+---+---+---+
+---+---+---+
| a | b | c |
+---+---+---+
| 1 | a | 1 |
| 2 | b | 1 |
| 3 | c | 1 |
| 4 | d | 1 |
+---+---+---+
+---+---+---+---+
| a | b | c | e |
+---+---+---+---+
| 1 | a | 1 | 3 |
| 2 | b | 1 | 3 |
| 3 | c | 1 | 3 |
| 4 | d | 1 | 3 |
+---+---+---+---+
Example 1: Basic usage | |
---|---|
1 |
|
+---+---+---+------+------+
| a | b | c | d | e |
+---+---+---+------+------+
| 1 | a | 1 | 2 | null |
| 2 | b | 1 | 2 | null |
| 3 | c | 1 | 2 | null |
| 4 | d | 1 | 2 | null |
| 1 | a | 1 | null | null |
| 2 | b | 1 | null | null |
| 3 | c | 1 | null | null |
| 4 | d | 1 | null | null |
| 1 | a | 1 | null | 3 |
| 2 | b | 1 | null | 3 |
| 3 | c | 1 | null | 3 |
| 4 | d | 1 | null | 3 |
+---+---+---+------+------+
Conclusion: Successfully unioned all data frames together.
Source code in src/toolbox_pyspark/duplication.py
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 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 |
|