简体   繁体   English

如何遍历数据框单列中的行?

[英]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.

相关问题 如何遍历 pyspark 中未知数据帧的列的行 - How to iterate through rows of a column of a unknown data-frame in pyspark 遍历数据帧行以发送 email - Iterate through data frame rows to send email 如何在 pandas 数据帧的单列内的行上应用 function? - How to apply a function over rows within a single column of a pandas data frame? 如何遍历满足某些条件的 Pandas 数据框并将这些行附加到新数据框? - How to iterate through pandas data frame that meet some condition and append those rows to new data frame? 遍历数据框 - Iterate through data frame Pyspark:如何遍历数据框列? - Pyspark: How to iterate through data frame columns? 如何遍历包含多个工作表的多个 Excel 工作簿的文件夹创建单个数据框? - How to iterate through a folder with multiple excel workbooks containing multiple worksheets create a single data frame? 遍历 dataframe 行并替换特定列中的字符串元素 - Iterate through dataframe rows and replace elements of strings within a specific column Pandas 迭代数据帧中单列的值 - Pandas iterate over values of single column in data frame 如何遍历具有已排序数字索引的数据框唯一行的列值,并在熊猫中进行重复? - How to iterate over column values for unique rows of a data frame with sorted, numerical index with duplicates in pandas?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM