pandas 使用dict中的列和子列构造panda DataFrame

kd3sttzy  于 2023-01-15  发布在  其他
关注(0)|答案(1)|浏览(154)

我有一个如下形式的法令

dict = {
    "Lightweight_model_20221103_downscale_1536px_RecOut": {
        "CRR": "75.379",
        "Sum Time": 33132,
        "Sum Detection Time": 18406,
        "images": {
            "uk_UA_02  (1).jpg": {
                "Time": "877",
                "Time_detection": "469"
            },
            "uk_UA_02  (10).jpg": {
                "Time": "914",
                "Time_detection": "323"
            },
            "uk_UA_02  (11).jpg": {
                "Time": "1169",
                "Time_detection": "428"
            },
            "uk_UA_02  (12).jpg": {
                "Time": "881",
                "Time_detection": "371"
            },
            "uk_UA_02  (13).jpg": {
                "Time": "892",
                "Time_detection": "335"
            }
        }
    },
    "Lightweight_model_20221208_RecOut": {
        "CRR": "71.628",
        "Sum Time": 41209,
        "Sum Detection Time": 25301,
        "images": {
            "uk_UA_02  (1).jpg": {
                "Time": "916",
                "Time_detection": "573"
            },
            "uk_UA_02  (10).jpg": {
                "Time": "927",
                "Time_detection": "442"
            },
            "uk_UA_02  (11).jpg": {
                "Time": "1150",
                "Time_detection": "513"
            },
            "uk_UA_02  (12).jpg": {
                "Time": "1126",
                "Time_detection": "531"
            },
            "uk_UA_02  (13).jpg": {
                "Time": "921",
                "Time_detection": "462"
            }
        }
    }
}

我想让DataFrame在输出中像在图像上一样包含子列
[![在此输入图像说明][1]][1]
但我不知道在使用代码时如何打开['images']中的subdict

df = pd.DataFrame.from_dict(dict, orient='index')
df_full = pd.concat([df.drop(['images'], axis=1), df['images'].apply(pd.Series)], axis=1)

在列中接收带有文件名的字典
[![结果][2]][2]
如何打开嵌套指令并将其转换为子列[1]:https://i.stack.imgur.com/hGrKo.png [2]:https://i.stack.imgur.com/8LlUW.png

olmpazwi

olmpazwi1#

下面是借助Pandas json_normalizeMultiIndex.from_productconcat方法实现此操作的一种方法:

import pandas as pd

df = pd.DataFrame.from_dict(dict, orient='index')

# Save first columns and add a second empty level header
tmp = df[["CRR", "Sum Time", "Sum Detection Time"]]
tmp.columns = [tmp.columns, ["", "", ""]]
dfs= [tmp]

# Process "images" column
df = pd.DataFrame.from_dict(df["images"].to_dict(), orient='index')

# Create new second level column header for each column in df
for col in df.columns:
    tmp = pd.json_normalize(df[col])
    tmp.index = df.index
    tmp.columns = pd.MultiIndex.from_product([[col], tmp.columns])
    dfs.append(tmp)

# Concat everything in a new dataframe
new_df = pd.concat(dfs, axis=1)

然后:

print(new_df)

输出:

相关问题