Transformations
flatten_dataframe(df, separator=':', replace_char='_', sanitized_columns=False)
Flattens the complex columns in the DataFrame.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
df |
DataFrame
|
The input PySpark DataFrame. |
required |
separator |
str
|
The separator to use in the resulting flattened column names, defaults to ":". |
':'
|
replace_char |
str
|
The character to replace special characters with in column names, defaults to "_". |
'_'
|
sanitized_columns |
bool
|
Whether to sanitize column names, defaults to False. |
False
|
Returns:
Type | Description |
---|---|
DataFrame .. note:: This function assumes the input DataFrame has a consistent schema across all rows. If you have files with different schemas, process each separately instead. .. example:: Example usage: >>> data = [ ( 1, ("Alice", 25), {"A": 100, "B": 200}, ["apple", "banana"], {"key": {"nested_key": 10}}, {"A#": 1000, "B@": 2000}, ), ( 2, ("Bob", 30), {"A": 150, "B": 250}, ["orange", "grape"], {"key": {"nested_key": 20}}, {"A#": 1500, "B@": 2500}, ), ] >>> df = spark.createDataFrame(data) >>> flattened_df = flatten_dataframe(df) >>> flattened_df.show() >>> flattened_df_with_hyphen = flatten_dataframe(df, replace_char="-") >>> flattened_df_with_hyphen.show()
|
The DataFrame with all complex data types flattened. |
Source code in quinn/transformations.py
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 402 |
|
flatten_map(df, col_name, separator=':')
Flattens the specified MapType column in the input DataFrame and returns a new DataFrame with the flattened columns.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
df |
DataFrame
|
The input PySpark DataFrame. |
required |
col_name |
str
|
The column name of the MapType to be flattened. |
required |
separator |
str
|
The separator to use in the resulting flattened column names, defaults to ":". |
':'
|
Returns:
Type | Description |
---|---|
DataFrame
|
The DataFrame with the flattened MapType column. |
Source code in quinn/transformations.py
278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 |
|
flatten_struct(df, col_name, separator=':')
Flattens the specified StructType column in the input DataFrame and returns a new DataFrame with the flattened columns.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
df |
DataFrame
|
The input PySpark DataFrame. |
required |
col_name |
str
|
The column name of the StructType to be flattened. |
required |
separator |
str
|
The separator to use in the resulting flattened column names, defaults to ':'. |
':'
|
Returns:
Type | Description |
---|---|
List[Column]
|
The DataFrame with the flattened StructType column. |
Source code in quinn/transformations.py
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 |
|
snake_case_col_names(df)
Function takes a DataFrame
instance and returns the same DataFrame
instance with all column names converted to snake case.
(e.g. col_name_1
). It uses the to_snake_case
function in conjunction with
the with_columns_renamed
function to achieve this.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
df |
DataFrame
|
A |
required |
Returns:
Type | Description |
---|---|
``DataFrame``.
|
A |
Source code in quinn/transformations.py
65 66 67 68 69 70 71 72 73 74 75 |
|
sort_columns(df, sort_order, sort_nested=False)
This function sorts the columns of a given DataFrame based on a given sort
order. The sort_order
parameter can either be asc
or desc
, which correspond to
ascending and descending order, respectively. If any other value is provided for
the sort_order
parameter, a ValueError
will be raised.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
df |
DataFrame
|
A DataFrame |
required |
sort_order |
str
|
The order in which to sort the columns in the DataFrame |
required |
sort_nested |
bool
|
Whether to sort nested structs or not. Defaults to false. |
False
|
Returns:
Type | Description |
---|---|
pyspark.sql.DataFrame
|
A DataFrame with the columns sorted in the chosen order |
Source code in quinn/transformations.py
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 |
|
to_snake_case(s)
Takes a string and converts it to snake case format.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
s |
str
|
The string to be converted. |
required |
Returns:
Type | Description |
---|---|
str
|
The string in snake case format. |
Source code in quinn/transformations.py
78 79 80 81 82 83 84 85 86 |
|
with_columns_renamed(fun)
Function designed to rename the columns of a Spark DataFrame
.
It takes a Callable[[str], str]
object as an argument (fun
) and returns a
Callable[[DataFrame], DataFrame]
object.
When _()
is called on a DataFrame
, it creates a list of column names,
applying the argument fun()
to each of them, and returning a new DataFrame
with the new column names.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
fun |
Callable[[str], str]
|
Renaming function |
required |
Returns:
Type | Description |
---|---|
Callable[[DataFrame], DataFrame]
|
Function which takes DataFrame as parameter. |
Source code in quinn/transformations.py
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
|
with_some_columns_renamed(fun, change_col_name)
Function that takes a Callable[[str], str]
and a Callable[[str], str]
and returns a Callable[[DataFrame], DataFrame]
.
Which in turn takes a DataFrame
and returns a DataFrame
with some of its columns renamed.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
fun |
Callable[[str], str]
|
A function that takes a column name as a string and returns a new name as a string. |
required |
change_col_name |
Callable[[str], str]
|
A function that takes a column name as a string and returns a boolean. |
required |
Returns:
Type | Description |
---|---|
`Callable[[DataFrame], DataFrame]`
|
A |
Source code in quinn/transformations.py
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 |
|