无法使用Pandas,Python更新数据框中的值

时间:2020-09-25 15:54:12

标签: python pandas csv dtype

我正在尝试使用python中的pandas库更新名为cvv文件的'股票','正常价格','销售价格'列。从网站提取用于更新csv的值。我使用硒登录网站并输入所需的页面,使用BeautifulSoup解析页面,使用json从解析的网页中提取特定值。我已经成功提取了所需的数据,但是我无法使用提取的值更新csv文件...请帮助。[没有回溯或任何错误,程序也成功生成了csv文件,没有任何更改:(]

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

其他代码行介于两者之间

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

1 个答案:

答案 0 :(得分:0)

对不起,我解决了问题.....这是一个愚蠢的错误。 在循环内将csv读取为df1; df1已更新,并使用未更新的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")