[英]how to iterate through rows within single column of data frame?
I would like preform a function/iterate through all the rows of a single column of a data frame with 2 columns (id,address).我想执行一个函数/遍历具有 2 列(id、地址)的数据帧的单列的所有行。 Then write the parsed addresses to a new data-frame WITH the respective id's.
然后将解析后的地址写入具有相应 id 的新数据帧。
this is what I have thus far:这就是我迄今为止所拥有的:
addresses =pd.read_sql(query, conn)
# Create a list to hold results
results = []
# Go through each address in turn
for rowno,address in addresses.iterrows():
clean_addresses = pyap.parse(addresses['address'], country='CA')
results.append(clean_addresses)
the example from the library is:图书馆的例子是:
test_address = """
2000 BATH RD KINGSTON ON
"""
addresses = pyap.parse(test_address, country='CA')
for address in addresses:
# shows found address
print(address)
# shows address parts
print(address.as_dict())
You should prefer the DataFrame's(series) apply function over the manual loop.您应该更喜欢 DataFrame 的(系列)应用 function 而不是手动循环。 It's faster and easier to read.
它更快更容易阅读。
Basically, it is the same as the for loop thus the lambda function inside is 'applied' to each row of the DataFrame(series).基本上,它与 for 循环相同,因此 lambda function 内部“应用于”DataFrame(系列)的每一行。
Check the example bellow:检查下面的示例:
addresses =pd.read_sql(query, conn)
# Create a list to hold results
addresses['addresses'] = addresses['addresses'].apply(lambda row: pyap.parse(row, country='CA'))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.