panda adding Iteratively Add Columns to a Dataframe

Issue

I want to save variable node to each row , each row has different node value

//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 same value.

how to make it

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

Solution

You can use .at() to locate the cell you want and change its value

first set you No. KP as index for the dataframe

students.set_index('No. KP')

then you can locate the cell using the index and column name

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

Answered By – burningalc

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published