2017-02-17 1 views
0

Ich benutze pandas read_csv, um eine einfache csv-Datei zu lesen. Allerdings hat es ValueError: could not convert string to float: was ich nicht verstehe warum.python pandas read_csv Tausendertrennzeichen funktioniert nicht

Der Code ist einfach

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

Aber ich bekomme diese Fehlermeldung

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'

Wie kann es möglich, Fehler zu erhalten, wenn eine Reihe von '79, 026,695.50' Umwandlung zu schweben? Ich habe bereits die beiden Optionen

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

angegeben Ist es ein Problem, unser Code oder ein Fehler in Pandas?

+0

können Sie den Inhalt der Datei hinzufügen, in Frage zu stellen? Oder besser Datei auf gdocs hochladen, Dropbox .. wenn Daten nicht vertraulich? – jezrael

+0

Können Sie eine Kopie der betreffenden Zeile bereitstellen? – IanS

Antwort

1

Es scheint, gibt es Probleme mit quoting, denn wenn Separator , und thousands, auch ist, einige zitiert hat in csv sein:

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 
Verwandte Themen