pandas - Updating python excel data in an existing dataframe -


i new python , facing issue of panda dataframe ... have loop outputs me on conditions name of person/folder in each iteration , want output name sent dataframe in dataframe 1 single row having output of last iteration , previous iteration outputs gets on write ... below code using hope u understand problem , help

from scipy.spatial import distance import csv import dlib import os import numpy np import cv2 import pandas pd skimage import  io import face_recognition pil import image open("data/train.csv","r") facefeatures2:     reader=csv.reader(facefeatures2)     featureslist2=[]     row in reader:         if len(row) != 0:             featureslist2= featureslist2 +[row]  facefeatures2.close() float_int2=[] results=[] f2 in range(0,len(featureslist2)):     float_int2 = float_int2 +[[float(str) str in subarray] subarray in [featureslist2[f2]]]     csv2 = np.vstack(float_int2) faces_folder_path = "data/newcropped" list = os.listdir(faces_folder_path) # dir directory path number_files = len(list) print (number_files)  writer = pd.excelwriter('pandas_name11.xlsx', engine='xlsxwriter') loop in range(0,number_files):     print("iteration ="+str(loop+1))     unknown_image = face_recognition.load_image_file(faces_folder_path + "/" + str(loop+1)+".jpg")     cv2.imshow("test",unknown_image)     cv2.waitkey(0)     #### --------------exception handling-----------####     try:         unknown_face_encoding = face_recognition.face_encodings(unknown_image)[0]      except  indexerror:         print("--->image not detectable")         pass         # ...........................#     results = face_recognition.compare_faces(csv2, unknown_face_encoding)     chunks=[results[x:x + 12] x in range(0, len(results),12)] # splits "results" list sublists of size 12     dirpath = "data/eachperson"     fname = []     fname = [f f in sorted(os.listdir(dirpath))]     counter = 0     index=0     c in range (0,len(chunks)):         if 'true' in str(chunks[c]):             counter=counter+1             index=c             df = pd.dataframe({'names': [fname[index]]})             df.to_excel(writer, sheet_name='sheet1')     if counter !=1 or counter ==0 :            print("student not present :(")     else:         print(str(fname[index])+" present!!!") writer.save() 

why don't initialise dataframe list? keep appending list, , @ end, should merge 1 big dataframe , write it. .to_excel overwrites excel file each time writes, calling inside loop not idea unless open in append mode. again, inefficient.

try this:

df_list = [] loop in range(0, number_files):    ...     c in range (0,len(chunks)):         if 'true' in str(chunks[c]):             ...              df_list.append(pd.dataframe({'names': [fname[index]]}))  writer = pd.excelwriter('pandas_name11.xlsx', engine='xlsxwriter') pd.concat(df_list).reset_index(drop=true).to_excel(writer, sheet_name='sheet1') 

if you, instead, want rewrite on each iteration, can consider taking @ this.


Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -