【问题标题】:MemoryError in Python when saving list to dataframe将列表保存到数据框时 Python 中的 MemoryError
【发布时间】:2021-01-12 18:26:43
【问题描述】:

我正在尝试执行以下操作,即将包含 json 字符串的 python 列表保存到 jupyternotebook 中的数据框中

df = pd.io.json.json_normalize(mon_list)
df[['gfmsStr','_id']]

但后来我收到了这个错误:

MemoryError

然后,如果我运行其他块,它们都开始显示内存错误。我想知道是什么原因造成的,如果有的话我可以增加内存以避免错误。

谢谢!

更新: mon_list 中的内容如下:

mon_list[1]

[{'id': 1, 'name': {'first': 'Coleen', 'last': 'Volk'}},
        {'name': {'given': 'Mose', 'family': 'Regner'}},
        {'id': 2, 'name': 'Faye Raker'}]

【问题讨论】:

    标签: python memory


    【解决方案1】:

    你真的有清单吗?或者你有一个 JSON 文件? “mon_list”变量是什么格式的?

    这是将列表转换为数据框的方式

    # import pandas as pd 
    import pandas as pd 
      
    # list of strings 
    lst = ['Geeks', 'For', 'Geeks', 'is',  
                'portal', 'for', 'Geeks'] 
      
    # Calling DataFrame constructor on list 
    df = pd.DataFrame(lst)
    

    https://www.geeksforgeeks.org/create-a-pandas-dataframe-from-lists/

    【讨论】:

    • 我的错。 Mon_list 实际上是一个列表,每个列表项都是一个 json 字符串
    • 您可以轻松地将 JSON 字符串转换为 python 中的字典。然后你可以从字典中制作一个数据框。 Python 更擅长处理字典。您可以通过 d = json.load(json_file) 将其转换为字典
    猜你喜欢
    • 2016-03-14
    • 2016-01-05
    • 2021-03-29
    • 1970-01-01
    • 1970-01-01
    • 2016-08-20
    • 2019-09-08
    • 1970-01-01
    • 2011-12-25
    相关资源
    最近更新 更多