本文介绍了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代码为ಓ
Now I am trying to replace the Direction value 0 by down arrow and 1 by up arrow. HTML code for down arrow is ಓ
推荐答案
尝试以下代码示例:
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>&#8595;</td>","<td><font color = red>↓</font></td>") # Remove extra tags and added red colour to down arrow
html_df = html_df.replace("<td>&#8593;</td>","<td><font color = green>↑</font></td>") # Remove extra tags and added green colour to up arrow
print(html_df) #print the df
在浏览器中输出文件:
这篇关于Python向数据框显示HTML箭头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!