熊猫-仅读取列表的json文件

时间:2020-10-27 11:25:34

标签: python json pandas

我有一个给定的.json文件,该文件另存为列表格式(我猜这不是正确的json格式)

如下:

users.json:

[ "user1", "user2" ]

我想将其读入pandas数据框,并尝试在orient参数中使用不同类型的参数,如下所示:

import pandas as pd
nodes = pd.read_json('users.json', orient='split')

我希望结果看起来像这样:

desired_df = pd.DataFrame({'col1': ["user1", "user2"]})

我发现的最接近的问题是

this one

任何帮助都会很棒!预先感谢

1 个答案:

答案 0 :(得分:2)

下面的代码将为您创建df。

顺便说一句-json文件是有效的json

{
  "$schema": "http://json.schemastore.org/launchsettings.json",
  "profiles": {
    "Build & Watch": {
      "commandName": "Project",
      "commandLineArgs": "watch run",
      "workingDirectory": "$(ProjectDir)",
      "launchBrowser": true,
      "applicationUrl": "https://localhost:5001;http://localhost:5000",
      "launchUrl": "swagger",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    }
  }
}

输出

import pandas as pd
import json
with open('users.json') as f:
  data = json.load(f)
desired_df = pd.DataFrame({'col1':data})
print(desired_df)