A collegue asked me if we had any samples that showed sending a data table to Python, modifying the data within Python, and returning the modified table. I didn't find one in the scripting index, so I created one to share.
This script shows a full round trip. Passing a data table to Python, modifying the data in Python, then returning the results to the JSL script. Using everybody's favorite data table... Big Class. This script passes the table to Python, creates a weight divided by height column as 'lb_inch', and returns the modified table back to JSL.
When JMP passes a data table to Python, it creates a Pandas dataframe as the Python object.
Because my machine has multiple installed version of Python, I have to tell JMP where to find the installation of Python I want to uses. This is done with the Python Init statement.
Python Init ( Path ( ), Use Python Version ( ) )
Path() is the path to the python shared library
Use Python Version() is the version of python I want.
On Windows, escape the path separator so instead of c:\anaconda3\...\python36.dll, it would be c:\\anaconda3\\...\\python36.dll where ... is the rest of the file system path.
This Python Init() points to the Python installation on my Mac.
Names Default To Here( 1 );
Python Init(Path("/Library/Frameworks/Python.framework/Versions/3.6/lib/libpython3.6.dylib"),
Use Python Version("3.6")
);
dt1 = Open( "$SAMPLE_DATA/Big Class.jmp" );
Python Send( dt1 );
Close( dt1 );
Python Submit("print(dt1)");
Python Submit("\[
# The JMP Data table is transferred to Python as a pandas data frame
# Print out the column names
for col in dt1.columns:
print( col )
# Create a new column and apply formula to data, creating pounds per inch
# weight / height
def my_formula(w,h):
return w / h
dt1['lb_inch'] = dt1.apply(lambda row: my_formula(row['weight'],row['height']), axis=1)
dt1.head()
# print the modified table from Python
print(dt1)
]\");
// Get the modified table back from Python
dt2 = Python Get(dt1);
dt2 << New Data View;
Python Term();
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.