【问题标题】:add many rows to df from dictionary [duplicate]从字典中向df添加许多行[重复]
【发布时间】:2019-06-07 19:26:50
【问题描述】:

我有一个简单的空数据框。

import pandas as pd
data_table = pd.DataFrame(columns=['ID', 'Name', 'Description'])

现在我读取了一个 json 文件,并用它创建了一个字典列表。
该列表中有数百个字典,看起来像这样

{'id':'1', 'name':'some_name', 'description': 'some description'}
{'id':'2', 'name':'other_name', 'description': 'other description'}

我尝试将所有这些字典推送到我的 data_table 变量中,但我尝试的每一种方式,所有的形状或索引都搞砸了。

【问题讨论】:

    标签: python pandas dictionary dataframe


    【解决方案1】:

    根据您在list 中的所有dict 描述

    youdf=pd.concat([pd.Series(x) for x in l],1).T
    youdf
             description id        name
    0   some description  1   some_name
    1  other description  2  other_name 
    

    数据输入

    l=[{'id':'1', 'name':'some_name', 'description': 'some description'},
    {'id':'2', 'name':'other_name', 'description': 'other description'}]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-03-08
      • 2015-06-29
      • 2017-11-24
      • 2021-11-25
      • 1970-01-01
      • 2011-09-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多