fipse
fipse

Reputation: 65

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 %}

Upvotes: 0

Views: 1520

Answers (1)

Harris Minhas
Harris Minhas

Reputation: 790

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.

Upvotes: 1

Related Questions