Cleaning Functions
Client agnostic functions designed to standardise social media performance data received from Tracer.
For example, functions here can be used to clean column names or clean common types of data found in the Tracer data such as Country the advert was placed in or the media type of the advert.
For example some of the adverts have "France" as the country, or "francais". These will get converted to FR by the extract_country_from_string() function
clean_column_names(df, name_of_df, hardcode_col_dict=None, on_error='ignore', cols_no_change=DEFAULT_COLS_NO_CHANGE, print_matches=False)
Cleans the column names of an advertisement performance (organic or paid) dataset, commonly from Tracer but could also be from Sprout social. The column names will be standardized so then other functions in other libraries can work with the dataset.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df |
DataFrame
|
Input dataframe containing a row item for each piece of creative or a day of advertising. |
required |
name_of_df |
str
|
The name of the dataframe, used in logging messages |
required |
hardcode_col_dict |
Dict[str, str]
|
A dictionary specifying exact transformations of column names from the key to the value. |
None
|
errors |
str
|
How to handle errors during the conversion. Can be 'raise', 'ignore' or 'warn' |
required |
cols_no_change |
List[str]
|
A list of column names to be left unchanged. |
DEFAULT_COLS_NO_CHANGE
|
Returns:
| Name | Type | Description |
|---|---|---|
DataFrame |
pd.DataFrame
|
Output dataframe with the column names standardized. |
Source code in veetility/cleaning_functions.py
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 163 164 165 | |
clean_media_type(media_type)
Clean the media type into a standardised version of media type
Source code in veetility/cleaning_functions.py
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 | |
clean_placement(placement)
Clean a placement string into a standardised placement
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
placement |
str
|
A string representing the placement of the post |
required |
Returns:
| Name | Type | Description |
|---|---|---|
placement |
str
|
The standardised placement of the post, if it is in 'Reel', 'Story' or 'Feed' |
Source code in veetility/cleaning_functions.py
323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 | |
clean_platform_name(platform)
Cleans the platform name into a the standard used company wide.
Most platforms are in the 'title' case, i.e. First letter of each word is capitalised However there are some platforms that have are one word but are usually presented as having a capital letter midway through the word. E.g. TikTok and LinkedIn
Source code in veetility/cleaning_functions.py
245 246 247 248 249 250 251 252 253 254 255 256 257 258 | |
clean_url(url)
Clean the url of the post to produce a string with just the important information for matching.
In the case of Tiktok remove everything after and including the ?, This removes the utm parameters
Source code in veetility/cleaning_functions.py
260 261 262 263 264 265 266 267 268 269 270 271 | |
extract_after_nth_occurrence(string, char, n)
Extracts the string between the nth and n+1th occurrence of the character
Source code in veetility/cleaning_functions.py
346 347 348 349 350 351 352 353 354 | |
extract_columns_twitter_2(df)
Special case of extracting columns from unique Twitter campaign naming convention
Source code in veetility/cleaning_functions.py
410 411 412 413 414 415 416 417 418 419 420 421 | |
extract_country_from_string(string, client_name, hardcode_dict=None)
Converts a string containing info identifying a certain
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
string |
str string to pass through perhaps in a lambda function, the country is detected from this |
required | |
client_name |
str name of the client that is removed to make the detection of country easier, for example so you are not searching for 'de' to find Germany with 'indeed' in the string |
required |
Returns:
| Name | Type | Description |
|---|---|---|
country_code |
str
|
str country Tag abbreviation mostly following the ISO 3166-1 alpha-2 format |
Source code in veetility/cleaning_functions.py
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 | |
extract_creative_name(name, group_name)
Extract the creative name from either the Ad name or the group name depending on which is available
Source code in veetility/cleaning_functions.py
356 357 358 359 360 361 362 363 364 | |
extract_quarter_from_date(date)
Extracts the quarter from the date
Source code in veetility/cleaning_functions.py
341 342 343 344 | |
extract_region_from_country(country)
Extracts the region from the given country.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
country |
str
|
A string representing the country name. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str | The region of the given country, if it is in 'UK/IE', 'NL', 'DE', 'FR', 'IT', 'BE', it returns "EMEA". If the country is 'CA' or 'US', it returns "N. America". If the country is not found, it returns None. |
Examples:
>>> extract_region_from_country("FR")
'EMEA'
>>> extract_region_from_country("US")
'N. America'
>>> extract_region_from_country("XX")
None
Source code in veetility/cleaning_functions.py
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 | |
extract_value(string, identifier)
Extract value based on naming convention identifier
Source code in veetility/cleaning_functions.py
366 367 368 369 370 371 372 373 374 375 376 377 | |
strip_object_columns(df)
Strips leading and trailing whitespaces in columns containing strings. This stops effective duplications when two categories in a column are essentially the same but one just has a whitespace
Source code in veetility/cleaning_functions.py
210 211 212 213 214 215 216 | |
two_urls_per_post_to_1(x, target_cols=None)
This function deals with posts that have two urls even though they are effectively the same post.
Sometimes social media posts are posted but then deleted, only to be reposted later.
This function returns just the url of the post with the highest amount of 'impressions' or 'video views'
You can take a dataframe of social media posts, groupby the unique identifier and this will return only the row item with the highest number of impressions or video_views.
Source code in veetility/cleaning_functions.py
380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 | |
updated_value_extract(url)
Extract the unique identifier for a post from the url This format of this code depends on the platform, sometimes it is a numerical code, sometimes it is alphanumeric
Source code in veetility/cleaning_functions.py
273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 | |
video_len_toseconds(video_length)
Convert video length in format from Tracer usually "1:20" to seconds (80 seconds in this case)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
video_length |
str or int
|
A string representing video length, which can be in the format of 'minutes:seconds' or 'minutes.seconds', or an integer representing the video length in seconds. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
int |
int
|
The video length in seconds. Returns None if the input does not make sense (for example, negative times, or strings that don't match the expected formats). |
Examples:
>>> video_len_toseconds('1:20')
80
>>> video_len_toseconds('0.20')
20
>>> video_len_toseconds('20')
20
>>> video_len_toseconds(30)
30
Source code in veetility/cleaning_functions.py
424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 | |