【发布时间】:2021-09-26 01:02:54
【问题描述】:
我正在尝试将示例 json 文件导入 pandas 数据框
我已经试过ValueError: Expected object or value when reading json as pandas dataframe
错误:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-8-bd3924a4e6a1> in <module>()
1 # working with json
2 import pandas as pd
----> 3 df = pd.read_json("/content/drive/MyDrive/Datasets/sample3.json", lines=True)
4 df.head()
6 frames
/usr/local/lib/python3.7/dist-packages/pandas/io/json/_json.py in _parse_no_numpy(self)
1117 if orient == "columns":
1118 self.obj = DataFrame(
-> 1119 loads(json, precise_float=self.precise_float), dtype=None
1120 )
1121 elif orient == "split":
ValueError: Expected object or value
1 尝试:
# working with json
import pandas as pd
df = pd.read_json("/content/drive/MyDrive/Datasets/sample3.json")
#df = pd.read_json("/content/drive/MyDrive/Datasets/sample3.json", lines=True) #also tried
#df = pd.read_json("/content/drive/MyDrive/Datasets/sample3.json", lines=True, orient='records') # also tried
#df = pd.read_json("/content/drive/MyDrive/Datasets/sample3.json", orient=str) # also tried
df.head()
Json 文件:
[
{
color: "red",
value: "#f00"
},
{
color: "green",
value: "#0f0"
},
{
color: "blue",
value: "#00f"
},
{
color: "cyan",
value: "#0ff"
},
{
color: "magenta",
value: "#f0f"
},
{
color: "yellow",
value: "#ff0"
},
{
color: "black",
value: "#000"
}
]
【问题讨论】:
-
color和value应该在双引号内。