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 文件內容
“列1”,“列2”,“列3”
“a”、“b”、“c”
“一”、“二”、“”
“a”、“b”、“e”
“一”、“二”、“”
自動化和腳本編寫回覆:JMP18:python:從 pandas 到 dataTable 的轉換會跳過缺少值的列使用上面的 p2j( ) 和更新後的 export.csv,其中一列缺少值作為第一個值的數值資料給出:

% 更多 ~/Downloads/export.csv
“列1”,“列2”,“列3”,“列4”
「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 程式碼將接受數字列或字元列中的“無”,並執行“正確”的操作。透過將 NaN 放入數字列中並將「」空字串放入字元列中。
回覆:JMP18:python:從 pandas 到 dataTable 的轉換會跳過缺少值的列簡短的答案是is_string_dtype( col3 ) 回傳 False。它不是字串列,而是物件列,有些是字串,有些是 NaN。上面的程式碼將所有非字串的內容都視為數字。 NaN 正確輸入為缺失值,但字元值「c」和「e」對於數字列無效並成為缺失值。我正在尋找將 pandas 列作為字元讀取的更好答案,而不測試每個值。