Python向dataframe显示HTML箭头
我创建了一个datframe dfPython向dataframe显示HTML箭头,python,html,pandas,dataframe,replace,Python,Html,Pandas,Dataframe,Replace,我创建了一个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 201
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代码是ಓ
df['direction']=df['direction'].map({0:'↓', 1: '↑'})代码>
输出
Title value direction
0 jimmy 82 ↓
1 red 38 ↑
2 blue 55 ↑
3 brad 19 ↓
4 oranges 33 ↓
请尝试以下代码示例:
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
将熊猫作为pd导入
水果=(‘苹果’、‘芒果’、‘葡萄’、‘香蕉’、‘橙子’)
价格=(100,80,50,90,60)
方向=(1,0,1,1,0)
df=pd.DataFrame({'Fruits':Fruits,'Price':Price,'direction':direction})
df.direction.replace([1,0],“↑;”,“↓;”],inplace=True)#使用html代码替换上下箭头的值。
html_df=df.to_html()#将df转换为html以获得更好的格式视图
html#df=html#df.replace(“&;#8595;”、“&;#8595;”)#删除额外的标记并向向下箭头添加红色
html#df=html#df.replace(“&;#8593;”、“&;#8593;”)#删除额外的标记并向上箭头添加绿色
打印(html_df)#打印df
浏览器中的输出文件:
您应该使用一个简单的映射,如下所示:df['Direction']=df['Direction'].map({0:'&3219',1:'codeforup'))
。现在,如果这在您的UI中显示箭头,我不知道。感谢回复@AntonvBR,但它显示字符串“ಓ”而不是箭头。不要使用Html代码。只需复制符号并使用它。从这里复制:-谢谢@Chadila07是否可以更改方向列的颜色,例如绿色表示向上,红色表示向下。@aakashingh尽管您没有更改方向列的颜色您的问题中没有询问颜色,但可以为箭头添加颜色。我已编辑了我的答案,为箭头添加颜色,并更新了输出。请检查此项。@aakashsingh如果您认为答案正确,请接受答案。@Chandila07:如果您仍在使用此选项,我需要询问您这不会产生table显示在您的输出中。相反,它向我显示了一个纯html标记的代码。如何修复该问题并在显示时查看输出here@Django0602在cli或控制台上,您将仅看到文本形式的html,但如果需要html视图(呈现)然后,您需要将cli输出重定向到某个扩展名为.html的文件,并在浏览器上打开它。您可以看到我编写了“output file in browser”(在浏览器中输出文件),因此,您只能在浏览器中看到此输出,因为它呈现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