繁体   English   中英

如何在不包含索引的情况下在 pandas 中生成 json 文件?

[英]How can I generate a json file in pandas without including the index?

我有一个输入文件 json,大致如下所示

[
  {
    "identifier": "116S5RJ63",
    "containers": [
      {
        "contains": "soap",
        "height": {
          "unit": "FT",
          "value": 12.07123829231181
        },
        "length": {
          "unit": "FT",
          "value": 12.07123829231181
        },
        "quantity": 1,
        "weight": {
          "unit": "volumeUnits",
          "value": 10000
        },
        "width": {
          "unit": "FT",
          "value": 12.07123829231181
        }
      }
    ],{...}]

我在使用中阅读它

input_json  = pd.read_json(input_json_file)

然后我对 input_json 进行了一些处理:没什么特别的,只是更改了一些字段的内容。 接下来我再次尝试 output json 作为

input_json.to_json(output_file, orient='records', date_format='iso')

但是 output 看起来像这样

[
  {
    "index": 28741,
    "identifier": "115JKLJVZ",
     "containers": [
      {
        "contains": "soap",
        "height": {
          "unit": "FT",
          "value": 12.07123829231181
        },
        "length": {
          "unit": "FT",
          "value": 12.07123829231181
        },
        "quantity": 1,
        "weight": {
          "unit": "volumeUnits",
          "value": 10000
        },
        "width": {
          "unit": "FT",
          "value": 12.07123829231181
        }
      }
    ],{...}]

具体来说,它现在包括字段“index”,我认为 orient='records' 应该处理它。 我不确定下一步该怎么做。 有什么建议么?

在将其保存到文件之前尝试input_json.reset_index(drop=True, inplace=True) 这应该将旧索引从添加为列中删除。 重置索引文档

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM