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.
Hello @quangdatitc
I’ve replied a post just yesterday, for a similar request.
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())
[image]
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
1 Like
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?
Hello @quangdatitc
Take a look into this post:
@toscano
If you adjust it inside the script it should work
[force]
Result
[res]
br
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
2 Likes
Hi @gonhaddock
Thanks for giving me advice.I also found another method through an article
1 Like
@quangdatitc please note some syntax has changed. Take a look at these examples:
@sirabelk3 I think you should make sure you have the latest version of the KNIME Python integration installed or a working Python installation of your own .
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.
2 Likes
Hi @mlauber71
thanks you so much!
1 Like
system
Closed
December 25, 2023, 9:52am
8
This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.