我正在尝试对现有数据框实施某些样式设计,但一直出现错误。我尝试按照看似简单明了但似乎找不到this issue
的答案的pandas文档进行操作我试图在移动到数据透视表之前查看df.style
是否在为我的表工作,但是它没有显示模板中的任何更改。
DataFrame:
DATE FINAL_SCORE FN G L MODEL_SCORE R RV SN TC TICKER_id id
0 2019-05-20 1.5 1.5 0.0 0.0 1.5 0.0 1.5 1.5 1.0 EWA 0001
1 2019-05-21 1.5 1.5 0.0 0.0 1.5 0.0 1.5 1.0 1.0 EWA 0002
2 2019-05-22 1.5 1.5 0.0 0.0 1.5 0.0 1.5 1.0 1.0 EWA 0003
3 2019-05-23 1.5 1.5 0.0 0.0 1.5 0.0 1.5 1.0 1.0 EWA 0004
4 2019-05-24 1.5 1.5 0.0 0.0 1.5 0.0 1.5 1.0 1.0 EWA 0005
这是我的views.py
index()
和color_negative_red()
的一部分:
def color_negative_red(val):
color = 'red' if (-2.0 <= val >= 2.0) else 'black'
return 'color: %s' % color
def index(request):
scores = overall_scores.objects.filter(TICKER__in = countries, DATE = last_date).values()
lag_scores_df = pd.DataFrame([x for x in overall_scores.objects.filter(TICKER__in = countries, DATE__gte = lag_date).values()])
lag_scores_df = lag_scores_df.pivot_table(index = 'DATE', columns = 'TICKER_id', values = 'FINAL_SCORE')
lag_scores_df.style.applymap(color_negative_red)
sector_top_scores = sorted(sector_scores, key = lambda i: i['FINAL_SCORE'], reverse = True)[:5]
context = {'sorted_top_scores': sorted_top_scores}
return render(request, template, context)
index.html:
<table class="table table-hover">
<thead>
<tr>
<th scope="col">Ticker</th>
<th scope="col">Final</th>
<th scope="col">G</th>
<th scope="col">L</th>
<th scope="col">R</th>
<th scope="col">FN</th>
<th scope="col">RV</th>
<th scope="col">SN</th>
<th scope="col">TC</th>
</tr>
</thead>
<tbody>
{% for ticker in sorted_top_scores %}
<tr>
<td>{{ ticker.TICKER_id }}</td>
<td>{{ ticker.FINAL_SCORE }}</td>
<td>{{ ticker.G }}</td>
<td>{{ ticker.L }}</td>
<td>{{ ticker.R }}</td>
<td>{{ ticker.FN }}</td>
<td>{{ ticker.RV }}</td>
<td>{{ ticker.SN }}</td>
<td>{{ ticker.TC }}</td>
</tr>
{% endfor %}
</tbody>
</table>