Home > other >  Cannot write value to specific Cell
Cannot write value to specific Cell

Time:10-22

New to Python. I am trying to store input value to specific cell. stockleft needs to be stored on the row where 'BARCODE' = barcode (user input barcode), Column ='STOCK'... What am i doing wrong???

import pandas as pd
import numpy as np

ufo = pd.read_excel('items.xlsx')
wb = load_workbook('items.xlsx')
ws= wb.active
#Columns ='BARCODE','DESCRIPTION','STOCK']

barcode=input("Enter Barcode: ")
stockleft=input("Enter How many are Left: ....")
ws[ufo.loc[ufo['BARCODE'] == barcode ,'STOCK']].value=stockleft #attempt1
#ufo.loc[ufo['BARCODE']==barcode, 'STOCK'] =stockleft #attempt2
wb.save('items.xlsx')

CodePudding user response:

Your attempt 2 looks correct, but you are applying it to ufo while you are saving wb. So if you are just looking at the output file you won't see any change, you must save the dataframe ufo instead.

CodePudding user response:

The attempt 2 syntax seems right, it will update the dataframe. The stockleft should be integer and save the same dataframe to excel using pandas

import pandas as pd
import numpy as np

ufo = pd.read_excel('items.xlsx')
#Columns ='BARCODE','DESCRIPTION','STOCK']

barcode = input("Enter Barcode: ")
stockleft = int(input("Enter How many are Left: ...."))
ufo.loc[ufo['BARCODE']==barcode, 'STOCK'] = stockleft #attempt2

ufo.to_excel('items_modified.xlsx')
  • Related