我有一个json
这样的文件:
{
"info": [
{
"product": "ABC",
"email-adr": "abc08@gmail.com",
"Gender": "M",
"Name": "João",
"id": "1069",
"mobile": "iOS | 9.1",
"client": " "
},
我试图:
data2 = pd.read_json('file.json', encoding=str)
但是只有一列df
:
info
0 {u'id':u'1069',u'client'..
将此json
文件读取到pandas df
的最佳方法是什么?
答案 0 :(得分:1)
您必须仅作为参数传递内部对象:
import json
z="""{
"info":
[
{"product": "ABC", "email-adr": "abc08@gmail.com", "Gender": "M", "Name": "João", "id": "1069", "mobile": "iOS | 9.1", "client": " " }]}"""
>>> pd.DataFrame(json.loads(z)['info'])
Gender Name client email-adr id mobile product
0 M João abc08@gmail.com 1069 iOS | 9.1 ABC
加载json文件:
with open("file.json", "r") as f:
data = json.load(f)
然后你可以做
pd.DataFrame(data['info'])