0

I'm trying to update columns named 'Stock', 'Regular Price', 'Sale price' of a csv file using pandas library in python. Values for updating the csv is extracted from a website. I used selenium to login into website and enter the required pages, BeautifulSoup to parse the page, json to extract particular values from the parsed webpage. I have successfully extracted the required data, but I can't update the csv file with the extracted values...please help.[there is no traceback or any error, also, program succesfully produced a csv file without any change :( ]

from selenium import webdriver
from time import sleep
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.options import Options
from bs4 import BeautifulSoup
import re
from selenium.webdriver.common.keys import Keys
import json
import pandas as pd
from glob import glob

other code lines are present in between

js=json.loads(reqrdthngs)
for variants in (js[0]["productVariants"]):
    k=js[0]["product"]["productName"]
    skucde=re.findall("\([Code:]+\s([A-Za-z0-9]+)",k)[0]
    df1=pd.read_csv(filename)
    df1.loc[(df1['Parent']==skucde)&(df1['Attribute 1 value(s)']==variants["productVariantDescription"]),'Stock']=variants["productVariantQuantity"]
    df1.loc[(df1['Parent']==skucde),'Sale price']=df.loc[(df['code']==skucde),'discount']
    df1.loc[(df1['Parent']==skucde),'Regular price']=df.loc[(df['code']==skucde),'retail']
df1.to_csv('updated_CSVready_to_import.csv')
print ("Done")
  • You're not actually updating the dataframe with *df.loc*. Rather, you're just accessing these values without changing anything. For a full working solution it would be helpful to get a sample of your data to see what the DataFrame looks like. – Jakub Sep 25 '20 at 16:14
  • Look at this, seems like a duplicate (https://stackoverflow.com/questions/23330654/update-a-dataframe-in-pandas-while-iterating-row-by-row) – D-E-N Sep 25 '20 at 17:15

1 Answers1

0

silly me, I solved the problem.....it was a silly mistake. csv was read as df1 inside the loop; df1 was updated and re-defined with non updated csv

df1=pd.read_csv(filename)
js=json.loads(reqrdthngs)
for variants in (js[0]["productVariants"]):
    k=js[0]["product"]["productName"]
    skucde=re.findall("\([Code:]+\s([A-Za-z0-9]+)",k)[0]
    df1.loc[(df1['Parent']==skucde)&(df1['Attribute 1 value(s)']==variants["productVariantDescription"]),'Stock']=variants["productVariantQuantity"]
    df1.loc[(df1['Parent']==skucde),'Sale price']=df.loc[(df['code']==skucde),'discount']
    df1.loc[(df1['Parent']==skucde),'Regular price']=df.loc[(df['code']==skucde),'retail']
df1.to_csv('updated_CSVready_to_import.csv')
print ("Done")