简体   繁体   English

如何遍历 pandas dataframe 中的列中的每一行

[英]How to loop through each row in a column in a pandas dataframe

I have an excel file like below with a set of names and their google scholar links.我有一个 excel 文件,如下所示,其中包含一组名称及其谷歌学者链接。

ID   Name   Link
1    A      www.abc.com
2    B      www.def.com
3    C      www.ghi.com

I have written a code to read the excel file, browse each link using a for loop, within each loop -scrape information from each link and write it in a new file.我编写了一个代码来读取 excel 文件,使用 for 循环浏览每个链接,在每个循环中 - 从每个链接中抓取信息并将其写入一个新文件。 The code is as follows.代码如下。

File=[]
for i in arr:
   driver.get(i)
   columns={}
   columns['Name'] = driver.find_element_by_id()
   columns['Citations'] = driver.find_element_by_id()
   File.append(columns)

My question is I want to include a column 'ID' in my new file which is the same as the column 'ID' as my excel file.我的问题是我想在我的新文件中包含一个列“ID”,它与我的 excel 文件中的列“ID”相同。 Essentially, I want the first row of the column'ID' in the first iteration of the for loop, the second row of the column'ID' in the second iteration of the loop and so on.本质上,我想要 for 循环的第一次迭代中列“ID”的第一行,循环的第二次迭代中列“ID”的第二行,依此类推。 Can someone please help?有人可以帮忙吗? Thanks!谢谢!

Instead of saving them as a dictionary, save them as a DataFrame and assign a new column, called source with the id:与其将它们保存为字典,不如将它们保存为 DataFrame 并分配一个名为 source 的新列,其 id 为:

File=[]
for i in arr:
   driver.get(i)
   columns={}
   columns['Name'] = driver.find_element_by_id()
   columns['Citations'] = driver.find_element_by_id()
   File.append(pd.DataFrame(columns).assign(source=i))

to get only a single dataframe out of it u then can use:要从中仅获取一个 dataframe ,您可以使用:

pd.concat(File)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用熊猫,如何逐行遍历数据帧,但每一行都是其自己的数据帧 - Using pandas, how do I loop through a dataframe row by row but with each row being its own dataframe 如何使用 pandas 循环遍历一行中的每一列 - how to loop through each column in a row using pandas 如何遍历pandas数据帧中的每一行,并在超过阈值后设置等于nan的值? - How to loop through each row in pandas dataframe and set values equal to nan after a threshold is surpassed? 如何使用熊猫循环:'对于文件中的每一行,对于行中的每一列' - How to loop with pandas: 'for each row in file, for each column in row' 如何在 Pandas Dataframe 中基于多个 if,elif 语句填充列的每一行值时消除循环 - How to Eliminate for loop in Pandas Dataframe in filling each row values of a column based on multiple if,elif statements 如何将 dataframe 中的每一列与另一个 dataframe pandas 的行相乘? - How to multiply each column in a dataframe with a row from another dataframe pandas? 如何循环通过 pandas dataframe 为每个变量运行独立的测试? - How to loop through a pandas dataframe to run an independent ttest for each of the variables? 如何遍历pandas数据帧,并有条件地将值分配给一行变量? - How to loop through pandas dataframe, and conditionally assign values to a row of a variable? 有没有办法逐行遍历 Pandas 数据帧并将每一行打印为一行,其中包含相应的团队 ID 和结果到文本文件? - Is there a way to loop through a pandas dataframe by row and print each row as a line with the corresponding team id and results to a text file? 如何在数据框中拆分一列并将每个值存储为新行(以熊猫为单位)? - How to split a column in a dataframe and store each value as a new row (in pandas)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM