pandas read_csv column dtype is set to decimal but converts to string pandas read_csv column dtype is set to decimal but converts to string pandas pandas

pandas read_csv column dtype is set to decimal but converts to string


I think you need converters:

import pandas as pdimport ioimport decimal as Dtemp = u"""a,b,c,d           1,1,1,1.0"""# after testing replace io.StringIO(temp) to filenamedf = pd.read_csv(io.StringIO(temp),                  dtype={'a': int, 'b': float},                  converters={'c': D.Decimal, 'd': D.Decimal})print (df)       a    b  c    d    0  1  1.0  1  1.0for i, v in df.iterrows():    print(type(v.a), type(v.b), type(v.c), type(v.d))    <class 'int'> <class 'float'> <class 'decimal.Decimal'> <class 'decimal.Decimal'>