[英]How can I convert a column of strings into hyperlink?
I want to convert the column of link as a hyperlink for show it on a datatable.我想将链接列转换为超链接以在数据表上显示它。
The column links looks like this:列链接如下所示:
link
0 https://twitter.com/CertSG/status/1286557929198563328
1 https://twitter.com/osiseguridad/status/1286565901568016384
I was thinking about create a function to convert de string hyperlink with <a href = "x" </a>
where x
is the value of 0
, 1
and else.我正在考虑创建一个函数来转换带有
<a href = "x" </a>
字符串超链接,其中x
是0
、 1
和其他的值。 I was also thinking that a for could help me to made it but I really don´t know what's the better way我还以为 for 可以帮助我做到这一点,但我真的不知道什么是更好的方法
the type(df.link)
is pandas.core.series.Series
I´m using pandas 3.8.5 type(df.link)
是pandas.core.series.Series
我使用的是pandas 3.8.5
I want that my table looks like this in the table add links https://plotly.com/python/figure-factory-table/我希望我的表格在表格中看起来像这样添加链接https://plotly.com/python/figure-factory-table/
Thank you very much, i´m new in this非常感谢,我是新来的
You can use df.apply(convert, axis=1)
to run own function convert()
on every row.您可以使用
df.apply(convert, axis=1)
convert()
在每一行上运行自己的函数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()
EDIT: Display directly in streamlit
编辑:直接以
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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.