huwenchao
2/21/2017 - 3:06 PM

convert value to demical when read_csv in pandas

convert value to demical when read_csv in pandas

# -*- coding: utf-8 -*-
import pandas as pd
import io
import decimal as D

temp = u"""a,b,c,d
0.1,0.2,0.1,0.2"""
df = pd.read_csv(
    io.StringIO(temp), converters={'c': D.Decimal,
                                   'd': D.Decimal})

for i, v in df.iterrows():
    print(type(v.a), type(v.b), type(v.c), type(v.d))