在pandas datafram中查找列的经度和纬度

2024-09-29 06:25:23 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个熊猫数据帧:

df

id  Description
1   POS Transfer A&W MONTREAL QC
2   MKLI QC Montreal DOLLARAMA
3   PC - PAYMENT FROM - *****11*22

我想从描述中找出地址。我所做的:

^{pr2}$

这里我要在一个数组中捕获信息。我能在另一个pandas专栏中直接捕捉到它吗

在这里,我捕捉到了所有有地址的描述。为了把这个发送到googleapi,我只想选择省的左边和右边的1个单词。在以下情况下:

^{3}$

我想捕捉:

A&W MONTREAL QC

在下列情况下

MKLI QC Montreal DOLLARAMA

我要抓住

QC Montreal DOLLARAMA

我该怎么做?在


Tags: 数据posiddf地址情况descriptionpayment
1条回答
网友
1楼 · 发布于 2024-09-29 06:25:23

我认为需要在province前后选择一个单词:

provinces=["qc","on"]

def f(x):
    #convert to lowercast splitted list for find
    x1 = x.lower().split()
    x2 = x.split()
    #get indices of search word with one word before and after
    #min and max is for avoid indexing errors 
    out = [' '.join(x2[max(0, i-1): min(len(x), i+2)]) for i, y in enumerate(x1) 
                                                       if y in provinces]
    #return first matched value if exist
    return out[0] if len(out) > 0 else ''


df['new'] = df['Description'].apply(f)
print (df)
   id                     Description               new
0   1    POS Transfer A&W MONTREAL QC       MONTREAL QC
1   2      MKLI QC Montreal DOLLARAMA  MKLI QC Montreal
2   3  PC - PAYMENT FROM - *****11*22                  

相关问题 更多 >