简体   繁体   中英

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). Then write the parsed addresses to a new data-frame WITH the respective id's.

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. 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).

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'))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM