我有一个数据框,在其中我按一列进行分组,然后将该数据框转换为字典。
context['data'] = "carrier_data": noDupsdf.groupby("market").apply(dict)
结果数据如下:
"ATL": {
"currency": ["USD", "EUR", "CAD"],
"amount": [1232, 22, 44],
},
"JFK": {
"currency": ["GBP", "EUR"],
"amount": [122, 442],
}
我想遍历数据并进行相应显示:
<ul style="list-style: none">
<li style="display: inline-block">ATL</li>
<li style="display: inline-block">JFK</li>
</ul>
<div class="first-container">
<div class="card">
<p>Currency: USD</p>
<p>Amount: 1232</p>
</div>
<div class="card">
<p>Currency: EUR</p>
<p>Amount: 22</p>
</div>
<div class="card">
<p>Currency: CAD</p>
<p>Amount: 44</p>
</div>
<div class="second-container">
<div class="card">
<p>Currency: GBP</p>
<p>Amount: 122</p>
</div>
<div class="card">
<p>Currency: EUR</p>
<p>Amount: 442</p>
</div>
最多会有三个市场。如何以这种方式显示数据?
答案 0 :(得分:0)
您必须稍微转换结果字典
context['data'] = {market_name: zip(currency_data['currency'], currency_data['amount']) for market_name, currency_data in data.items()}
然后您的模板将如下所示:
{% for market_name, currency_data in data.items %}
<div>
{% for currency, amount in currency_data %}
<div class="card">
<p>Currency: {{ currency }}</p>
<p>Amount: {{ amount }}</p>
</div>
{% endfor %}
</div>
{% endfor %}