Python向数据框显示HTML箭头 [英] Python show HTML arrows to dataframe

查看:135
本文介绍了Python向数据框显示HTML箭头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个datframe df,

I created a datframe df,

           Value     Change        Direction    
Date                                    
2015-03-02  2117.38   NaN              0        
2015-03-03  2107.79  -9.609864         0    
2015-03-04  2098.59  -9.250000         0    
2015-03-05  2101.04   2.510010         1    
2015-03-06  2071.26  -29.780029        0
. 
.
.

现在,我正尝试将Direction值0替换为向下箭头,将1替换为向上箭头.向下箭头的HTML代码为&#3219

Now I am trying to replace the Direction value 0 by down arrow and 1 by up arrow. HTML code for down arrow is &#3219

推荐答案

尝试以下代码示例:

import pandas as pd

Fruits = ('Apple', 'Mango', 'Grapes', 'Banana', 'orange')
Price = (100, 80, 50, 90, 60)
direction = (1, 0, 1, 1, 0)

df = pd.DataFrame({'Fruits': Fruits, 'Price': Price,'direction':direction})

df.direction.replace([1, 0],["↑", "↓"], inplace=True)    #replace the values using html codes for up and down arrow.

html_df= df.to_html()    #convert the df to html for better formatting view

html_df = html_df.replace("<td>&amp;#8595;</td>","<td><font color = red>&#8595;</font></td>")    # Remove extra tags and added red colour to down arrow
html_df = html_df.replace("<td>&amp;#8593;</td>","<td><font color = green>&#8593;</font></td>")    # Remove extra tags and added green colour to up arrow

print(html_df)    #print the df

在浏览器中输出文件:

这篇关于Python向数据框显示HTML箭头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆