← Back to list

KNIME Snippets (5) — Python Overview

KNIME Analytics Platform is a low-code platform, so you can easily integrate the power of Python with KNIME

Markus Lauber in Low Code for Data Science · 2024-10-31 19:18 · 173 claps · 4.2 min read
#python #knime #pandas #knime-tutorial #getting-started
Open on Medium ↗
Wiki topics: GRW · Growth & Analytics 🐾 · Pets & Animals

GETTING STARTED | PYTHON | KNIME ANALYTICS PLATFORM

KNIME Snippets (5) — Python Overview

KNIME Analytics Platform is a low-code platform, so you can easily integrate the power of Python with KNIME

1 — Python is already on-board, no need for messy conda installations

The installation and management of Python (packages) and dependencies sometimes can be quite complicated. So it is great that KNIME has a lot of basic Python functions and packages already on-board encapsulated in a nice extension that you can just install like any other:

[embed]How to set up Python extensions in KNIME | KNIME Here is a quick step by step guide on how to install Python, get it working in KNIME, and also configure the Python…www.knime.com

2 — Basic Python Script in KNIME

After you have installed the extension you can start right away with writing a Python Script. The basic input and output does look like in this example:

Basic data in and out for the KNIME Python Script. Inside you have your pandas data frame (df) — (https://hub.knime.com/s/fLRIlwhYLlDj4skh)

Basic data in and out for the KNIME Python Script. Inside you have your pandas data frame (df) — (https://hub.knime.com/s/fLRIlwhYLlDj4skh)

Just make sure that after your processes inside Python are finished you have your data again as a Pandas Data frame (df). The number in brackets corresponds to the order of the KNIME data ports of the node. You can always add or remove ports if you like.

# import the basic KNIME Python Package
import knime.scripting.io as knio

# import additional packages
import numpy as np
import pandas as pd

# data being transfered from KNIME to Python is in Apache Arrow
# often you might want to convert it to a Pandas dataframe (df)
df = knio.input_tables[0].to_pandas()

# do whatever you want in Python / Pandas

# data from python (pandas) to KNIME
knio.output_tables[0] = knio.Table.from_pandas(df)

Under the hood KNIME uses Arrow to transfer data between the Analytics Platform and Python. So you could also use that but most people will be familiar with Pandas or Numpy.

3 — More complex usage: Flow Variables

You can also work with KNIME Flow Variables and use them in Python script. And you can also create Flow Variables inside Python and use them in KNIME.

Python Script with KNIME Flow Variables (https://hub.knime.com/s/fLRIlwhYLlDj4skh)

Python Script with KNIME Flow Variables (https://hub.knime.com/s/fLRIlwhYLlDj4skh)

import knime.scripting.io as knio
import pandas as pd
from datetime import datetime

# Use a Flow Variable from KNIME inside Python Script
var_text_1 = knio.flow_variables["Text_Variable"]

# Create a simplified DataFrame with two rows and both date and 
# datetime columns. One value is from a Flow Variable "var_text_1"
data_simple = {
    "StringColumn": [var_text_1, "Example2"],
    "DoubleColumn": [5.5, 6.6],
    "IntegerColumn": [10, 20],
    "DateColumn": [datetime(2023, 5, 1).date(), 
                   datetime(2023, 6, 1).date()],
    "DatetimeColumn": [datetime(2023, 5, 1, 8, 30), 
                       datetime(2023, 6, 1, 15, 45)]
}

# Create the simplified DataFrame
df_simple = pd.DataFrame(data_simple)

# Bring the dataframe back to KNIME
knio.output_tables[0] = knio.Table.from_pandas(df_simple)

# Fill a KNIME Flow Variable from inside Python Script
knio.flow_variables["Variable_Out"] = 'Message from inside Python'

It is also useful to provide paths to the Python script so you can store data directly from inside Python.

More examples how to use the KNIME Python integration you can find in this Collection — including how to work with KNIME and Jupyter notebooks. But before you go to that you should explore the “getting started” space on the KNIME Community Hub:

[embed]Getting started with KNIME's Python integration - knime The KNIME Python Integration allows you to run Python scripts inside KNIME workflows seamlessly. This allows you to tap…hub.knime.com

4 — Complete Example with Machine Learning and Graphic Export

To explore further you can take a look at more examples on the KNIME Hub like this example for a classic Machine Learning case. Preparing the data, removing missing values and encoding strings and then storing the pipeline in a pickled object:

A classic ML Random Forest with a Data Preparation Pipeline for Missing Values etc. (https://hub.knime.com/s/fLRIlwhYLlDj4skh)

A classic ML Random Forest with a Data Preparation Pipeline for Missing Values etc. (https://hub.knime.com/s/fLRIlwhYLlDj4skh)

Also you can export graphics from the Python Script to KNIME or store them on the hard drive as SVG or PNG files (ort even PDFs).

Export Graphics from Python Script (https://hub.knime.com/s/fLRIlwhYLlDj4skh)

Export Graphics from Python Script (https://hub.knime.com/s/fLRIlwhYLlDj4skh)

You can do way more advanced stuff . Also with the help of the integrated KNIME AI Agent that can help you create code (as well as ChatGPT and others).

Graphics with Python

A great way to utilize Python and KNIME is to create advanced graphics with the most common libraries like Matplotlib or Seaborn (already on board with the Python extension):

[embed]Exploring the Power of Python Graphics with KNIME: A Collection of Examples TL;DR: Python graphics made easy with KNIME’s low-code approach. From scatter, violin and density plots to PNG files…medium.com

[embed]KNIME and Python Graphics — Another Collection of Examples In two recent articles I explored how to set up a Python graphic with the low-code tool KNIME and also how to build a…medium.com

Use Conda / Miniforge to install additional Python packages and Environments

In case you want to expand your world and install additional packages or specific Python versions, use conda package manager you can read this article and examples:

[embed]KNIME and Python — Setting up and managing Conda environments TL;DR: install python/conda thru Miniforge, find and use specific YAML-files to manage an environment, tell KNIME and…medium.com

If you want more little KNIME Snippets here is the collection:

[embed]KNIME Snippets — A Collection I started writing out some smaller articles about aspects of KNIME. This article is collecting them for convenience and…medium.com

If you enjoyed this article you can follow me on Medium (https://medium.com/@mlxl) and on the KNIME Forum (https://forum.knime.com/u/mlauber71/summary) and KNIME Community Hub (https://hub.knime.com/mlauber71).


메타데이터
post_id
aa4f3a55a768
slug
knime-snippets-5-python-overview-aa4f3a55a768
url
https://medium.com/low-code-for-advanced-data-science/knime-snippets-5-python-overview-aa4f3a55a768
canonical_url
https://medium.com/low-code-for-advanced-data-science/knime-snippets-5-python-overview-aa4f3a55a768
author_url
https://medium.com/@mlxl
status
ok
fetched_at
2026-07-22 08:44:43