# how to use python script?

**URL:** <https://forum.knime.com/t/how-to-use-python-script/72948>\
**Category:** KNIME Extensions\
**Tags:** scripting, python\
**Created:** [September 26, 2023, 5:29am UTC](https://forum.knime.com/t/how-to-use-python-script/72948 "2023-09-26T05:29:25Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![quangdatitc](https://forum-cdn.knime.com/letter_avatar_proxy/v4/letter/q/13edae/32.png) [@quangdatitc](https://forum.knime.com/u/quangdatitc)\
**Post date:** [September 26, 2023, 5:29am UTC](https://forum.knime.com/t/how-to-use-python-script/72948/1 "2023-09-26T05:29:25Z")

</div>

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

Hello everyone!

I have a question related to a Python script node. I want to use this node to divide a list of Excel files with an equal number of rows into separate groups. I have already found a solution in Jupyter Notebook, but it’s quite challenging to implement it in KNIME. I’ve installed Miniconda and set up the basic parameters, but I’m unsure how to proceed with the output. Can anyone help me with this? I really appreciate it. Thank you very much.

---

<div class="post-metadata">

**Author:** ![gonhaddock](https://forum-cdn.knime.com/user_avatar/forum.knime.com/gonhaddock/32/35425_2.png) [@gonhaddock](https://forum.knime.com/u/gonhaddock)\
**Post date:** [September 26, 2023, 7:51am UTC](https://forum.knime.com/t/how-to-use-python-script/72948/2 "2023-09-26T07:51:22Z")

</div>

Hello @quangdatitc  
I’ve replied a post just yesterday, for a similar request.

> [@Working with data using python](https://forum.knime.com/t/working-with-data-using-python/72932/2):
>
> Hello @Alex_025 You can learn in the node by looking at the examples in ‘Templates’ tab. Besides that, take a look in the following code as starting point: import knime.scripting.io as knio import pandas as pd # data in df = knio.input\_tables[0].to\_pandas() # things happen print(df) # output as pd.DataFrame knio.output\_tables[0] = knio.Table.from\_pandas(df.to\_frame()) BR

The basics are about how to control input data and output data. You can play with multiple outputs by adding output ports as in the image.

You can make work your Jupyter Notebook code, by fitting it into the ‘# Things happen’ section of code.

I hope this helps to answer your question.

BR

---

<div class="post-metadata">

**Author:** ![quangdatitc](https://forum-cdn.knime.com/letter_avatar_proxy/v4/letter/q/13edae/32.png) [@quangdatitc](https://forum.knime.com/u/quangdatitc)\
**Post date:** [September 26, 2023, 8:24am UTC](https://forum.knime.com/t/how-to-use-python-script/72948/3 "2023-09-26T08:24:18Z")

</div>

Thank you for taking the time to guide me!  
but my output like list() ex:df=[[1,3,4],[2,61,1]]  
so what should i do?

---

<div class="post-metadata">

**Author:** ![gonhaddock](https://forum-cdn.knime.com/user_avatar/forum.knime.com/gonhaddock/32/35425_2.png) [@gonhaddock](https://forum.knime.com/u/gonhaddock)\
**Post date:** [September 26, 2023, 8:32am UTC](https://forum.knime.com/t/how-to-use-python-script/72948/4 "2023-09-26T08:32:41Z")

</div>

Hello @quangdatitc  
Take a look into this post:

> [@Unknown data type when converting pandas to KNIME](https://forum.knime.com/t/unknown-data-type-when-converting-pandas-to-knime/70333/7):
>
> @toscano If you adjust it inside the script it should work Result [res] br

```auto
import knime.scripting.io as knio
import pandas as pd
knio.output_tables[0] = knio.Table.from_pandas(
    pd.DataFrame(
        [[1,3,4],[2,61,1]], columns=['column01', 'column02', 'column03']
    )
)

```

You can also remove input port, and use Python Script node as a source; importing or creating your own data in the code.

BR

---

<div class="post-metadata">

**Author:** ![quangdatitc](https://forum-cdn.knime.com/letter_avatar_proxy/v4/letter/q/13edae/32.png) [@quangdatitc](https://forum.knime.com/u/quangdatitc)\
**Post date:** [September 26, 2023, 9:36am UTC](https://forum.knime.com/t/how-to-use-python-script/72948/5 "2023-09-26T09:36:26Z")

</div>

Hi @gonhaddock  
Thanks for giving me advice.I also found another method through an article

> [@Python Script Table Output](https://forum.knime.com/t/python-script-table-output/59365/2):
>
> @mcrisnidh welcome to the knime forum. You will have to convert your list to a [Pandas dataframe](https://www.geeksforgeeks.org/create-a-pandas-dataframe-from-lists/). [https://docs.knime.com/latest/python\_installation\_guide/#\_configuration](https://docs.knime.com/latest/python_installation_guide/#_configuration) Something like this df = pd.DataFrame(list) knio.output\_tables[0] = knio.Table.from\_pandas(df) Or df = pd.DataFrame(list) knio.output\_tables[0] = knio.write\_table(df)

---

<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:** [September 26, 2023, 9:37am UTC](https://forum.knime.com/t/how-to-use-python-script/72948/6 "2023-09-26T09:37:52Z")

</div>

@quangdatitc please note some syntax has changed. Take a look at these examples:

> [@AttributeError NoneType object has no attribute 'to\_arrow\_table'](https://forum.knime.com/t/attributeerror-nonetype-object-has-no-attribute-to-arrow-table/64640/2):
>
> @sirabelk3 I think you should make sure you have the latest version of the [KNIME Python integration](https://www.knime.com/blog/how-to-setup-the-python-extension) installed or a [working Python installation of your own](https://medium.com/p/2ac217792539). Also you should check what type of data your code would produce. If you want to bring data back from Python to KNIME you would have to have an Arrow data table. Often you would have a Pandas data frame that you would then convert to Arrow. Maybe check out these examples.

---

<div class="post-metadata">

**Author:** ![quangdatitc](https://forum-cdn.knime.com/letter_avatar_proxy/v4/letter/q/13edae/32.png) [@quangdatitc](https://forum.knime.com/u/quangdatitc)\
**Post date:** [September 26, 2023, 9:51am UTC](https://forum.knime.com/t/how-to-use-python-script/72948/7 "2023-09-26T09:51:43Z")

</div>

Hi @mlauber71  
thanks you so much!

---

<div class="post-metadata">

**Author:** ![system](https://forum-cdn.knime.com/uploads/default/original/1X/ab3ccf34482a0329361734a18199390177204f15.png) [@system](https://forum.knime.com/u/system)\
**Post date:** [December 25, 2023, 9:52am UTC](https://forum.knime.com/t/how-to-use-python-script/72948/8 "2023-12-25T09:52:05Z")

</div>

Closed on December 25, 2023, 9:52am UTC

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
