Skip to main content

Bulk Rename of Keys

In eZintegration, we provide a rename operation, which works well for renaming individual columns. However, when dealing with multiple columns, it may not always be the most efficient approach.  We can leverage Python for dynamic column renaming, making it easy to handle several columns at once.

This Python code is used particularly useful when you want to rename all the key names by adding a prefix to the key names inside an array within a dictionary.

Let’s walk through how you can use Python to prepend a prefix to all column names in a dataset. This method only requires two parameters: the prefix you want to add and the items (the actual data)

Python Code for Renaming Multiple Columns

Responsedata = []
new_data = pycode_data
prefix = "billTo_"

# Extract items from the dataset response

items = new_data["bizdata_dataset_response"]["items"]
updated_items = []

# Iterate through the items and rename each column by adding the prefix

for item in items:
    updated_item = {}
    for key, value in item.items():
        new_key = f'{prefix}{key}'  # Add the prefix to each column name
        updated_item[new_key] = value
    updated_items.append(updated_item)

# Update the original dataset with the renamed columns

new_data["bizdata_dataset_response"]["items"] = updated_items
pycode_data = new_data

Key Parameters:

prefix: In this example, the prefix is set to "billTo_". You can modify this to suit your specific naming requirements.

items: This is the dataset, specifically under the bizdata_dataset_response section.


This approach is particularly useful in scenarios where the dataset contains a large number of columns, and you need to apply consistent naming conventions, such as adding a prefix or suffix. It’s also a more scalable solution when dealing with changes to multiple data sources in an integration pipeline.