我创建了一个数据框 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
.
.
.
现在我尝试用向下箭头替换方向值 0,用向上箭头替换方向值 1。向下箭头的 HTML 代码是 ಓ
尝试下面的代码示例:
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
浏览器中的输出文件:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)