如何将一列字符串转换为超链接?
How can I convert a column of strings into hyperlink?
我想将 link 的列转换为 hyperlink 以便在数据 table 上显示它。
第 links 列如下所示:
link
0 https://twitter.com/CertSG/status/1286557929198563328
1 https://twitter.com/osiseguridad/status/1286565901568016384
我正在考虑创建一个函数来将字符串 hyperlink 转换为 <a href = "x" </a>
,其中 x
是 0
、1
和别的。我也在想 for 可以帮助我做到这一点,但我真的不知道什么是更好的方法
type(df.link)
是 pandas.core.series.Series
我正在使用 pandas 3.8.5
我希望我的 table 在 table 添加 links https://plotly.com/python/figure-factory-table/
中看起来像这样
非常感谢,我是新来的
您可以使用 df.apply(convert, axis=1)
在每一行上 运行 自己的函数 convert()
。
import pandas as pd
df = pd.DataFrame({
'link': [
'https://twitter.com/CertSG/status/1286557929198563328',
'https://twitter.com/osiseguridad/status/1286565901568016384'
]
})
def convert(row):
#print(row)
return '<a href="{}">{}</a>'.format(row['link'], row.name)
df['link'] = df.apply(convert, axis=1)
print(df)
# Display it with `plotly`
import plotly.figure_factory as ff
fig = ff.create_table(df)
fig.show()
编辑:直接显示在streamlit
import pandas as pd
df = pd.DataFrame({
'link': [
'https://twitter.com/CertSG/status/1286557929198563328',
'https://twitter.com/osiseguridad/status/1286565901568016384'
]
})
def convert(row):
#print(row)
return '<a href="{}">{}</a>'.format(row['link'], row.name)
df['link'] = df.apply(convert, axis=1)
print(df)
import streamlit as st
st.write(df.to_html(escape=False), unsafe_allow_html=True)
我想将 link 的列转换为 hyperlink 以便在数据 table 上显示它。
第 links 列如下所示:
link
0 https://twitter.com/CertSG/status/1286557929198563328
1 https://twitter.com/osiseguridad/status/1286565901568016384
我正在考虑创建一个函数来将字符串 hyperlink 转换为 <a href = "x" </a>
,其中 x
是 0
、1
和别的。我也在想 for 可以帮助我做到这一点,但我真的不知道什么是更好的方法
type(df.link)
是 pandas.core.series.Series
我正在使用 pandas 3.8.5
我希望我的 table 在 table 添加 links https://plotly.com/python/figure-factory-table/
中看起来像这样非常感谢,我是新来的
您可以使用 df.apply(convert, axis=1)
在每一行上 运行 自己的函数 convert()
。
import pandas as pd
df = pd.DataFrame({
'link': [
'https://twitter.com/CertSG/status/1286557929198563328',
'https://twitter.com/osiseguridad/status/1286565901568016384'
]
})
def convert(row):
#print(row)
return '<a href="{}">{}</a>'.format(row['link'], row.name)
df['link'] = df.apply(convert, axis=1)
print(df)
# Display it with `plotly`
import plotly.figure_factory as ff
fig = ff.create_table(df)
fig.show()
编辑:直接显示在streamlit
import pandas as pd
df = pd.DataFrame({
'link': [
'https://twitter.com/CertSG/status/1286557929198563328',
'https://twitter.com/osiseguridad/status/1286565901568016384'
]
})
def convert(row):
#print(row)
return '<a href="{}">{}</a>'.format(row['link'], row.name)
df['link'] = df.apply(convert, axis=1)
print(df)
import streamlit as st
st.write(df.to_html(escape=False), unsafe_allow_html=True)