Trying to create a new column in a jmp.Datatable and check if it already exists.
Need to get list of column names
Go to Solution
The data table is an iterable collection of columns.
import jmp dt = jmp.open(jmp.SAMPLE_DATA + 'Big Class.jmp') col_names = [ x.name for x in dt] print(col_names)
Resulting in
['name', 'age', 'sex', 'height', 'weight']
View solution in original post
Hi
One sample from JSL index
Names Default To Here( 1 ); dt = Open( "$SAMPLE_DATA/Big Class.jmp" ); n = dt << Get Column Names(); Show( n ); CNames = dt << Get Column Names( Continuous ); Show( CNames ); SNames = dt << Get Column Names( String ); Show( SNames );
Here is the method I normally use to determine if a specific column is or is not in the data table
Names Default To Here( 1 ); dt = // Open Data Table: Big Class.jmp // → Data Table( "Big Class" ) Open( "$SAMPLE_DATA/Big Class.jmp" ); If( Try( :mycolumn << get name, "" ) == "", New Column( "mycolumn" ) );
Thanks for the inputs.
My bad for not mentioning that was trying to do it in the python environment
Sample code below
import jmp import jmputils dt = jmp.current() # get current datatable dt.new_column('colA', jmp.DataType.Character)
Want to check if colA exists before creating a new one
You can always fall back to jsl using jmp.run_jsl
CNames = jmp.run_jsl(''' Data Table( "Powered by Python" ) << Get Column Names( Continuous, String ); ''')
Or you could maybe loop over the columns check them with col_obj.name
import jmp dt = jmp.open(jmp.SAMPLE_DATA + "Big Class.jmp") print(dt[0].name)
Comparing column names might not be that easy in JMP and you might want to use something like << Has Column with jmp.run_jsl (data table : message column exists(colname)? - JMP User Community)
From which it is a simple check to see if the name is in the list.
if 'age' in col_names: print('found age')