一位同事问我是否有任何示例可以显示将数据表发送到 Python、在 Python 中修改数据并返回修改后的表。 我在脚本索引中没有找到,所以我创建了一个来分享。
该脚本显示了完整的往返行程。 将数据表传递给Python,在Python中修改数据,然后将结果返回给JSL脚本。 使用每个人最喜欢的数据表... 大班. 该脚本将表传递给 Python,创建一个体重除以高度列作为“lb_inch”,并将修改后的表返回给 JSL。
当 JMP 将数据表传递给 Python 时,它会创建一个 Pandas 数据框作为 Python 对象。
因为我的机器安装了多个 Python 版本,所以我必须告诉 JMP 在哪里可以找到我想要使用的 Python 安装。 这是通过 Python Init 语句完成的。
Python Init(路径(),使用Python版本())
Path()是python共享库的路径
Use Python Version() 是我想要的 python 版本。
在 Windows 上,转义路径分隔符,因此不是 c:\anaconda3\...\python36.dll,而是 c:\\anaconda3\\...\\python36.dll,其中 ... 是其余部分文件系统路径。
这个 Python Init() 指向我的 Mac 上的 Python 安装。
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()<BR />
# print the modified table from Python
print(dt1)
]\");
<BR />// Get the modified table back from Python
dt2 = Python Get(dt1);
dt2 << New Data View;
Python Term();