结合城市和州弦的大熊猫 - python

我有一个数据框,其中的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']]

在返回'Response'(Python)中传递多个参数 - python

我在Angular工作,正在使用Http请求和响应。是否可以在“响应”中发送多个参数。角度文件:this.http.get("api/agent/applicationaware").subscribe((data:any)... python文件:def get(request): ... return Response(seriali…

Python exchangelib在子文件夹中读取邮件 - python

我想从Outlook邮箱的子文件夹中读取邮件。Inbox ├──myfolder 我可以使用account.inbox.all()阅读收件箱,但我想阅读myfolder中的邮件我尝试了此页面folder部分中的内容,但无法正确完成https://pypi.python.org/pypi/exchangelib/ 参考方案 您需要首先掌握Folder的myfo…

R'relaimpo'软件包的Python端口 - python

我需要计算Lindeman-Merenda-Gold(LMG)分数,以进行回归分析。我发现R语言的relaimpo包下有该文件。不幸的是,我对R没有任何经验。我检查了互联网,但找不到。这个程序包有python端口吗?如果不存在,是否可以通过python使用该包? python参考方案 最近,我遇到了pingouin库。

Python ThreadPoolExecutor抑制异常 - python

from concurrent.futures import ThreadPoolExecutor, wait, ALL_COMPLETED def div_zero(x): print('In div_zero') return x / 0 with ThreadPoolExecutor(max_workers=4) as execut…

如何用'-'解析字符串到节点js本地脚本? - python

我正在使用本地节点js脚本来处理字符串。我陷入了将'-'字符串解析为本地节点js脚本的问题。render.js:#! /usr/bin/env -S node -r esm let argv = require('yargs') .usage('$0 [string]') .argv; console.log(argv…