# Python scripts in Loops

**URL:** <https://forum.knime.com/t/python-scripts-in-loops/62798>\
**Category:** KNIME Extensions\
**Created:** [February 28, 2023, 3:30pm UTC](https://forum.knime.com/t/python-scripts-in-loops/62798 "2023-02-28T15:30:56Z")\
**Posts on this page:** 1\
**Showing post:** 3

<div class="post-metadata">

**Author:** ![mlauber71](https://forum-cdn.knime.com/user_avatar/forum.knime.com/mlauber71/32/73590_2.png) [@mlauber71](https://forum.knime.com/u/mlauber71)\
**Post date:** [February 28, 2023, 5:21pm UTC](https://forum.knime.com/t/python-scripts-in-loops/62798/3 "2023-02-28T17:21:36Z")

</div>

@ActionAndi a solution could look something like this:

 ![image](https://forum-cdn.knime.com/uploads/default/original/3X/d/d/ddf02f4dc40a9c043898294ed5a50e01ba1444ea.png)

> **[Python - loop thru data rows and create PNG files within a loop - store which...](https://hub.knime.com/mlauber71/spaces/Public/latest/kn_example_python_loop_graphic_restart~2bCmw6mkvVrBlTa0)**
>
> Python - loop thru data rows and create PNG files within a loop - store which items have already been processed

[kn\_example\_python\_loop\_graphic\_restart.knwf](https://forum.knime.com/uploads/short-url/vXNJsHxRLJOImecmskqvFYjVCA.knwf) (164.6 KB)

The script would look like this:

```auto
import knime.scripting.io as knio

# This example script creates an output table containing randomly drawn integers using numpy and pandas.

import numpy as np
import pandas as pd

import matplotlib.pyplot as plt

var_data_path = knio.flow_variables['context.workflow.data-path']

# Load your data into a Pandas DataFrame
# df = pd.read_csv(var_data_path + "your_data.csv")

df = knio.input_tables[0].to_pandas()

# Load the parquet file containing previously processed rows
try:
    processed_df = pd.read_parquet(var_data_path + "output.parquet")
except FileNotFoundError:
    processed_df = pd.DataFrame(columns=["row_id", "name_column", "timestamp"])

# Loop through each row in the DataFrame
for index, row in df.iterrows():

    # Check if this row has already been processed
    if any(processed_df["row_id"] == index):
        continue

    # Create a plot based on the data in this row
    x = row["x_values"]
    y = row["y_values"]
    plt.plot(x, y)

    # Set the file name based on a column in the DataFrame
    file_name = f"{row['name_column']}.png"

    # Save the plot to disk with the file name
    plt.savefig(var_data_path + file_name)

    # Save the row ID to the parquet file
    new_row = pd.DataFrame({"row_id": [index], "name_column": [row["name_column"]],
                            "timestamp": [pd.Timestamp.now()]})
    processed_df = pd.concat([processed_df, new_row], ignore_index=True)

    # Save the processed rows to the parquet file
    processed_df.to_parquet(var_data_path + "output.parquet", index=False)

    # Close the plot
    plt.close()

knio.output_tables[0] = knio.Table.from_pandas(df)

```

---

_[View the full topic](https://forum.knime.com/t/python-scripts-in-loops/62798)._
