一位同事問我是否有任何示例可以顯示將數據表發送到 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();