[英]How to apply a function over rows within a single column of a pandas data frame?
I am using an address parsing library which accepts strings in the following way我正在使用一个地址解析库,它以下列方式接受字符串
import pyap
test_address = """
4998 Stairstep Lane Toronto ON
"""
addresses = pyap.parse(test_address, country='CA')
for address in addresses:
# shows found address
print(address)
# shows address parts
print(address.as_dict())
I would like to use this function on every row of a single pandas data-frame column.The dataframe contains two columns (id,address) This is what I have so far我想在单个 pandas 数据框列的每一行上使用这个 function
addresses.apply(lambda x: pyap.parse(x['address'], country='CA'),axis=1)
Though this runs, it results in a series instead of a 'pyap.address.Address'虽然这会运行,但它会产生一系列而不是“pyap.address.Address”
You have to do what you do, but in reverse: Let's say your dataframe is this:你必须做你所做的,但反过来:假设你的 dataframe 是这样的:
d = [{'id': '1', 'address': '4998 Stairstep Lane Toronto ON'}, {'id': '2', 'address': '1234 Stairwell Road Toronto ON'}]
df = pd.DataFrame(d)
df
id address
0 1 4998 Stairstep Lane Toronto ON
1 2 1234 Stairwell Road Toronto ON
Extract these addresses to a list将这些地址提取到列表中
address_list = df['address'].tolist()
and then process each with pyapp:然后用pyapp处理每个:
for al in address_list:
addresses = pyap.parse(al, country='CA')
for address in addresses:
print(address)
print(address.as_dict())
Let me know if it works.让我知道它是否有效。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.