【问题标题】:Python count number of rows in CSV and pass each row to variablePython计算CSV中的行数并将每一行传递给变量
【发布时间】:2019-04-04 22:13:28
【问题描述】:

我有一个如下所示的 .csv:

Name    Gender     DOB      Status
A       M          12/13/79  Expired
B       F          10/25/75  Undocumented

我正在尝试使用 selenium 将每条记录更新到网站。

import pandas as pd
import numpy as np
rec = pd.read_table('oldclients.csv', sep=',')

count_row = people.shape[0]
print (count_row)
s=people.loc[0].tolist()

到目前为止,我已经能够使用 s[1] s[2] 将变量传递给 selenium 以更新网页,但我只能让它工作一行。

我的逻辑是

Count Number of rows in csv
Foreach Row in csv -> pass values
Selenium-> Insert said values in webpage

【问题讨论】:

    标签: python pandas selenium


    【解决方案1】:

    您可以通过多种方式解决此问题:

    1。手动循环遍历行

    如果这是你想做的,你想使用pd.DataFrame.iterrows()

    for row in people.iterrows():
        #your code here
    

    2。将函数应用于 DataFrame

    这是我推荐的,因为它感觉更适合熊猫。

    def func(row):
        #This function takes in a row and does something with Selenium
        #and does not return anything.
    people.apply(func, axis=1) 
    #^This applies the function to each row but does not actually change people.
    

    【讨论】:

      【解决方案2】:

      以下代码遍历数据框并提供值

      for row in df.itertuples():
          name = row.Name
          gender = row.Gender
          ...
          do something with the values and send to selenium
          ...
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-04-21
        • 1970-01-01
        • 1970-01-01
        • 2011-09-10
        • 2019-11-03
        相关资源
        最近更新 更多