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

如何将Pickle加载的类JSON数据转换为指定格式的Pandas DataFrame

Solution to Convert Pickle Data to Target DataFrame

First, let's load the pickle data safely (using a with statement avoids leaving file handles open accidentally):

import pickle
import pandas as pd

# Load the pickle data properly
with open("name_ethnicities.pkl", "rb") as f:
    data = pickle.load(f)

Next, we'll transform the dictionary into the structure you need. For each name (the key in your dict), we extract all best values from the list of dictionaries, then join them with commas:

# Process the data into a format suitable for a DataFrame
processed_data = []
for name, ethnicity_entries in data.items():
    # Extract all 'best' values and join with commas
    ethnicity_str = ", ".join([entry["best"] for entry in ethnicity_entries])
    processed_data.append({"name": name, "ethnicity": ethnicity_str})

# Create the final DataFrame
df = pd.DataFrame(processed_data)

If you prefer a more concise approach, you can build the DataFrame directly with a list comprehension:

df = pd.DataFrame(
    [
        (name, ", ".join(entry["best"] for entry in ethnicity_entries))
        for name, ethnicity_entries in data.items()
    ],
    columns=["name", "ethnicity"]
)

Why pd.read_json didn't work?

pd.read_json is built to parse JSON-formatted files or strings into DataFrames. Your data is already a native Python dictionary loaded from a pickle file—it's not JSON data, which is why that method failed. We need to process the dictionary directly instead of treating it like JSON.

Running this code will produce exactly the output you're expecting:

nameethnicity
t creavalleGreaterEuropean, British
uyŏng yiAsian, GreaterEastAsian, EastAsian
temple ormeGreaterEuropean, British

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.15 04:42:31