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",""
"a", "b", "e"
"a", "b",""
자동화 및 스크립팅Re: JMP18: python: pandas에서 dataTable로 변환하면 누락된 값이 있는 열을 건너뜁니다.위의 p2j( ) 및 업데이트된 내보내기.csv를 첫 번째 값으로 누락된 값이 있는 숫자 데이터 열을 사용하면 다음이 제공됩니다.

% 이상 ~/Downloads/export.csv
"col1","col2","col3","col4"
"알파벳",
"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'는 숫자 열에 유효하지 않으며 누락된 값이 됩니다. 모든 값을 테스트 하지 않고 팬더 열을 문자로 읽는 것에 대한 더 나은 답변을 찾고 있습니다 .