티스토리 뷰

반응형

Stack Overflow에 자주 검색, 등록되는 문제들과 제가 개발 중 찾아 본 문제들 중에서 나중에도 찾아 볼 것 같은 문제들을 정리하고 있습니다.

Stack Overflow에서 가장 먼저 확인하게 되는 가장 높은 점수를 받은 Solution과 현 시점에 도움이 될 수 있는 가장 최근에 업데이트(최소 점수 확보)된 Solution을 각각 정리하였습니다.

 

아래 word cloud를 통해 이번 포스팅의 주요 키워드를 미리 확인하세요.

Python Pandas Error tokenizing data

Python Pandas 데이터 토큰화 오류

 문제 내용 

I'm trying to use pandas to manipulate a .csv file but I get this error:

판다스를 사용하여 .csv 파일을 조작하려고 하는데 다음 오류가 표시됩니다:

 

pandas.parser.CParserError: Error tokenizing data. C error: Expected 2 fields in line 3, saw 12

 

I have tried to read the pandas docs, but found nothing.

저는 판다스 문서를 읽으려고 노력했지만 아무것도 발견하지 못했습니다.

 

My code is simple:

제 코드는 간단하다:
path = 'GOOG Key Ratios.csv'
#print(open(path).read())
data = pd.read_csv(path)

 

How can I resolve this? Should I use the csv module or another language ?

이 문제를 해결하려면 어떻게 해야 합니까? csv 모듈을 사용해야 합니까 아니면 다른 언어를 사용해야 합니까?

 

File is from Morningstar

파일은 모닝스타에서 왔습니다

 

 

 

 높은 점수를 받은 Solution 

you could also try;

시도해 볼 수도 있습니다;
data = pd.read_csv('file1.csv', on_bad_lines='skip')

 

Do note that this will cause the offending lines to be skipped.

이렇게 하면 위반 라인이 건너뜁니다.

 

Edit

편집

 

For Pandas < 1.3.0 try

판다의 경우 < 1.3.0 시도
data = pd.read_csv("file1.csv", error_bad_lines=False)

 

as per pandas API reference.

팬더 API 참조에 따라.

 

 

 

 가장 최근 달린 Solution 

I came across multiple solutions for this issue. Lot's of folks have given the best explanation for the answers also. But for the beginners I think below two methods will be enough :

저는 이 문제에 대한 여러 가지 해결책을 발견했습니다. 많은 사람들이 또한 답에 대해 가장 좋은 설명을 해주었습니다. 하지만 초보자들에게는 아래의 두 가지 방법으로 충분할 것이라고 생각합니다:

 

import pandas as pd

#Method 1

data = pd.read_csv('file1.csv', error_bad_lines=False)
#Note that this will cause the offending lines to be skipped.

#Method 2 using sep

data = pd.read_csv('file1.csv', sep='\t')

 

 

출처 : https://stackoverflow.com/questions/18039057/python-pandas-error-tokenizing-data

반응형
댓글
공지사항
최근에 올라온 글