将字典的字典转换为数据帧

2024-09-24 22:21:21 发布

您现在位置:Python中文网/ 问答频道 /正文

假设我有一本字典,看起来像这样:

{Row1 : {Col1: Data, Col2: Data ..} , Row2: {Col1: Data,...},...}

词典外:

{'1': {'0': '1', '1': '2', '2': '3'}, '2': {'0': '4', '1': '5', '2': '6'}}

我用orient='index'用dict方法检查熊猫,但这不是我需要的

这就是我的作品:

df_pasted_data = pd.DataFrame()
for v in dictionary.values():
    # need to set the index to 0 since im passing in a basic dictionary that looks like: col1: data, col2: data
    # otherwise it will throw an error saying  ValueError: If using all scalar values, you must pass an index
    temp = pd.DataFrame(v, index=[0])
    # append doesn't happen in place so i need to set it to itself
    df_pasted_data = df_pasted_data.append(temp, ignore_index=True)

这是可行的,但我在网上读到,做附件和其他事情效率不高,有没有更好的方法


Tags: to方法indataframedfdataindexdictionary