Search code examples
pythonpandasjinja2

How to remove duplicates rows in for loop


I need to remove the perfect duplicates rows in loop when I use itertuples in jinja2:

{% for rows in df.itertuples() %}
    {% set value = rows['value'] %}
    {{value}}
{% endfor %}

at the end of loop, I have a duplicates value like:

123 
456
123
456
123
456

I need to remove the duplicates but I don't found the solution Do you have an idea?

The result I want is:

123
456

Solution

  • You could change your code to drop duplicates in df:

    {% for rows in df.drop_duplicates().itertuples() %}
        {% set value = rows['value'] %}
        {{value}}
    {% endfor %} 
    

    Here's a link to the documentation for drop_duplicates(): https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.drop_duplicates.html

    Output:

    123
    456