1
Pandas : populate column with if condition not working as expected의 텍스트 데이터를 데이터 프레임으로 읽으려고합니다.파이썬에서 팬더 데이터 프레임으로 텍스트 데이터를 읽을 수 없습니다.
dftxt = """
0 1 2
1 10/1/2016 'stringvalue' 456
2 NaN 'anothersting' NaN
3 NaN 'and another ' NaN
4 11/1/2016 'more strings' 943
5 NaN 'stringstring' NaN
"""
from io import StringIO
df = pd.read_csv(StringIO(dftxt), sep='\s+')
print (df)
그러나 나는 다음과 같은 오류가 점점 오전 : 내 코드는
Traceback (most recent call last):
File "mydf.py", line 16, in <module>
df = pd.read_csv(StringIO(dftxt), sep='\s+')
File "/usr/lib/python3/dist-packages/pandas/io/parsers.py", line 646, in parser_f
return _read(filepath_or_buffer, kwds)
File "/usr/lib/python3/dist-packages/pandas/io/parsers.py", line 401, in _read
data = parser.read()
File "/usr/lib/python3/dist-packages/pandas/io/parsers.py", line 939, in read
ret = self._engine.read(nrows)
File "/usr/lib/python3/dist-packages/pandas/io/parsers.py", line 1508, in read
data = self._reader.read(nrows)
File "pandas/parser.pyx", line 848, in pandas.parser.TextReader.read (pandas/parser.c:10415)
File "pandas/parser.pyx", line 870, in pandas.parser.TextReader._read_low_memory (pandas/parser.c:10691)
File "pandas/parser.pyx", line 924, in pandas.parser.TextReader._read_rows (pandas/parser.c:11437)
File "pandas/parser.pyx", line 911, in pandas.parser.TextReader._tokenize_rows (pandas/parser.c:11308)
File "pandas/parser.pyx", line 2024, in pandas.parser.raise_parser_error (pandas/parser.c:27037)
pandas.io.common.CParserError: Error tokenizing data. C error: Expected 4 fields in line 5, saw 6
내가 6 필드에 오류가 읽어되고있는 이해할 수 없다 : Expected 4 fields in line 5, saw 6
. 문제는 어디에 있으며 어떻게 해결 될 수 있습니까? 문제는 당신의 분리에있다
3 NaN 'and another ' NaN
1 2 3 4 5 6
-
아주 잘 작동합니다. 'engine = 'python'이 필요한 이유는 무엇입니까? 코드는이 옵션도없이 잘 작동하는 것 같습니다. – rnso
@rnso 수정하십시오. 기본적으로 '파이썬'엔진은 C 엔진이 지원하지 않으므로 가변 너비 구분 기호로 사용됩니다. 그것은 아직도 경고를 던진다. 암시 적보다 암시가 좋습니다! (Zen of Python을 기억하십시오.) –