Pandas Series.apply(dtype)
검사 결과에서 '\n'을 카운트해서 for문을 돌려가면서 metastasis 판독 여부를 확인하려고 하였다.
dataframe에서 특정 칼럼의 값을 가져와서 변수에 할당하고 type을 보니 numpy array였다.
문자열로 변환하기 위해 np.array2string()을 해 주었고, 변수 타입은 분명히 string으로 바뀌었는데, 여전히 count()는 실행되지 않았고, 텍스트를 출력했을때 list형태를 보여주었다.
code |
result = subset.loc[:,['검사결과']]
text = result.values[0]
print(type(text))
print(text)
text_str = np.array2string(text)
print(type(text_str))
print(text_str)
print('count number -----> ', text_str.count('\n'))
|
RESULT | ![]() |
아래의 예문은 문자열이 series로 출력되는 경우를 보여준다. 물론 count()도 먹힌다.
CODE |
text_str = '임상진단 : rectal cancer\n받은 조직은 포르말린에 고정된 매우 작은 생검 조직으로 총 4개이다. 전부 포매함.\nMICRO (1 HE)\nDIAGNOSIS: \nRectum, endoscopic biopsy:\n ADENOCARCINOMA, moderately differentiated'
print(text_str)
print(type(text_str))
print('count number -----> ', text_str.count('\n'))
|
RESULT | ![]() |
이 문제를 해결하기 위해 array를 series형태로 변환시켜주는 Pandas Series.apply(dtype)를 이용하여 다시 변환하였고, 결과는 아래와 같이 성공하였다.
CODE |
subset["검사결과"] = subset["검사결과"].apply(str)
print(subset, "\n")
print(subset.dtypes)
text = subset["검사결과"][0] # 요기 row index를 for문으로 돌려 줄 예정이다.
print(text)
print('count number -----> ', text.count('\n'))
|
RESULT | ![]() |
'Programming Language > Python' 카테고리의 다른 글
[Python]ImportError: DLL load failed while importing _arpack: 지정된 프로시저를 찾을 수 없습니다. (0) | 2022.08.06 |
---|---|
[Python]파이썬 프로그램 실행중 강제종료 (0) | 2022.06.21 |
[Python]escape code (0) | 2022.06.16 |
[Python]excel file 불러와서 특정 컬럼 추출 후 정렬, 인덱스 리셋 (0) | 2022.06.16 |