Issue
Hi I'm working on a script, and I can't figure out how to accomplish -I have an excel file with student INFO -I wanna add that data to a PANDAS data frame -but I can't(or IDK how, yet) since I gotta make a code dynamic so it does it by itself regardless of how many student I have at that moment IDK if I explained myself correctly... here is the code, I'll appreciate any help
path = "database.xlsx" #location
#------------------------------------------------------------------------------------------------------
# This is to read excell and take SIDG variable which is (Students ID Given) so the script knows
# how many lines he's dealing with, and their position...
#
#
wb_obj = openpyxl.load_workbook(path) #----To open the workbook, workbook object is created
sheet_obj = wb_obj.active #----------------Get workbook active sheet object, from the active attribute
obj = sheet_obj.cell(row = 1, column = 1)#-Assign obj variable a cell in the excel
yet_to_convert_SIDG = obj.value #----------assign cell value to SIDG variable
SIDG = int(yet_to_convert_SIDG)#-----------assign to SIDG the integer converted value of yet_to_convert
to_load = SIDG + 2 #-----------------------it just tell the code the line where the Student info
# start, which is the second row of the excel
#------------------------------------------------------------------------------------------------------
data = [] # variable for data set# variable where the LIST from the excel will be stored
x = 0 #variable to append data list to dataframe PANDA
#----------------------------------------------------------------------------------------------------------------------------------------------------------------
while to_load > 2: # to process database
#assign a variable to certain cell
loading_id = sheet_obj.cell(row = to_load, column = 1)
loading_dl = sheet_obj.cell(row = to_load, column = 2)
loading_fn = sheet_obj.cell(row = to_load, column = 3)
loading_ln = sheet_obj.cell(row = to_load, column = 4)
loading_dob = sheet_obj.cell(row = to_load, column = 5)
loading_ssn = sheet_obj.cell(row = to_load, column = 6)
loading_pho = sheet_obj.cell(row = to_load, column = 7)
loading_email = sheet_obj.cell(row = to_load, column = 8)
loading_today = sheet_obj.cell(row = to_load, column = 9)
#assign a variable the value of said cell
id = loading_id.value
dl = loading_dl.value
fn = loading_fn.value
ln = loading_ln.value
dob = loading_dob.value
ssn = loading_ssn.value
pho = loading_pho.value
email = loading_email.value
today = loading_today.value
idname = 'ID'#-------------just to make the ID variable readable
idname = idname + str(id)#-just to make the ID variable readable
locals()[idname] = [id,fn,ln,dob,ssn,pho,email,today]# convert determine idname value into
# the actual variable and add a list
# of the info as the value
data.append([id,dl,fn,ln,dob,ssn,pho,email,today])# Append IDNAME list into Data MAster list
df = pd.DataFrame([data[x]], columns=('ID','DL','First Name','Last Name','DOB','SSN','Phone','Gmail','SIGN DATE'))
# my attempt to make the PANDA dataframe with the DATA list
# but when in the reality it just overwrite the previous Student info for obvius reasons
Solution
Did you try using pd.read_excel
?
import pandas as pd
path = "database.xlsx" #location
df = pd.read_excel(path)
Answered By - bottledmind
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.