JMP18:python:从 pandas 到 dataTable 的转换会跳过缺少值的列import jmp, numpy as np, pandas as pd
import jmputils
def p2j(df):
dt2 = jmp.DataTable('dt2', df.shape[0])
# get the column names from the data frame
names = list(df.columns)
# loop across the columns of the data frame
for j in range( df.shape[1]):
# check if the coulumn data type is string or numeric
if is_string_dtype(df[names[j] ]):
dt2.new_column(names[j], jmp.DataType.Character)
else:
dt2.new_column(names[j], jmp.DataType.Numeric)
# populate the JMP column with data
dt2[j] = list(df.iloc[:,j])
return(dt2)
fname = r"export.csv";
df = pd.read_csv(fname)
filt_csv = p2j(df);使用上述代码读取 csv 文件。p2j 函数似乎无法正确导入 col3,因为它缺少值

csv 文件内容
“col1”,“col2”,“col3”
“a”,“b”,“c”
“a”,“b”,“”
“a”,“b”,“e”
“a”,“b”,“”
自动化和脚本回复:JMP18:python:从 pandas 到 dataTable 的转换会跳过缺少值的列使用上述 p2j() 和更新的 export.csv,其中一列数值数据具有缺失值作为第一个值,结果如下:

% 更多 ~/Downloads/export.csv
“col1”,“col2”,“col3”,“col4”
“a”,“b”,“c”,
“a”,“b”,“”,2.718
“a”,“b”,“e”,1.618
“a”,“b”,“”,3.14159
回复:JMP18:python:从 pandas 到 dataTable 的转换会跳过缺少值的列此处显示更新的代码,如果列类型不是数字或字符串,则检查直到我们获得非缺失值,然后再确定列类型是字符还是数字。
import jmp
import numpy as np, pandas as pd
from pandas.api.types import is_string_dtype
from pandas.api.types import is_numeric_dtype
def p2j(df):
dt2 = jmp.DataTable('dt2', df.shape[0])
# get the column names from the data frame
names = list(df.columns)
# loop across the columns of the data frame
for j in range( df.shape[1]):
# check if the coulumn data type is string or numeric
if is_numeric_dtype( df[ names[j] ] ):
dt2.new_column(names[j], jmp.DataType.Numeric)
dt2[j] = list(df.iloc[:,j]) # populate the JMP column with data
elif is_string_dtype( df[ names[j]] ):
dt2.new_column(names[j], jmp.DataType.Character)
dt2[j] = list(df.iloc[:,j]) # populate the JMP column with data
else:
col = list()
dtype = None
for i in range(dt2.nrows):
cell_value = df.iloc[i,j]
# delay setting column type until we see first non-null value
if pd.isna( cell_value ):
col.append(None)
else:
if not dtype:
if is_numeric_dtype( cell_value ):
dtype = jmp.DataType.Numeric
else:
# assume it's character - could be datetime64,...
dtype = jmp.DataType.Character
col.append( cell_value )
print(col)
if dtype:
dt2.new_column(names[j], dtype)
dt2[j] = col # populate the JMP column with data
return(dt2)
fname = r"export.csv";
df = pd.read_csv(jmp.HOME + 'Downloads/' + fname)
print(df)
print(df.dtypes)
print( f'col1 is a string: {is_string_dtype(df[names[0]])}' )
print( f'col2 is a string: {is_string_dtype(df[names[1]])}' )
print( f'col3 is a string: {is_string_dtype(df[names[2]])}' )
filt_csv = p2j(df);
得出以下结果:
/*:
col1 col2 col3
0 a b c
1 a b NaN
2 a b e
3 a b NaN
col1 object
col2 object
col3 object
dtype: object
col1 is a string: True
col2 is a string: True
col3 is a string: False
['c', None, 'e', None]
请注意,在我的函数中,我将数字 NaN 转换为 None。JMP DataTable 代码将接受数字列或字符列中的 None,并执行“正确”的操作。通过将 NaN 放在数字列中,将“”空字符串放在字符列中。
回复:JMP18:python:从 pandas 到 dataTable 的转换会跳过缺少值的列简短的回答是is_string_dtype( col3 ) 返回 False。它不是字符串列,而是对象列,有些是字符串,有些是 NaN。上面的代码将所有非字符串视为数字。NaN 正确地作为缺失值输入,但字符值“c”和“e”对于数字列无效并成为缺失值。我正在寻找一个更好的答案,将 pandas 列读取为字符,而无需测试每个值。