HTML 在 Pandas DataFrame 中格式化
HTML formatting in Pandas DataFrame
在 IPython 笔记本中使用 Pandas DataFrame 时,是否有任何方法可以在其中添加格式化列?我想添加一个数据 URL <img>
并将其显示在单元格中。
这是一个例子:
from IPython.display import HTML
df = pd.DataFrame({'A': np.linspace(1, 10, 10)})
# this file is in the same folder as the notebook I'm using on my drive
df['image'] = 'so-logo.png'
df['new'] = df['image'].apply(lambda x: '<img src="{}"/>'.format(x) if x else '')
HTML(df.to_html(escape=False))
或者您可以使用 df.style
而不是 IPython.display.HTML
。这也允许您添加其他格式,例如在悬停时突出显示该行:
from IPython.display import HTML
df = pd.DataFrame({'A': np.linspace(1, 10, 10)})
df['image'] = 'so-logo.png'
df['new'] = df['image'].apply(lambda x: '<img src="{}"/>'.format(x) if x else '')
def hover(hover_color="#ffff99"):
return dict(selector="tr:hover",
props=[("background-color", "%s" % hover_color)])
styles = [
hover(),
dict(selector="th", props=[("font-size", "150%"),
("text-align", "center")]),
dict(selector="caption", props=[("caption-side", "bottom")])
]
html = (df.style.set_table_styles(styles)
.set_caption("Hover to highlight."))
html
我的光标在此处的第二行:
如果您的 src
网址太长,pandas 会在显示时截断它们。您需要增加最大列宽以适应这一点。
跟踪当前最大列宽
current_max_colwidth = pd.get_option('display.max_colwidth')
print(current_max_colwidth)
50
将选项设置得大一些
pd.set_option('display.max_colwidth', 1000)
导入HTML
显示函数
from IPython.display import HTML
使用带有参数 escape=False
的数据帧 to_html
HTML(
pd.DataFrame([
"""<img src="http://whosebug.com/users/flair/2336654.png?theme=default">""",
"""<img src="http://whosebug.com/users/flair/2543372.png?theme=default">""",
"""<img src="http://whosebug.com/users/flair/44330.png?theme=default">"""
]).to_html(escape=False))
设置回选项
pd.set_option('display.max_colwidth', current_max_colwidth)
将其包装在一个整洁的函数中
def my_to_html(df):
current_max_colwidth = pd.get_option('display.max_colwidth')
pd.set_option('display.max_colwidth', 1000)
from IPython.display import HTML
my_html = HTML(df.to_html(escape=False))
pd.set_option('display.max_colwidth', current_max_colwidth)
return my_html
df = pd.DataFrame([
"""<img src="http://whosebug.com/users/flair/2336654.png?theme=default">""",
"""<img src="http://whosebug.com/users/flair/2543372.png?theme=default">""",
"""<img src="http://whosebug.com/users/flair/44330.png?theme=default">"""
])
my_to_html(df)
在 IPython 笔记本中使用 Pandas DataFrame 时,是否有任何方法可以在其中添加格式化列?我想添加一个数据 URL <img>
并将其显示在单元格中。
这是一个例子:
from IPython.display import HTML
df = pd.DataFrame({'A': np.linspace(1, 10, 10)})
# this file is in the same folder as the notebook I'm using on my drive
df['image'] = 'so-logo.png'
df['new'] = df['image'].apply(lambda x: '<img src="{}"/>'.format(x) if x else '')
HTML(df.to_html(escape=False))
或者您可以使用 df.style
而不是 IPython.display.HTML
。这也允许您添加其他格式,例如在悬停时突出显示该行:
from IPython.display import HTML
df = pd.DataFrame({'A': np.linspace(1, 10, 10)})
df['image'] = 'so-logo.png'
df['new'] = df['image'].apply(lambda x: '<img src="{}"/>'.format(x) if x else '')
def hover(hover_color="#ffff99"):
return dict(selector="tr:hover",
props=[("background-color", "%s" % hover_color)])
styles = [
hover(),
dict(selector="th", props=[("font-size", "150%"),
("text-align", "center")]),
dict(selector="caption", props=[("caption-side", "bottom")])
]
html = (df.style.set_table_styles(styles)
.set_caption("Hover to highlight."))
html
我的光标在此处的第二行:
如果您的 src
网址太长,pandas 会在显示时截断它们。您需要增加最大列宽以适应这一点。
跟踪当前最大列宽
current_max_colwidth = pd.get_option('display.max_colwidth')
print(current_max_colwidth)
50
将选项设置得大一些
pd.set_option('display.max_colwidth', 1000)
导入HTML
显示函数
from IPython.display import HTML
使用带有参数 escape=False
to_html
HTML(
pd.DataFrame([
"""<img src="http://whosebug.com/users/flair/2336654.png?theme=default">""",
"""<img src="http://whosebug.com/users/flair/2543372.png?theme=default">""",
"""<img src="http://whosebug.com/users/flair/44330.png?theme=default">"""
]).to_html(escape=False))
设置回选项
pd.set_option('display.max_colwidth', current_max_colwidth)
将其包装在一个整洁的函数中
def my_to_html(df):
current_max_colwidth = pd.get_option('display.max_colwidth')
pd.set_option('display.max_colwidth', 1000)
from IPython.display import HTML
my_html = HTML(df.to_html(escape=False))
pd.set_option('display.max_colwidth', current_max_colwidth)
return my_html
df = pd.DataFrame([
"""<img src="http://whosebug.com/users/flair/2336654.png?theme=default">""",
"""<img src="http://whosebug.com/users/flair/2543372.png?theme=default">""",
"""<img src="http://whosebug.com/users/flair/44330.png?theme=default">"""
])
my_to_html(df)