Adam
Adam

Reputation: 325

insert anchor tags using python in pandas dataframe

I have a list of urls and a list of descriptions in separate columns in a pandas dataframe. I'm trying to figure out a way to use python to insert an anchor tag with a link in the first few words of the description.

I have:

df.description[0]
"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt 
ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation"

df.link[0]
https://www.google.com

Desired output:

df.complete[0]
<a href="https://www.google.com"> Lorem ipsum dolor sit amet</a>, consectetur adipiscing elit, 
sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim 
veniam, quis nostrud exercitation

The text from the description column varies in length. I'm trying to make sure the hyperlink ends on the last character of a word - the number of words wrapped in the anchor tag don't matter.

Upvotes: 0

Views: 408

Answers (2)

Adam
Adam

Reputation: 325

My answer:

df["short_description"] = df["description"].apply(lambda s: shorten(s, width=20, placeholder=""))
df['remaining_desc'] = df.apply(lambda row : row['description'].replace(str(row['short_description']), ''), axis=1)
df['remaining_desc']

df["description_link"] = '<a href="' + df["story_link"] + '">' + df["short_description"] + "</a>" + df["remaining_desc"]
df['description_link']

Upvotes: 0

Fred
Fred

Reputation: 502

You can use a combination of textwrap.shorten and string manipulation. Try something like this:

from textwrap import shorten

# Change 20 to any value that makes sense
df["short_description"] = df["description"].apply(lambda s: shorten(s, width=20, placeholder=""))

df["description_link"] = (
    # Create the start of the anchor tag
    '<a href="' + df["link"] + '">'

    # Add in the short description text
    df["short_description"]

    # Close the anchor tag
    + "</a>"

    # Append the rest of the description
    + df["description"].str[df["short_description"].str.len:]
)

Upvotes: 1

Related Questions