Python script loop over rows

Hi there,

I am new to Knime and need a quick pointer to Python script - how to loop over each row?

example:

Sales Quantity Revenue
10 2
7 3

How to read the input table and loop over Revenue for Sales x Quantity = Revenue?

I find some examples of python scripting is invalid and cannot give insights.

Hi anguslou,

Tables in KNIME’s Python snippet nodes (e.g., input_table and output_table in the Python Script (1⇒1) node) are represented as pandas DataFrames. Please refer to the pandas documentation or other related resources (e.g., this one) to find out how to use DataFrames.

Would you mind pointing to these examples and possibly describe what problems you experienced with them? Then we can review them for correctness. Thanks!

Marcel

1 Like

Hi Marcel,

Thanks, you pointed me to right direction.

I was running the example in 07_Scripting/03_Python/03_Fetch_ChEMBL_Target_Data and get an error message when running Retreive assays, activities, and targets nodes.

ERROR PostgreSQL Connector 5:238:0:236 Execute failed: Connection to localhost:5432 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.

Hi anguslou,

Glad I could help!

Yes, this is probably not a good fit for an introductory example of how to use Python in KNIME. It requires you to have a specific database server running and to provide the connection information to that server to some nodes in the workflow (this is also stated in the yellow-bordered workflow annotation box at the top of the workflow).
In fact, there are, unfortunately, no really basic examples in the 07_Scripting/03_Python directory of our examples server, at least not yet. Perhaps searching the KNIME Hub can help you find better suited example workflows provided by the community, for the time being.

Marcel

1 Like

You could try the workflows on the hub that are related to the KNIME Python integration.

And tow very basic examples.
https://hub.knime.com/mlauber71/spaces/Public/latest/kn_example_python_iris

https://hub.knime.com/mlauber71/spaces/Public/latest/kn_example_python_iris_graphics

https://hub.knime.com/mlauber71/spaces/Public/latest/kn_example_python_numpy_save

4 Likes