熊猫添加迭代添加列到数据帧

时间:2021-07-30 01:42:49

标签: python pandas selenium

我想将变量节点保存到每一行,每一行都有不同的节点值

//这一行我想将变量节点添加到万国邮联列,但目前它只复制最新的节点变量并保存到所有行相同的值。

制作方法

current result 

No. KP      UPU
88888888    tidak berjaya
66666666    tidak berjaya
55555555    tidak berjaya
expected result
No. KP      UPU
88888888    tidak berjaya
66666666    tahniah
55555555    harap maaf
import pandas as pd
import requests
from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from pandas import ExcelWriter


excel_file = 'upu6.csv'
students = pd.read_csv(excel_file, dtype={'No. KP': object})
# print (students.head())
# writer = pd.ExcelWriter('outputUpu.xlsx')


options = Options()
    # options.add_argument("user-data-dir=C:\\Users\\Administrator\\AppData\\Local\\Google\\Chrome\\User Data\\")
options.headless = False
browser = webdriver.Chrome(executable_path=r'C:\chromedriver.exe', options=options) # Your webdriver path

for index,row in students.iterrows():
    nokp = (row["No. KP"])
    browser.get("https://jpt.unimas.my/semakKeputusanSPM.jsp")
    ic = browser.find_element_by_id('vNOKP')
    ic.send_keys(nokp)

    browser.find_element_by_id('bMASUK').click()

    result = BeautifulSoup(browser.page_source, "html.parser")

    for node in result.find_all(text=lambda t: t and any(x in t for x in ['TIDAK BERJAYA', 'TAHNIAH', 'HARAP MAAF. TIADA REKOD PERMOHONAN'])):
        
        print(row['No. KP'] +" "+node)
        //this line I want to add variable node to column UPU, but currently it only copy the 
        //latest node variable and save to all row
        students['UPU'] = node

    
    

students.to_csv('data.csv')

1 个答案:

答案 0 :(得分:0)

您可以使用.at()来定位您想要的单元格并更改其值

首先将您设置为 No. KP 作为数据框的索引

students.set_index('No. KP')

然后您可以使用索引和列名定位单元格

students.at[row['No. KP'], ['UPU']] = node