为每一行创建的熊猫DataFrame

时间:2018-11-28 05:06:12

标签: python python-3.x pandas dataframe

我正在尝试将JSON中的数据从API传递到Pandas DataFrame。我无法使用pandas.read_json处理API数据,因此我确定这不是最佳解决方案,但是我目前for loop通过JSON运行以提取所需的值。

这里是我所拥有的:

import json
import urllib.request
import pandas as pd

r = urllib.request.urlopen("https://graph.facebook.com/v3.1/{page-id}/insights?access_token={access-token}&pretty=0&metric=page_impressions%2cpage_engaged_users%2cpage_fans%2cpage_video_views%2cpage_posts_impressions").read()

output = json.loads(r)

for item in output['data']:
    name = item['name']
    period = item['period']
    value = item['values'][0]['value']

    df = [{'Name': name, 'Period': period, 'Value': value}]

    df = pd.DataFrame(df)

    print(df)

这是API的JSON摘录:

    {
  "data": [
    {
      "name": "page_video_views",
      "period": "day",
      "values": [
        {
          "value": 634,
          "end_time": "2018-11-23T08:00:00+0000"
        },
        {
          "value": 465,
          "end_time": "2018-11-24T08:00:00+0000"
        }
      ],
      "title": "Daily Total Video Views",
      "description": "Daily: Total number of times videos have been viewed for more than 3 seconds. (Total Count)",
      "id": "{page-id}/insights/page_video_views/day"
    },

我现在面临的问题是由于For循环(我相信),每一行数据都被插入到自己的DataFrame中,如下所示:

               Name Period  Value
0  page_video_views    day    465
               Name Period  Value
0  page_video_views   week   3257
               Name   Period  Value
0  page_video_views  days_28   9987
               Name Period  Value
0  page_impressions    day   1402

我如何像这样将它们轻松地传递到同一DataFrame中?

               Name Period  Value
0  page_video_views    day    465
1  page_video_views   week   3257
2  page_video_views  days_28   9987
3  page_impressions    day   1402

同样,我知道这极有可能不是最佳解决方案,因此非常欢迎您提出任何有关改善任何方面的建议。

3 个答案:

答案 0 :(得分:1)

您可以创建词典列表并传递给DataFrame构造函数:

L = []
for item in output['data']:
    name = item['name']
    period = item['period']
    value = item['values'][0]['value']

    L.append({'Name': name, 'Period': period, 'Value': value})

df = pd.DataFrame(L)

或使用list comprehension

L = [({'Name': item['name'], 'Period': item['period'], 'Value': item['values'][0]['value']}) 
       for item in output['data']]

df = pd.DataFrame(L)
print (df)
               Name Period  Value
0  page_video_views    day    634

测试样本:

output = {
  "data": [
    {
      "name": "page_video_views",
      "period": "day",
      "values": [
        {
          "value": 634,
          "end_time": "2018-11-23T08:00:00+0000"
        },
        {
          "value": 465,
          "end_time": "2018-11-24T08:00:00+0000"
        }
      ],
      "title": "Daily Total Video Views",
      "description": "Daily: Total number of times videos have been viewed for more than 3 seconds. (Total Count)",
      "id": "{page-id}/insights/page_video_views/day"
    }]}

答案 1 :(得分:0)

在将json加载到数据框like后尝试转换字典:

output = json.loads(r)
df = pd.DataFrame.from_dict(output , orient='index')
df.reset_index(level=0, inplace=True)

答案 2 :(得分:0)

如果您要从网址中获取数据。我建议使用这种方法,并只传递存储在属性下的数据

import request
data=request.get("url here").json('Period')

Period现在是字典,您现在可以调用pd.DataFrame.from_dict(data)来解析数据

df = pd.DataFrame.from_dict(Period)