Buy Me a Coffee? Your support is much appreciated!

Source Code:  

import os
from google.cloud import bigquery

os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = 'bigquery_service_acct.json'
client = bigquery.Client('sql-for-bigquery')

# -------------------------------
# Example 1. Add labels
# -------------------------------
# Step 1. Create table reference
dataset_ref = bigquery.DatasetReference(client.project, 'Staging')
table_ref = bigquery.TableReference(dataset_ref, 'AZURE')

# Step 2.a create labels
labels = {
    'type': 'social_media',
    'category': 'cloud_computing'
}
azure_table = client.get_table(table_ref)
azure_table.labels = labels
client.update_table(azure_table, ['labels'])

# Step 2.b add label to the table reference


# -------------------------------
# Example 2. Update/append labels
# -------------------------------
new_labels = {
    'type': 'software',
    'year': '2022'
}
table = client.get_table(table_ref)
table.labels = new_labels
client.update_table(table, ['labels'])


# -------------------------------
# Example 3. Delete labels
# -------------------------------
table = client.get_table(table_ref)
labels = table.labels
labels = {k: None for k, v in labels.items()}
table.labels = labels
client.update_table(table, ['labels'])