CSVDocumentCleaner
CSVDocumentCleaner
CSVDocumentCleaner는 CSV 형식의 내용을 텍스트로 담은 Document 객체 리스트를 입력받아, 완전히 비어 있는 행과 열을 제거해 데이터를 정리해요. 정리 전에 보존할 행과 열 수를 지정할 수도 있습니다.
출처: 문서
본문
Overview
CSVDocumentCleaner는 CSV 형식의 내용을 텍스트로 담은 Document 객체 리스트를 입력받아, 완전히 비어 있는 행과 열을 제거해 데이터를 정리해요. 정리 전에 보존할 행과 열 수를 사용자가 지정할 수 있습니다.
Parameters
ignore_rows— 처리를 시작하기 전에 CSV 테이블 위쪽에서 무시할 행 수. 열이 제거되면 같은 열이 무시된 행에서도 제거돼요.ignore_columns— 처리를 시작하기 전에 CSV 테이블 왼쪽에서 무시할 열 수. 행이 제거되면 같은 행이 무시된 열에서도 제거돼요.remove_empty_rows— 완전히 빈 행을 제거할지 여부.remove_empty_columns— 완전히 빈 열을 제거할지 여부.keep_id— 출력 문서에 원본 문서 ID를 유지할지 여부.
Cleaning Process
CSVDocumentCleaner 알고리즘은 다음 단계를 따릅니다:
- pandas를 사용해 각 문서의 내용을 CSV 테이블로 읽습니다.
- 위쪽에서 지정한
ignore_rows수와 왼쪽에서ignore_columns수를 유지합니다. - 완전히 비어 있는(오직 NaN 값만 담긴) 행과 열을 제거합니다.
- 열이 제거되면 무시된 행에서도 함께 제거합니다.
- 행이 제거되면 무시된 열에서도 함께 제거합니다.
- 남은 무시된 행과 열을 다시 붙여 원래 위치를 유지합니다.
- 정리된 CSV 내용을 새
Document객체로 반환합니다.
Usage
On its own
CSVDocumentCleaner를 독립적으로 사용해 CSV 문서를 정리할 수 있어요:
from haystack import Document
from haystack.components.preprocessors import CSVDocumentCleaner
cleaner = CSVDocumentCleaner(ignore_rows=1, ignore_columns=0)
documents = [Document(content="""col1,col2,col3\n,,\na,b,c\n,,""")]
cleaned_docs = cleaner.run(documents=documents)
In a pipeline
from pathlib import Path
from haystack import Pipeline
from haystack.document_stores.in_memory import InMemoryDocumentStore
from haystack.components.converters import XLSXToDocument
from haystack.components.preprocessors import CSVDocumentCleaner
from haystack.components.writers import DocumentWriter
document_store = InMemoryDocumentStore()
p = Pipeline()
p.add_component(instance=XLSXToDocument(), name="xlsx_file_converter")
p.add_component(
instance=CSVDocumentCleaner(ignore_rows=1, ignore_columns=1),
name="csv_cleaner",
)
p.add_component(instance=DocumentWriter(document_store=document_store), name="writer")
p.connect("xlsx_file_converter.documents", "csv_cleaner.documents")
p.connect("csv_cleaner.documents", "writer.documents")
p.run({"xlsx_file_converter": {"sources": [Path("your_xlsx_file.xlsx")]}})
이렇게 하면 CSV 문서가 추가 처리나 저장 전에 제대로 정리됩니다.
더 알아보기 (Learn more)
- CSVDocumentCleaner — Haystack 공식 문서