gpt4 book ai didi

python - 查找 pandas 数据框中列的经度和纬度

转载 作者:行者123 更新时间:2023-12-01 09:26:48 24 4
gpt4 key购买 nike

我有一个 pandas 数据框:

df

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

我正在尝试从描述中获取地址。我做了什么:

provinces=["qc","on"]

b=[]
for index, row in df.iterrows():
c=0
for i in provinces:
if i in row["Description"].lower().split():
a=[row["id"],row["description"]]
c=1
break
if(c==1):
b.append(a)


b

[[0, 'POS Transfer A&W MONTREAL QC'],
[1, ' MKLI QC Montreal DOLLARAMA ']]

这里我正在捕获数组中的信息。我可以直接在附加的 pandas 列中捕获它

在这里,我捕获了所有有地址的描述。为了将其发送到 google api,我只想选择省份左侧和右侧的 1 个单词。情况如下:

POS Transfer A&W MONTREAL QC

我想捕捉:

A&W MONTREAL QC

如果是

MKLI QC Montreal DOLLARAMA

我要捕捉

QC Montreal DOLLARAMA

我该怎么做?

最佳答案

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

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

关于python - 查找 pandas 数据框中列的经度和纬度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50323809/

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