2017-02-17 7 views
0

간단한 csv 파일을 읽으려면 pandas read_csv를 사용합니다. 그러나, 나는 이유를 이해하지 못하는 ValueError: could not convert string to float:을 가지고있다.python pandas read_csv 천 단위 구분 기호가 작동하지 않습니다.

코드는 단순히

rawdata = pd.read_csv(r'Journal_input.csv' , 
         dtype = { 'Base Amount' : 'float64' } , 
         thousands = ',' , 
         decimal = '.', 
         encoding = 'ISO-8859-1') 

이다 그러나 나는이 오류를 얻을

pandas\parser.pyx in pandas.parser.TextReader.read (pandas\parser.c:10415)()

pandas\parser.pyx in pandas.parser.TextReader._read_low_memory (pandas\parser.c:10691)()

pandas\parser.pyx in pandas.parser.TextReader._read_rows (pandas\parser.c:11728)()

pandas\parser.pyx in pandas.parser.TextReader._convert_column_data (pandas\parser.c:13162)()

pandas\parser.pyx in pandas.parser.TextReader._convert_tokens (pandas\parser.c:14487)()

ValueError: could not convert string to float: '79,026,695.50'

, 026,695.50 '부유하는 '79의 문자열을 변환 할 때 어떻게이 가능한 오류를 얻을 수 있습니까? 이미 두 가지 옵션을 지정했습니다

thousands = ',' , 
decimal = '.', 

팬더의 코드 또는 버그가 문제입니까?

+0

당신은 질문 파일의 내용을 추가 할 수 있습니까? 또는 파일을 gdocs, dropbox에 업로드하는 것이 더 좋습니다. 데이터가 신중하지 않은 경우? – jezrael

+0

문제의 행 사본을 제공 할 수 있습니까? – IanS

답변

1

그것은 일부는 csv에 있어야한다 인용, 구분 ,thousands이 너무 , 인 경우 때문에 문제가 quoting으로이 보인다

import pandas as pd 
from pandas.compat import StringIO 
import csv 

temp=u"""'a','Base Amount' 
'11','79,026,695.50'""" 
#after testing replace 'StringIO(temp)' to 'filename.csv' 
df = pd.read_csv(StringIO(temp), 
       dtype = { 'Base Amount' : 'float64' }, 
       thousands = ',' , 
       quotechar = "'", 
       quoting = csv.QUOTE_ALL, 
       decimal = '.', 
       encoding = 'ISO-8859-1') 

print (df) 
    a Base Amount 
0 11 79026695.5 

temp=u'''"a","Base Amount" 
"11","79,026,695.50"''' 
#after testing replace 'StringIO(temp)' to 'filename.csv' 
df = pd.read_csv(StringIO(temp), 
       dtype = { 'Base Amount' : 'float64' }, 
       thousands = ',' , 
       quotechar = '"', 
       quoting = csv.QUOTE_ALL, 
       decimal = '.', 
       encoding = 'ISO-8859-1') 

print (df) 
    a Base Amount 
0 11 79026695.5 
관련 문제