gpt4 book ai didi

Python - 将更改应用于整个列

转载 作者:太空宇宙 更新时间:2023-11-04 02:57:31 24 4
gpt4 key购买 nike

修改整个列中所有值的最佳方法是什么?

这是它的样子:

data.locations.head()    

0 [{'name': 'Chevy Chase, MD'}]
1 [{'name': 'Irvine, CA'}]
2 [{'name': 'San Francisco Bay Area'}]
3 [{'name': 'Macon, GA'}]
4 [{'name': 'Fremont, CA'}]
Name: locations, dtype: object

我想改成这样:

0           [{'Chevy Chase, MD'}]
1 [{'Irvine, CA'}]
2 [{'San Francisco Bay Area'}]
3 [{'Macon, GA'}]
4 [{'Fremont, CA'}]

最佳答案

您可以使用 apply 通过 [0]list 中选择第一个 dict 然后 名称:

print (df.locations.apply(lambda x: [{x[0]['name']}]))
0 [{Chevy Chase, MD}]
1 [{Irvine, CA}]
2 [{San Francisco Bay Area}]
3 [{Macon, GA}]
4 [{Fremont, CA}]
Name: locations, dtype: object

关于Python - 将更改应用于整个列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41942789/

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