gpt4 book ai didi

python - 如何使用petl转换表中的值

转载 作者:太空宇宙 更新时间:2023-11-03 14:34:08 27 4
gpt4 key购买 nike

我在转换表中的多个值时遇到了一个奇怪的问题。

我有一个数据,如下所示:

+-------------+--------------+-------------+
| id | name | category_id |
+=============+==============+=============+
| 1 | Horse | 5 |
+-------------+--------------+-------------+
| 2 | Cow | 5 |
+-------------+--------------+-------------+
| 3 | Pig | 2 |
+-------------+--------------+-------------+
| 4 | Chicken | 3 |
+-------------+--------------+-------------+

然后我通过 category_id 查找,如下所示:

# find the item category id
items_cat_id = etl.values(table, 'category_id')

然后我像这样循环数据,这样我就可以将上面的category_id转换为目标类别id:

    for item in items_cat_id:
"""
fetch target mongo collection.
source_name is the category name to look up in the target,
if we get a match convert the table category_id value to
the mongo id.
"""
target_category_id = target_db.collection.find_one({ 'name': source_name })

converted_table = etl.convert(table, 'category_id',
lambda _: target_category_id.get('_id'),
where=lambda x: x.category_id == item)

我似乎只得到这个:

+-------------+--------------+-----------------------------+
| id | name | category_id |
+=============+==============+=============================+
| 1 | Horse | 5 |
+-------------+--------------+-----------------------------+
| 2 | Cow | 5 |
+-------------+--------------+-----------------------------+
| 3 | Pig | 2 |
+-------------+--------------+-----------------------------+
| 4 | Chicken | QnicP3f4njL54HRqu |
+-------------+--------------+-----------------------------+

什么时候应该

+-------------+--------------+-----------------------------+
| id | name | category_id |
+=============+==============+=============================+
| 1 | Horse | 5 |
+-------------+--------------+-----------------------------+
| 2 | Cow | 5 |
+-------------+--------------+-----------------------------+
| 3 | Pig | yrDku5Yqkc2MKZZkD |
+-------------+--------------+-----------------------------+
| 4 | Chicken | QnicP3f4njL54HRqu |
+-------------+--------------+-----------------------------+

有什么建议吗?

最佳答案

etl.convert() 每次迭代都会从同一个未更改的源 中创建一个新表。因此,每个结果中仅更改一行。像这样更改您的代码:

for x in y:
table = etl.convert(table, ...)

现在您始终使用最后的结果。

关于python - 如何使用petl转换表中的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47083575/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com