【问题标题】:Json import leads to ValueError: Mixing dicts with non-Series may lead to ambiguous orderingJson 导入导致 ValueError:将 dicts 与非系列混合可能会导致排序不明确
【发布时间】:2021-01-05 11:05:15
【问题描述】:

我有一个带有国家几何/形状的 *.json 数据集,名为 world-110m.json(见下文)。我怀疑它可能来自https://www.naturalearthdata.com/downloads/10m-cultural-vectors/

我尝试通过以下方式在 Pandas 中导入文件:

import pandas as pd
world_json = pd.read_json('world-110m.json')

但我收到以下错误:

ValueError: Mixing dicts with non-Series may lead to ambiguous ordering.

知道如何解决这个问题吗?我需要这个特定文件,我无法从 NaturalEarthData.com 下载另一个版本。

{
  "type": "Topology",
  "arcs""transform": {
    "scale": [
      0.012741070487533123,
      0.008502952811159802
    ],
    "translate": [
      -179.99999999999997,
      -55.60772818836408
    ]
  },
  "objects": {
    "collection": {
      "type": "GeometryCollection",
      "geometries": [
        {
          "arcs": [
            [
              0,
              1,
              2,
              3,
              4,
              5
            ]
          ],
          "type": "Polygon",
          "properties": {
            "name": "Afghanistan",
            "nameLong": "Afghanistan",
            "abbrev": "Afg.",
            "isoA2": "AF",
            "isoA3": "AFG",
            "continent": "Asia"
          }
        },
        {

完整文件可通过以下方式获得:https://pastebin.com/d6E2WYVw

【问题讨论】:

    标签: python json pandas


    【解决方案1】:

    我假设您需要从文件中获取哪些信息,但无论您想要什么信息,概念都是一样的。

    import json
    import pandas as pd
    
    # your path the the json file
    path = r'your\file\path'
    
    # open your json file and read it using json.load
    with open(path) as data:    
        d = json.load(data)  
        
    # use json_normalize to create your dataframe
    df = pd.json_normalize(d['objects']['collection']['geometries'])
    
                                           arcs          type  \
    0                      [[0, 1, 2, 3, 4, 5]]       Polygon   
    1          [[[6, 7, 8, 9]], [[10, 11, 12]]]  MultiPolygon   
    2                    [[13, 14, 15, 16, 17]]       Polygon   
    3                    [[18, 19, 20, 21, 22]]       Polygon   
    4  [[[23, 24]], [[25, 26, 27, 28, 29, 30]]]  MultiPolygon   
    
            properties.name   properties.nameLong properties.abbrev  \
    0           Afghanistan           Afghanistan              Afg.   
    1                Angola                Angola              Ang.   
    2               Albania               Albania              Alb.   
    3  United Arab Emirates  United Arab Emirates            U.A.E.   
    4             Argentina             Argentina              Arg.   
    
      properties.isoA2 properties.isoA3 properties.continent  
    0               AF              AFG                 Asia  
    1               AO              AGO               Africa  
    2               AL              ALB               Europe  
    3               AE              ARE                 Asia  
    4               AR              ARG        South America  
    

    【讨论】:

    • 非常感谢您的回复!但是,对我来说,这会产生AttributeError: module 'pandas' has no attribute 'json_normalize'
    • 您正在使用旧版本的熊猫。您将需要导入模块:from pandas.io.json import json_normalize 然后而不是执行 pd.json_normalize 您只需调用 json_normalize
    • 感谢您的评论。奇怪,我更新了熊猫,但它仍然不起作用。但似乎这可能是 Spyder IDE This command failed to be executed because an error occurred while trying to get the file code from Spyder's editor. The error was: An exception has occurred, use %tb to see the full traceback. TypeError: handle_get_file_code() got an unexpected keyword argument 'save_all' 的问题。
    • 这似乎是一个奇怪的追溯。我不使用 Spyder,所以我不知道我能提供多少帮助。
    猜你喜欢
    • 2021-08-20
    • 2019-11-22
    • 2018-09-05
    • 2021-02-19
    • 2021-12-25
    • 2019-07-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多