gpt4 book ai didi

python - Pandas 结合城市和州字符串

转载 作者:行者123 更新时间:2023-12-02 04:48:46 27 4
gpt4 key购买 nike

我有一个数据框,其中的 locations 列包含一串城市和州。我想加入城市与国家。

0       Seattle, WA,Portland, OR,Everett, WA,Oklahoma ...
1 Silver Spring, MD,Portland, OR,Everett, WA,Den...
2 Oklahoma City, OK,Kingston, WA,Gardner, MA,Tul...
3 Portland, OR,Oklahoma City, OK,Eugene, OR,Corv...
4 Silver Spring, MD,Seattle, WA,Everett, WA,Spok...
3241 Seattle, WA,Silver Spring, MD,Portland, OR,Okl...

通过对 SO 的研究,我发现了将它们分开并重新组合在一起的建议。但是,我无法使 join/zip 正常工作。

test_df['locations'].str.split(',')

这是我通过作业尝试的内容:

' '.join, zip(test_df['locations'][0::2], test_df['locations'][1::2])

期望的输出:

0       ['Seattle, WA','Portland, OR', 'Everett, WA', 'Oklahoma City, OK']
1 ['Silver Spring, MD', 'Portland, OR', 'Everett, WA', 'Denver, CO']
...

最佳答案

设置:

df = pd.DataFrame({'locations': {0: 'Seattle, WA,Portland, OR,Everett, WA',
1: 'Silver Spring, MD,Portland, OR,Everett, WA',
2: 'Oklahoma City, OK,Kingston, WA,Gardner, MA',
3: 'Portland, OR,Oklahoma City, OK,Eugene, OR',
4: 'Silver Spring, MD,Seattle, WA,Everett, WA',
3241: 'Seattle, WA,Silver Spring, MD,Portland, OR'}})

解决方案:

如果位置有n对“城市,州”的固定模式,则可以按如下方式完成:

import numpy as np
(
df.locations.str.split(',')
.dropna()
.apply(lambda x: x+[''] if len(x)%2 != 0 else x)
.apply(lambda x: [','.join(e) for e in np.asarray(x).reshape(-1,2)])
.tolist()
)

[['Seattle, WA', 'Portland, OR', 'Everett, WA'],
['Silver Spring, MD', 'Portland, OR', 'Everett, WA'],
['Oklahoma City, OK', 'Kingston, WA', 'Gardner, MA'],
['Portland, OR', 'Oklahoma City, OK', 'Eugene, OR'],
['Silver Spring, MD', 'Seattle, WA', 'Everett, WA'],
['Seattle, WA', 'Silver Spring, MD', 'Portland, OR']]

关于python - Pandas 结合城市和州字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59915365/

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