向 pandas 数据帧添加多个JSON数据 [英] Adding multiple json data to panda dataframes

查看:125
本文介绍了向 pandas 数据帧添加多个JSON数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用一个API来获取3个json数据,我想将这些数据添加到1个panda数据帧中

I am using a api to get 3 json data and I would like to add those datas to 1 panda dataframes

这是我的代码 我正在传递包含x的书籍ID的书籍,而这3个ID会返回3个具有所有书籍信息的json对象.

This is my code I am passing in books which contains the book id as x and those 3 id returns me 3 different json objects with all the book information.

for x in books:
newDF = pd.DataFrame()
bookinfo = requests.get( http://books.com/?x})
    books = bookinfo.json() 
    print(books)

这是我打印书籍后得到的3个数组,

This is the 3 arrays I get after printing books,

{  
   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个数据帧中

What I would like to do is only take u'bookInfo from the three arrays and make them into 1 dataframe

推荐答案

IIUC:

pd.concat(
    pd.DataFrame([requests.get( http://books.com/?x}).json() for x in books]),
    ignore_index=True)

或者,您可以将JSON响应收集到列表中,然后执行以下操作:

Alternatively you can collect JSON responses into a list and do the following:

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是您已发布的字典列表:

where d is a list of dicts, you've posted:

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'}]

这篇关于向 pandas 数据帧添加多个JSON数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆