python - 如何循环遍历字典以在最终数据帧的单独行中获取它们的输出?

标签 python pandas api dataframe

我是 python 新手,正在尝试循环数据:

我正在使用 sendgrid api 类别统计端点,并且成功获取一个 API key 的输出。

我的代码:

import pandas as pd
import json
from pandas.io.json import json_normalize
import datetime
yesterday = datetime.date.fromordinal(datetime.date.today().toordinal()-1).strftime("%F")
import http.client

conn = http.client.HTTPSConnection("api.sendgrid.com")
payload = "{}"

keys = {
    #"CF_APHI_marketing" : "SG.UdhzjmjYR**",
#    "CF_APHI_operational": "SG.FO0b2O*",
#    "CF_HPI_marketing": "SG._dnhFlnrSL-**",
#    "CF_HPI_operational": "SG.4duN1bHiT9G1**",
#    "CF_PPI_operational": "SG.F3zH1XEB**",
#    "CF_PP_operational": "SG.9TcrDUAkSLWT**",
#    "CF_PP_marketing" : "SG.GD5wMhE_**",
    "CF_SandBox" : "SG.eDU7TY**",
#    "CF_PKN_operational" : "SG.JsIws8hLT5iYxry0oJlGqw.**",
#    "CF_PTC_operational" : "SG.2-dxeY8BRsabipDlRuvZMg.-**",
#    "CF_SP_operational" : "SG.tSF_YmxlRkmmsiHNrK2CWQ.xtyhO4UX3U--**",
#    "CF_SP_marketing" : "SG.KDuHYR-bRq-T74UexS4WpA.**-**",
#    "CF_EI_operational" : "SG.2TRc1VQMT-WDirJG-U-PtQ.sAx-**",
#    "CF_D_operational" :  "SG.KNFxNavYQ1yJwGTpnIDKxA.**-**"   
}
df = []  # Create new Dataframe

for name, value in keys.items():
    headers = { 'authorization': "Bearer " + value }

    conn.request("GET", "/v3/categories/stats/sums?aggregated_by=&start_date={d}&end_date={d}".format(d=yesterday).format(d=yesterday), payload, headers)

    res = conn.getresponse()
    data = res.read()
    print(data.decode("utf-8"))

    d = json.loads(data.decode("utf-8"))
    c=d['stats']
#    row = d['stats'][0]['name']
    # Add Brand to data row here with 'name'
    df.append(c)  # Load data row into df

当我调用 df 时,我的输出是:

df 输出[8]:

[[{'type': 'category',
   'name': 'Claim Notification Email',
   'metrics': {'blocks': 0,
    'bounce_drops': 0,
    'bounces': 0,
    'clicks': 0,
    'deferred': 0,
    'delivered': 5,
    'invalid_emails': 0,
    'opens': 0,
    'processed': 0,
    'requests': 5,
    'spam_report_drops': 0,
    'spam_reports': 0,
    'unique_clicks': 0,
    'unique_opens': 0,
    'unsubscribe_drops': 0,
    'unsubscribes': 0}},
  {'type': 'category',
   'name': 'Records Request 2',
   'metrics': {'blocks': 0,
    'bounce_drops': 0,
    'bounces': 0,
    'clicks': 0,
    'deferred': 0,
    'delivered': 1,
    'invalid_emails': 0,
    'opens': 0,
    'processed': 0,
    'requests': 1,
    'spam_report_drops': 0,
    'spam_reports': 0,
    'unique_clicks': 0,
    'unique_opens': 0,
    'unsubscribe_drops': 0,
    'unsubscribes': 0}},
  {'type': 'category',
   'name': 'Records Request 1',
   'metrics': {'blocks': 0,
    'bounce_drops': 0,
    'bounces': 0,
    'clicks': 0,
    'deferred': 0,
    'delivered': 1,
    'invalid_emails': 0,
    'opens': 0,
    'processed': 0,
    'requests': 1,
    'spam_report_drops': 0,
    'spam_reports': 0,
    'unique_clicks': 0,
    'unique_opens': 0,
    'unsubscribe_drops': 0,
    'unsubscribes': 0}},
  {'type': 'category',
   'name': 'Appeal Received Email',
   'metrics': {'blocks': 0,
    'bounce_drops': 0,
    'bounces': 0,
    'clicks': 0,
    'deferred': 0,
    'delivered': 0,
    'invalid_emails': 0,
    'opens': 1,
    'processed': 0,
    'requests': 0,
    'spam_report_drops': 0,
    'spam_reports': 0,
    'unique_clicks': 0,
    'unique_opens': 0,
    'unsubscribe_drops': 0,
    'unsubscribes': 0}}]]

我需要我的最终数据框看起来像:

Brand CF_APHI_marketing

name                  blocks  bounce_drops  bounces  ...  unique_opens  unsubscribe_dropsunsubscribes 
Appeal Received Email  0         7            14                9  ...          1240               0             
Records Request       18         12            7               2000                 0              0
Records Request2       2           10           8               3333                 1             0

基本上想要按品牌(键)获取上述 13 个品牌中每个品牌的所有类别名称的输出。有没有更好的方法来创建这个数据框?

我试过了

sendgrid = pd.DataFrame(df)

但是当我将其导出到 csv 时,我没有收到上面那样的输出,只是得到了一些 json 文本

最佳答案

如果:

df = [[{'type': 'category',
   'name': 'Claim Notification Email',
   'metrics': {'blocks': 0,
    'bounce_drops': 0,
    'bounces': 0,
    'clicks': 0,
    'deferred': 0,
    'delivered': 5,
    'invalid_emails': 0,
    'opens': 0,
    'processed': 0,
    'requests': 5,
    'spam_report_drops': 0,
    'spam_reports': 0,
    'unique_clicks': 0,
    'unique_opens': 0,
    'unsubscribe_drops': 0,
    'unsubscribes': 0}},
  {'type': 'category',
   'name': 'Records Request 2',
   'metrics': {'blocks': 0,
    'bounce_drops': 0,
    'bounces': 0,
    'clicks': 0,
    'deferred': 0,
    'delivered': 1,
    'invalid_emails': 0,
    'opens': 0,
    'processed': 0,
    'requests': 1,
    'spam_report_drops': 0,
    'spam_reports': 0,
    'unique_clicks': 0,
    'unique_opens': 0,
    'unsubscribe_drops': 0,
    'unsubscribes': 0}},
  {'type': 'category',
   'name': 'Records Request 1',
   'metrics': {'blocks': 0,
    'bounce_drops': 0,
    'bounces': 0,
    'clicks': 0,
    'deferred': 0,
    'delivered': 1,
    'invalid_emails': 0,
    'opens': 0,
    'processed': 0,
    'requests': 1,
    'spam_report_drops': 0,
    'spam_reports': 0,
    'unique_clicks': 0,
    'unique_opens': 0,
    'unsubscribe_drops': 0,
    'unsubscribes': 0}},
  {'type': 'category',
   'name': 'Appeal Received Email',
   'metrics': {'blocks': 0,
    'bounce_drops': 0,
    'bounces': 0,
    'clicks': 0,
    'deferred': 0,
    'delivered': 0,
    'invalid_emails': 0,
    'opens': 1,
    'processed': 0,
    'requests': 0,
    'spam_report_drops': 0,
    'spam_reports': 0,
    'unique_clicks': 0,
    'unique_opens': 0,
    'unsubscribe_drops': 0,
    'unsubscribes': 0}}]]

试试这个:

df = pd.DataFrame(df[0])
df_new = df[['type', 'name']]
df_metric =pd.DataFrame(list(df['metrics'].values))
sendgrid = pd.concat([df_new, df_metric], axis=1, sort=False)
sendgrid.set_index('name', inplace = True)
sendgrid.to_csv("sendgrid.csv")

你应该得到:

                              type  blocks  bounce_drops  bounces  clicks  \
name                                                                        
Claim Notification Email  category       0             0        0       0   
Records Request 2         category       0             0        0       0   
Records Request 1         category       0             0        0       0   
Appeal Received Email     category       0             0        0       0   

                          deferred  delivered  invalid_emails  opens  \
name                                                                   
Claim Notification Email         0          5               0      0   
Records Request 2                0          1               0      0   
Records Request 1                0          1               0      0   
Appeal Received Email            0          0               0      1   

                          processed  requests  spam_report_drops  \
name                                                               
Claim Notification Email          0         5                  0   
Records Request 2                 0         1                  0   
Records Request 1                 0         1                  0   
Appeal Received Email             0         0                  0   

                          spam_reports  unique_clicks  unique_opens  \
name                                                                  
Claim Notification Email             0              0             0   
Records Request 2                    0              0             0   
Records Request 1                    0              0             0   
Appeal Received Email                0              0             0   

                          unsubscribe_drops  unsubscribes  
name                                                       
Claim Notification Email                  0             0  
Records Request 2                         0             0  
Records Request 1                         0             0  
Appeal Received Email                     0             0  

关于python - 如何循环遍历字典以在最终数据帧的单独行中获取它们的输出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60100167/

相关文章:

python - 使用 MySQL 时,Django IntegerField 将 0 作为 None 值检索

python-3.x - Pandas 数据框在多列中获取索引和标题

python - 使用 pandas 读取 zip 文件中包含的多个文件

python - 将 pandas DataFrame 中的列转换为具有 nan 值的 float

javascript - jQuery Ajax - 意外 token :

ios - RestKit 0.20 映射问题(使用来自 API 的 JSON)

Python 计数器 : print key whose count is x

python - 在 matplotlib 注释中添加一行

python - 在tensorflow和python中保存和加载文件的区别

api - Web API Post 方法不起作用