You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何编辑DataFrame,将多列字典列表合并至指定列?

Solution to Merge Dictionaries Across Columns in DataFrame

Hey there, let's work through how to merge dictionaries from multiple columns into your id2 column while keeping all other columns (including name3 and future ones) intact. Here's a step-by-step solution:

First, let's set up our example DataFrame to match your input:

import pandas as pd

# Create the original DataFrame
df = pd.DataFrame({
    'ID': [101, 103],
    'Name': ['A', 'B'],
    'id2': [[{'a': '1'}, {'b': '2'}], [{'c': '3'}, {'d': '6'}]],
    'name2': [[{'e': '4'}, {'f': '5'}], [{'g': '7'}, {'h': '8'}]],
    'name3': [[{'x': '4'}, {'y': '5'}], [{'t': '4'}, {'o': '5'}]]
})

Next, we'll create a helper function to merge dictionaries at corresponding positions across multiple columns. This function will handle any number of columns you want to merge (including future ones you add later):

def merge_corresponding_dicts(row, merge_columns):
    # Grab all the lists of dictionaries from the specified columns
    dict_list_collection = [row[col] for col in merge_columns]
    
    # Merge dictionaries that are at the same index across all lists
    merged_dicts = []
    for dict_group in zip(*dict_list_collection):
        combined_dict = {}
        # Update the combined dict with each individual dict in the group
        for d in dict_group:
            combined_dict.update(d)
        merged_dicts.append(combined_dict)
    
    return merged_dicts

Now, define which columns you want to merge into id2. In your case, that's id2 (the base) plus name3—and you can add any future columns to this list as needed:

# List of columns to merge (id2 is the target, add new columns here later)
columns_to_merge = ['id2', 'name3']

# Update the id2 column with the merged dictionaries
df['id2'] = df.apply(lambda row: merge_corresponding_dicts(row, columns_to_merge), axis=1)

If you print the updated DataFrame, you'll get exactly the result you want (with name3 still retained, as noted):

print(df)

Output:

ID Name                                                id2                     name2                     name3
0  101    A  [{'a': '1', 'x': '4'}, {'b': '2', 'y': '5'}]  [{'e': '4'}, {'f': '5'}]  [{'x': '4'}, {'y': '5'}]
1  103    B  [{'c': '3', 't': '4'}, {'d': '6', 'o': '5'}]  [{'g': '7'}, {'h': '8'}]  [{'t': '4'}, {'o': '5'}]

Key Notes:

  • This solution assumes all columns you're merging have lists of dictionaries with the same length (which matches your example). If you ever have columns with unequal lengths, you'd need to add extra logic to handle that.
  • When merging, if dictionaries have duplicate keys, the later dictionary in the columns_to_merge list will overwrite the earlier one's values. If you need a different conflict resolution, you can adjust the combined_dict.update(d) step.
  • To add future columns, just append their names to the columns_to_merge list—no need to change the helper function!

内容的提问来源于stack exchange,提问作者R.singh

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.06 19:53:09