Reputation: 196
This is a very basic question : I want to iterate through a dataframe column which I extracted in a list "unique" and write each of the visits between quotation marks and separated by Space into a cell in an excel file, this code just return the last Value :
unique = Contains['VISIT'].unique().tolist()
i = 0
visit_id = []
sizeofList = len(unique)
while i < sizeofList :
g = unique[i]
visit_id = '"'+g+'"'
print(visit_id)
i += 1
d = {'Visitname': [visit_id]}
df = pd.DataFrame(data=d)
df.to_excel('Visits.xlsx', index= False )
Output :
"VISIT 10"
"VISIT 10.1"
"VISIT 10.2"
Visitname
"VISIT 10.2"
Upvotes: 0
Views: 79
Reputation: 1326
There is a much simpler way to do this using apply
but to keep with the way you have it here.
You haven't added anything to your array visit_id
so that's why you are only getting one value.
Try this.
unique = Contains['VISIT'].unique().tolist()
i = 0
visit_id = []
sizeofList = len(unique)
while i < sizeofList :
g = unique[i]
visit_id.append('"'+g+'"')
i += 1
print(visit_id)
df = pd.DataFrame({'Visitname': [visit_id]})
df.to_excel('Visits.xlsx', index= False )
Upvotes: 1