【问题标题】:Get JSON response for each df row获取每个 df 行的 JSON 响应
【发布时间】:2021-07-26 00:27:11
【问题描述】:

假设您有以下 df:

d = {'KvK': [72941014, 76912027, 75090058], 'line amount#2': [0.0, 0.05, .05], 'ExclBTW': [0.5, 0.18, .05]}
df = pd.DataFrame(data=d)
df

      KvK   line am   ExclBTW   APIOutput
0   72941014    0.00    0.50    https://api.kvk.nl/api/v2/search/companies?use...
1   76912027    0.05    0.18    https://api.kvk.nl/api/v2/search/companies?use...
2   75090058    0.05    0.05    https://api.kvk.nl/api/v2/search/companies?use...

现在我添加了 APIOutput 列:

df['APIOutput'] = df['KvK'].apply(lambda x: f"https://api.kvk.nl/api/v2/search/companies?user_key=l7xxd3eea0c598a645a7fa69dbb&q={x}")

我想将来自 APIOutput 的所有 JSON 响应放入一列 apioutput1。

我试过下面的代码;

df['apioutput1'] = requests.get.apply(lambda x: f"df['APIOutput']{x}", verify=False)

但是上述方法不起作用..

期望的输出:

       KvK     lineam  ExclBTW       APIOutput                                     apioutput1
0   72941014    0.00    0.50    https://api.kvk.nl/api/v2/search/companies?use...   JSON
1   76912027    0.05    0.18    https://api.kvk.nl/api/v2/search/companies?use...   JSON
2   75090058    0.05    0.05    https://api.kvk.nl/api/v2/search/companies?use...   JSON

以上如何实现?

请帮忙

【问题讨论】:

    标签: python json pandas numpy


    【解决方案1】:

    在系列上致电apply,就像您在填写APIOutput 栏时所做的那样。

    df['apioutput1'] = df['APIOutput'].apply(lambda url: requests.get(url, verify=False))
    

    【讨论】:

      【解决方案2】:

      使用 .apply 在每一行调用 requests.get

      df['apioutput1'] = df['APIOutput'].apply(lambda x: requests.get(x, verify=False) )

      【讨论】:

        猜你喜欢
        • 2021-08-19
        • 2022-06-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-02-03
        • 1970-01-01
        • 1970-01-01
        • 2019-04-04
        相关资源
        最近更新 更多