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」、「」
「a」、「b」、「e」
「a」、「b」、「」
自動化とスクリプトRe: 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
Re: 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 を入れ、文字列に空の文字列 "" を入れることで、正しい処理が行われます。
Re: JMP18: python: pandas から dataTable への変換で欠損値のある列がスキップされる簡単に答えると、is_string_dtype( col3 ) は False を返します。これは文字列の列ではなく、オブジェクトの列であり、一部は文字列で一部は NaN です。上記のコードは、文字列ではないものをすべて数値として扱います。NaN は欠損値として適切に扱われますが、文字値 'c' と 'e' は数値列では無効であり、欠損値になります。すべての値をテストせずに、pandas 列を文字として読み取るためのより良い回答を探しています。