python - 如何使用 f 字符串遍历数据框?
问题描述
使用 f 字符串遍历数据框
想象一下,您有以下 df:
d = {'KvK': [0.21, 0.13, 0.1], 'line amount#2': [0.0, 0.05, .05], 'ExclBTW': [0.5, 0.18, .05]}
df = pd.DataFrame(data=d)
df
KvK line amount#2 ExclBTW
0 0.21 0.00 0.50
1 0.13 0.05 0.18
2 0.10 0.05 0.05
现在,我想将每个 KvK 值输入到 API 调用中。
我尝试了以下方法:
for index, row in df.iterrows():
details = df(row)
f"https://api.kvk.nl/api/v2/search/companies?user_key=ldbb&q=f{row['KvK']}"
但是,这不起作用。
期望的输出:
KvK line amount#2 ExclBTW APIoutput#1
0 0.21 0.00 0.50 0.21APIsampleAPIOutput
1 0.13 0.05 0.18 0.13APIsampleAPIOutput
2 0.10 0.05 0.05 0.10APIsampleAPIOutput
请帮忙!
解决方案
您可以使用 apply 来格式化您的 URL 字符串,如下所示:
df['APIOutput'] = df['KvK'].apply(lambda x: f"https://api.kvk.nl/api/v2/search/companies?user_key=ldbb&q=f{x}")
推荐阅读
- python - 如何在文件列表中为文件夹中的所有文件添加前缀“_”[Python]
- javascript - 获取购物车中物品的价值并相应地移除库存
- r - 从文件列表中读取 CSV 并“变异”一列(日期)
- c++ - 如何正确释放动态内存
- angular-material - 如何在 Angular 8 中的 Material Angular 中创建持续时间选择器
- python - pip 安装问题
- kubernetes - skaffold/kaniko:从另一个命名空间访问服务
- mysql - 将数据库存储在 /var/lib/docker/volumes/ 中有多安全?
- docker - docker.service 无法运行“启动”任务:没有这样的文件或目录
- javascript - 在 useCallback 钩子中获取 ref null