python - ValueError : Conflicting metadata name name, 需要区分 Pandas 中的前缀

标签 python pandas

执行以下操作时出现 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"
        }
    },

错误消息(我猜是因为 name 字段在 '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 

最佳答案

您需要做的就是添加 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

关于python - ValueError : Conflicting metadata name name, 需要区分 Pandas 中的前缀,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52085169/

相关文章:

python - 从多行包含空值的数据帧创建平面数据帧

python - 将 Pandas 索引从整数格式更改为日期时间格式

python - pandas DataFrame,如何将函数应用于特定列?

python - 使用 CountVectorizer 在 python 中 todense 时出现内存错误

python - 使用 Flask 服务器和 xlsxwriter 导出 Excel

python - 允许用户在 tkinter askopenfilename() 中键入路径

python - 值错误: cannot reindex from a duplicate axis (python pandas)

python - 通过选择行中的日期时间值来转置 Pandas DataFrame

python - Pandas 爆炸 - 无法从重复的轴重新索引

python-3.x - 无法根据条件获取索引