programing

Pandas 데이터 프레임에서 만든 Excel 시트에 하이퍼링크를 _http 방법에 추가합니다.

jooyons 2023. 6. 19. 21:27
반응형

Pandas 데이터 프레임에서 만든 Excel 시트에 하이퍼링크를 _http 방법에 추가합니다.

를 사용하여 판다 데이터 프레임을 엑셀 시트로 변환하였습니다.

이제 한 열에 있는 값에 하이퍼링크를 추가하려고 합니다.즉, 고객이 내 Excel 시트를 보면 셀을 클릭하여 웹 페이지를 열 수 있습니다(이 셀의 값에 따라 다름).

우리가 사용할 수 없는 @guillaume-jacquenot의 접근 방식을 기반으로 구축.apply이를 전체 시리즈에 적용할 수 있습니다.

df = pd.DataFrame({'Year': [2000, 2001, 2002 , 2003]})

청결을 위해 도우미 방법을 썼습니다.

def make_hyperlink(value):
    url = "https://custom.url/{}"
    return '=HYPERLINK("%s", "%s")' % (url.format(value), value)

그리고나서,apply시리즈로 이동:

df['hyperlink'] = df['Year'].apply(make_hyperlink)
    Year    hyperlink
0   2000    =HYPERLINK("https://custom.url/2000", "2000")
1   2001    =HYPERLINK("https://custom.url/2001", "2001")
2   2002    =HYPERLINK("https://custom.url/2002", "2002")
3   2003    =HYPERLINK("https://custom.url/2003", "2003")

당신은 사용할 수 있습니다.HYPERLINK기능.

import pandas as pd
df = pd.DataFrame({'link':['=HYPERLINK("http://www.someurl.com", "some website")']})
df.to_excel('test.xlsx')

@maxymoo의 대답에서, 여기 완전한 예가 있습니다.

import pandas as pd
df = pd.DataFrame({'Year': [2000, 2001, 2002 , 2003]})
df['link'] = '-'
df.set_value(0, 'link', '=HYPERLINK("https://en.wikipedia.org/wiki/2000", 2000)')
df.set_value(1, 'link', '=HYPERLINK("https://en.wikipedia.org/wiki/2001", 2001)')
df.set_value(2, 'link', '=HYPERLINK("https://en.wikipedia.org/wiki/2002", 2002)')
df.set_value(3, 'link', '=HYPERLINK("https://en.wikipedia.org/wiki/2003", 2003)')
df.to_excel('test.xlsx', index = False)

사용할 수 있는 항목:

df = pd.DataFrame(list(range(5)), columns=['a'])

df['a'] = df['a'].apply(lambda x: '<a href="http://youtube.com/{0}">link</a>'.format(x))

HTML(df.to_html(escape=False))

Excel 파일에서 텍스트 파일을 생성하던 중 생성된 .txt 파일의 이름을 Dataframe의 특정 기존 열에 연결하고 싶었습니다.

생성된 .txt 파일이 저장된 로컬 드라이브 디렉터리를 해당 "파일 이름"으로 푸시하려고 했습니다.파일 이름을 클릭하면 .txt 파일이 열립니다.

The dataframe

rancheck_DF = pd.read_excel(excel_file, delim_whitespace = True, encoding = 'utf-8')

for index_df in range(len(rancheck_DF)):

    Desc = rancheck_DF.loc[index_df,'Description Text']

    MainFile = rancheck_DF.loc[index_df,'File Name']

    fileName = r'.\Documents\TestF\TestF_{}.txt'.format(index_df)

    with open(fileName, 'w', encoding='utf-8') as txtfile:
        txtfile.write(Desc)

    rancheck_DF.loc[index_df,'File Name'] = '=HYPERLINK("{}","{}")'.format(fileName,MainFile)

rancheck_DF.to_excel('./damn.xlsx', index=None)

언급URL : https://stackoverflow.com/questions/31820069/add-hyperlink-to-excel-sheet-created-by-pandas-dataframe-to-excel-method

반응형