【问题标题】:ValueError: Conflicting metadata name name, need distinguishing prefix in pandasValueError:元数据名称名称冲突,需要区分熊猫中的前缀
【发布时间】:2018-08-29 19:55:11
【问题描述】:

执行以下操作时出现 ValueError

df = pd.DataFrame.from_dict(
                  json_normalize(
                                 data,
                                 'protocol_parameters',
                                 [['status','status'], 
                                  'auto_discovered', 
                                  'average_eps',
                                  'creation_date',
                                  'description',
                                  'last_event_time',
                                  'name']
                                ), orient='columns')

代码中使用的数据对象

{
        "protocol_parameters": [
            {
                "name": "identifier",
                "value": "x.x.x.x"
            },
            {
                "name": "incomingPayloadEncoding",
                "value": "UTF-8"
            }
        ],
        "description": "LinuxServer device",
        "average_eps": 0,
        "creation_date": 0,
        "name": "LinuxServer @ x.x.x.x",
        "auto_discovered": true,
        "last_event_time": 1535539535018,
        "status": {
            "status": "SUCCESS"
        }
    },

错误消息(我猜是因为名称字段在“protocol_parameters”中,也是一个单独的对象。但我无法解决这个问题

Traceback (most recent call last):
  File "D:\Qradar\python\LogSources.py", line 31, in <module>
    df = pd.DataFrame.from_dict(json_normalize(data,'protocol_parameters',[['status','status'],'auto_discovered','average_eps','creation_date','description','last_event_time','name']), orient='columns')
  File "D:\VM\python\lib\site-packages\pandas\io\json\normalize.py", line 262, in json_normalize
    'need distinguishing prefix ' % k)
ValueError: Conflicting metadata name name, need distinguishing prefix 

【问题讨论】:

  • 发布整个错误
  • 请 1) 将您的整个错误发布为对您的问题的编辑,而不是在评论部分中;2) 将您的代码(和错误)格式化为代码。
  • 您能否也共享一部分数据对象,因为架构和数据似乎不一致。
  • 我已经更新了帖子,谢谢!

标签: python pandas


【解决方案1】:

您已经很接近了,您需要做的就是添加 record_prefix 来处理名称被使用两次。将前缀设置为您想要的任何字符串:

json_normalize(data,
               'protocol_parameters',
               [['status','status'], 
               'auto_discovered', 
               'average_eps',
               'creation_date',
               'description',
               'last_event_time',
               'name'], record_prefix='_'
                )

出来:

_name   _value  status.status   auto_discovered average_eps creation_date   description last_event_time name
0   identifier  x.x.x.x SUCCESS True    0   0   LinuxServer device  1535539535018   LinuxServer @ x.x.x.x
1   incomingPayloadEncoding UTF-8   SUCCESS True    0   0   LinuxServer device  1535539535018   LinuxServer @ x.x.x.x

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-12-14
    • 2022-01-17
    • 2018-03-25
    • 1970-01-01
    • 1970-01-01
    • 2017-10-31
    • 2023-02-10
    • 2018-02-17
    相关资源
    最近更新 更多