首页 > 解决方案 > Display only one value of jinja2 loop iteration

问题描述

I´m doing a flask app and I´m having a pandas df and iterate through it with a jinja2 loop. I get all the values of my df printed on my page, but i was wondering how i can get only one value displayed as i couldn´t find anything on the web. I´m new to stackoverflow so please tell me if you need more information. thanks in advance.

jinja2 code:

{% for row in dataframe.iterrows() %}
    {{row[0]}}: {{row[1][0]}}
{% endfor %}

标签: pythonflaskjinja2

解决方案


Try using the answer from the following question as a starting point. : Accessing every 1st element of Pandas DataFrame column containing lists

Generally, accessing the first row of a dataframe is easy and is done in the following way,

{{ df.head(1) }}

Accessing the first element of each row can be done by iterating over the dataframe as you have done.

Accessing the first element of the first row can be done in the following way,

{{ df["first_column_name"].str[0] }}

I hope any one of the above mentioned solutions can solve your problem.


推荐阅读