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

DataFrame键值对列拆分转置:将键设为表头的技术问询

Convert Dictionary Column in DataFrame to Separate Columns

Got it, let's walk through how to split that dictionary-filled column into proper table columns—this is a super common task in pandas, so I've got you covered.

Scenario Recap

You have a DataFrame where one column (let's say it's named Data) contains dictionaries. Each dictionary has ~50 key-value pairs, and you want those keys to become new column headers, with their corresponding values as row entries. For example, your original DataFrame might look like this:

import pandas as pd

# Sample original DataFrame
df_Normal = pd.DataFrame({
    "Data": [
        {"ProductID": "P001", "Price": 29.99, "Category": "Electronics"},
        {"ProductID": "P002", "Price": 14.99, "Category": "Apparel"}
    ]
})

Solution 1: Use pd.json_normalize() (Most Efficient)

The cleanest and fastest way to handle this is with pandas' built-in json_normalize() function. It's purpose-built for flattening JSON-like structures (like dictionaries) into tabular data, and it's way more efficient than manual methods for large datasets.

Code Implementation:

# Extract the list of dictionaries from the 'Data' column
dict_records = df_Normal["Data"].tolist()

# Normalize the dictionaries into a new DataFrame
normalized_df = pd.json_normalize(dict_records)

# If you need to keep other columns from the original DataFrame, join them back
# Example: if original df has an 'OrderID' column:
# normalized_df = df_Normal.drop("Data", axis=1).join(normalized_df)

Result:

ProductIDPriceCategory
P00129.99Electronics
P00214.99Apparel

Solution 2: Use apply(pd.Series) (For Smaller Datasets)

If you're working with a small dataset, you can also use apply() to convert each dictionary into a pandas Series. This will automatically align dictionary keys as column headers. Note that this method is slower for large datasets, but works perfectly for smaller ones.

Code Implementation:

normalized_df = df_Normal["Data"].apply(pd.Series)

# Join back original non-'Data' columns if needed:
# normalized_df = df_Normal.drop("Data", axis=1).join(normalized_df)

Key Notes

  • Both methods handle missing keys gracefully: if a dictionary doesn't include a key present in others, the corresponding cell will be filled with NaN.
  • If your dictionaries have nested structures (e.g., {"Product": {"ID": "P001"}, "Seller": {"Name": "TechStore"}}), json_normalize() can handle that too with parameters like record_path and meta to flatten nested levels.

内容的提问来源于stack exchange,提问作者Peter Lucas

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.27 03:42:26