gpt4 book ai didi

Python 将 HTML 箭头显示到数据框

转载 作者:行者123 更新时间:2023-12-02 14:57:35 26 4
gpt4 key购买 nike

我创建了一个数据框 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>&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

在浏览器中输出文件:

Output file in browser

关于Python 将 HTML 箭头显示到数据框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52247052/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com