我正在使用api获取3个json数据,我想将这些数据添加到1个熊猫数据帧
这是我的代码 我传递的书籍包含书籍ID为x,而这3个id会返回3个不同的json对象以及所有书籍信息。
for x in books:
newDF = pd.DataFrame()
bookinfo = requests.get( http://books.com/?x})
books = bookinfo.json()
print(books)
这是印刷书籍后得到的3个数组,
{
u'bookInfo':[
{
u'book_created':u'2017-05-31',
u'book_rating':3,
u'book_sold':0
},
{
u'book_created':u'2017-05-31',
u'book_rating':2,
u'book_sold':1
},
],
u'book_reading_speed':u'4.29',
u'book_sale_date':u'2017-05-31'
}
{
u'bookInfo':[
{
u'book_created':u'2017-05-31',
u'book_rating':3,
u'book_sold':0
},
{
u'book_created':u'2017-05-31',
u'book_rating':2,
u'book_sold':1
},
],
u'book_reading_speed':u'4.29',
u'book_sale_date':u'2017-05-31'
}
{
u'bookInfo':[
{
u'book_created':u'2017-05-31',
u'book_rating':3,
u'book_sold':0
},
{
u'book_created':u'2017-05-31',
u'book_rating':2,
u'book_sold':1
},
],
u'book_reading_speed':u'4.29',
u'book_sale_date':u'2017-05-31'
}
我想做的只是从三个数组中取u'bookInfo
并将它们分成1个数据帧
答案 0 :(得分:1)
IIUC:
pd.concat(
pd.DataFrame([requests.get( http://books.com/?x}).json() for x in books]),
ignore_index=True)
或者,您可以将JSON响应收集到列表中并执行以下操作:
In [30]: pd.concat([pd.DataFrame(x['bookInfo']) for x in d], ignore_index=True)
Out[30]:
book_created book_rating book_sold
0 2017-05-31 3 0
1 2017-05-31 2 1
2 2017-05-31 3 0
3 2017-05-31 2 1
4 2017-05-31 3 0
5 2017-05-31 2 1
或
In [25]: pd.DataFrame([y for x in d for y in x['bookInfo']])
Out[25]:
book_created book_rating book_sold
0 2017-05-31 3 0
1 2017-05-31 2 1
2 2017-05-31 3 0
3 2017-05-31 2 1
4 2017-05-31 3 0
5 2017-05-31 2 1
其中d
是一个dicts列表,您已发布:
In [20]: d
Out[20]:
[{'bookInfo': [{'book_created': '2017-05-31',
'book_rating': 3,
'book_sold': 0},
{'book_created': '2017-05-31', 'book_rating': 2, 'book_sold': 1}],
'book_reading_speed': '4.29',
'book_sale_date': '2017-05-31'},
{'bookInfo': [{'book_created': '2017-05-31',
'book_rating': 3,
'book_sold': 0},
{'book_created': '2017-05-31', 'book_rating': 2, 'book_sold': 1}],
'book_reading_speed': '4.29',
'book_sale_date': '2017-05-31'},
{'bookInfo': [{'book_created': '2017-05-31',
'book_rating': 3,
'book_sold': 0},
{'book_created': '2017-05-31', 'book_rating': 2, 'book_sold': 1}],
'book_reading_speed': '4.29',
'book_sale_date': '2017-05-31'}]