简体   繁体   中英

Apply string function to data frame

The task is to wrap URLs in excel file with html tag. For this, I have a fucntion and the following code that works for one column named ANSWER:

import pandas as pd
import numpy as np
import string
import re

def hyperlinksWrapper(myString):
    #finding all substrings that look like a URL

    URLs = re.findall("(?P<url>https?://[^','')'' ''<'';'\s\n]+)", myString)
    #print(URLs)
    
    #replacing each URL by a link wrapped into <a> html-tags
    for link in URLs:
        wrappedLink = '<a href="' + link + '">' + link + '</a>'
        myString = myString.replace(link, wrappedLink)
    
    return(myString)
#Opening the original XLS file
filename = "Excel.xlsx"
df = pd.read_excel(filename)

#Filling all the empty cells in the ANSWER cell with the value "n/a"
df.ANSWER.replace(np.NaN, "n/a", inplace=True)

#Going through the ANSWER column and applying hyperlinksWrapper to each cell
for i in range(len(df.ANSWER)):
    df.ANSWER[i] = hyperlinksWrapper(df.ANSWER[i])

#Export to CSV
df.to_excel('Excel_refined.xlsx')

The question is, how do I look not in one column, but in all the columns (each cell) in the dataframe without specifying the exact column names?

Perhaps you're looking for something like this:

import pandas as pd
import numpy as np
import string
import re

def hyperlinksWrapper(myString):
    #finding all substrings that look like a URL

    URLs = re.findall("(?P<url>https?://[^','')'' ''<'';'\s\n]+)", myString)
    #print(URLs)
    
    #replacing each URL by a link wrapped into <a> html-tags
    for link in URLs:
        wrappedLink = '<a href="' + link + '">' + link + '</a>'
        myString = myString.replace(link, wrappedLink)
    
    return(myString)

# dummy dataframe
df = pd.DataFrame(
    {'answer_col1': ['https://example.com', 'https://example.org', np.nan], 
     'answer_col2': ['https://example.net', 'Hello', 'World']}
)

# as suggested in the comments (replaces all NaNs in df)
df.fillna("n/a", inplace=True)

# option 1
# loops over every column of df
for col in df.columns:
    # applies hyperlinksWrapper to every row in col
    df[col] = df[col].apply(hyperlinksWrapper)
    
# [UPDATED] option 2
# applies hyperlinksWrapper to every element of df
df = df.applymap(hyperlinksWrapper) 

df.head()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM