【问题标题】:Processing API data (json) into a singular data frame (list of list of dictionaries)?将 API 数据(json)处理成单个数据框(字典列表列表)?
【发布时间】:2021-09-25 15:09:51
【问题描述】:

所以这是我之前帖子的延续,除了现在我有 API 数据可以使用。我正在尝试将键类型和电子邮件作为数据框中的列来得出最终数字。我的代码:

jsp_full=[]
for p in payloads:
    payload = {"payload": {"segmentId":p}}
    r = requests.post(url,headers = header, json = payload)
    #print(r, r.reason)
    time.sleep(r.elapsed.total_seconds())

    json_data = r.json() if r and r.status_code == 200 else None

    json_keys = json_data['payload']['supporters']

    json_package = []
    jsp_full.append(json_package)
    for row in json_keys:
        SID = row['supporterId']
        Handle = row['contacts']
        a_key = 'value'
        list_values = [a_list[a_key] for a_list in Handle]
        string = str(list_values).split(",")
        data = {
            'SupporterID' : SID,
            'Email' : strip_characters(string[-1]),
            'Type' : labels(p)
        }
        json_package.append(data)



    t2 = round(time.perf_counter(),2)

    b_key = "Email"
    e = len([b_list[b_key] for b_list in json_package])
    t = str(labels(p))

    #print(json_package)
    print(f'There are {e} emails in the {t} segment')
    print(f'Finished in {t2 - t1} seconds')


    excel = pd.DataFrame(json_package)
    excel.to_excel(r'C:\Users\am\Desktop\email parsing\{0} segment {1}.xlsx'.format(t, str(today)), sheet_name=t)

这部分工作得很好。 API 中的每个有效负载代表不同的人群,因此我将它们分成不同的文件。但是,我需要将所有记录合并到一个数据框中,因此我将其附加到 jsp_full。这是一个字典列表的列表。

一旦我有了它,我将运行我的代码的余额,如下所示:

S= pd.DataFrame(jsp_full[0], index = {0})
Advocacy_Supporters = S.sort_values("Type").groupby("Type", as_index=False)["Email"].first()
print(Advocacy_Supporters['Email'].count())

print("The number of Unique Advocacy Supporters is :")
Advocacy_Supporters_Group = Advocacy_Supporters.groupby("Type")["Email"].nunique()
print(Advocacy_Supporters_Group)

一些样本数据:

[{'SupporterID': '565f6a2f-c7fd-4f1b-bac2-e33976ef4306', 'Email': 'somebody@somewhere.edu', 'Type': 'd_Student Ambassadors'}, {'SupporterID': '7508dc12-7647-4e95-a8b8-bcb067861faf', 'Email': 'someoneelse@email.somewhere.edu', 'Type': 'd_Student Ambassadors'},...`

我想要的输出是一个如下所示的数据框:

SupporterID                           Email                     Type
565f6a2f-c7fd-4f1b-bac2-e33976ef4306  somebody@somewhere.edu    d_Student Ambassadors
7508dc12-7647-4e95-a8b8-bcb067861faf  someoneelse@email.somewhere.edu d_Student Ambassadors

非常感谢任何帮助!

【问题讨论】:

  • 我应该注意,当我尝试转换为 Dataframe 时,我会收到诸如“传递标量值时添加索引”之类的错误,或者我得到的记录太少,就像上面的代码一样。我尝试过嵌套的 for 循环,但似乎无法将字典从嵌套中取出(它返回空)。我也尝试过列表理解,但似乎......跨数据结构时很困难。
  • 尝试jsp_full.extend(json_package),然后尝试S= pd.DataFrame(jsp_full)
  • 这不起作用(列太多),但我确实找到了解决方法

标签: python json dataframe dictionary tuples


【解决方案1】:

因此,因为这段代码为每个段创建了一个 excel 文件,所以我所做的只是通过一个 for 循环在 excel 中读回,如下所示:

filesnames = ['e_S Donors', 'b_Contributors', 'c_Activists', 'd_Student Ambassadors', 'a_Volunteers', 'f_Offline Action Takers']

S= pd.DataFrame()
for i in filesnames:
    data = pd.read_excel(r'C:\Users\am\Desktop\email parsing\{0} segment {1}.xlsx'.format(i, str(today)),sheet_name= i, engine = 'openpyxl')
    S= S.append(data)

这成功了,因为它是我想要的格式。

【讨论】:

    猜你喜欢
    • 2021-04-10
    • 1970-01-01
    • 2020-05-15
    • 2021-08-14
    • 1970-01-01
    • 1970-01-01
    • 2023-02-07
    • 1970-01-01
    • 2021-09-06
    相关资源
    最近更新 更多