Python показывает стрелки HTML в dataframe - PullRequest
0 голосов
/ 09 сентября 2018

Я создал dfframe,

           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-код для стрелки вниз - & # 3219

Ответы [ 2 ]

0 голосов
/ 09 сентября 2018

Попробуйте пример кода ниже:

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

0 голосов
/ 09 сентября 2018

Использование

df['direction'] = df['direction'].map({0: '↓', 1: '↑'})

import pandas as pd

Title = ('jimmy', 'red', 'blue', 'brad', 'oranges')
value = (82, 38 , 55, 19, 33)
direction = (0, 1, 1, 0, 0)

df = pd.DataFrame({'Title': Title, 'value': value,'direction':direction})
df['direction'] = df['direction'].map({0: '↓', 1: '↑'})
df

выход

    Title   value   direction
0   jimmy   82      ↓
1   red     38      ↑
2   blue    55      ↑
3   brad    19      ↓
4   oranges 33      ↓
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...