I have a dataframe with 1,000s of URLs and company names that I need to convert to HTML links as well as do some formatting. I wrote a function that can go down the list and create the tags:
def linkcreate():
if row['url'] == '####' or row['url'] == '#####':
print('<span style="color: #293789!important; margin-bottom:0;">' + row['name'] + '</span>')
else:
print('<a href="' + row['url'] + '" target="_blank">' + row['name'] + '</a>')
The if statement is doing a bit of a clean up since there are a few dozen companies that do not have a url. Those are represented as '####' and '#####' in the df. For those, I am adding a span tag instead of a tag with some styling that will look like a link. else statement just constructs the link based on two columns in the df.
Another thing I wanted to do was put the half of the links in and the second half in . Below is my code with explanation:
# Calculates the middle point from the total count of rows in df
count = (int(data['url'].count()))/2
# Set counter to 0
counter = 0
for index, row in data.iterrows():
counter = counter + 1
# Before the first <a> tag start the first section <div>
if counter == 1:
print('<div class="side-1">')
# If counter is less then or equals to the half point of rows in df build the links using the linkcreate()
elif counter <= count:
linkcreate()
# If counter is +1 from the half way point of rows add the closing </div> and start the second <div>
elif counter == count + 1:
print('</div>')
print(' ')
print('<div class="side-2">')
# If counter is greater then the half point of rows in df build the rest of the links using the linkcreate()
elif counter > count:
linkcreate()
# Closing </div> tag for the second set of links.
print('</div>')
This code works but is it the most efficient way to do this?