cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Browse apps to extend the software in the new JMP Marketplace
Choose Language Hide Translation Bar
minion
Level II

JMP18: Python: jmp.Datatable: HowTo get column names without converting to pandas dataframe

Trying to create a new column in a jmp.Datatable and check if it already exists. 

Need to get list of column names

1 ACCEPTED SOLUTION

Accepted Solutions

Re: JMP18: Python: jmp.Datatable: HowTo get column names without converting to pandas dataframe

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

6 REPLIES 6
frank_wang
Level IV

Re: HowTo get column names of jmp.Datatable without converting to pandas dataframe

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 );

 

心若止水
txnelson
Super User

Re: HowTo get column names of jmp.Datatable without converting to pandas dataframe

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" )
);
Jim
minion
Level II

Re: HowTo get column names of jmp.Datatable without converting to pandas dataframe

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

jthi
Super User

Re: HowTo get column names of jmp.Datatable without converting to pandas dataframe

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)

jthi_0-1717044361108.png

 

-Jarmo

Re: JMP18: Python: jmp.Datatable: HowTo get column names without converting to pandas dataframe

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']

Re: JMP18: Python: jmp.Datatable: HowTo get column names without converting to pandas dataframe

From which it is a simple check to see if the name is in the list.

if 'age' in col_names:
    print('found age')