问题描述
我正在使用 pandas (v0.18.1) 从名为test.csv"的文件中导入以下数据:
i am using pandas (v0.18.1) to import the following data from a file called 'test.csv':
a,b,c,d 1,1,1,1.0
我已将列 'c' 和 'd' 的 dtype 设置为 'decimal.decimal' 但它们返回为类型 'str'.
i have set the dtype to 'decimal.decimal' for columns 'c' and 'd' but instead they return as type 'str'.
import pandas as pd import decimal as d df = pd.read_csv('test.csv', dtype={'a': int, 'b': float, '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))
结果:
``
我还尝试在导入后显式转换为十进制,但没有成功(转换为浮点有效但不是十进制).
i have also tried converting to decimal explicitly after import with no luck (converting to float works but not decimal).
df.c = df.c.astype(float) df.d = df.d.astype(d.decimal) for i, v in df.iterrows(): print(type(v.a), type(v.b), type(v.c), type(v.d))
结果:
``
以下代码将str"转换为decimal.decimal",所以我不明白为什么 pandas 的行为方式不同.
the following code converts a 'str' to 'decimal.decimal' so i don't understand why pandas doesn't behave the same way.
x = d.decimal('1.0') print(type(x))
结果:
``
推荐答案
我觉得你需要转换器:
import pandas as pd import io import decimal as d temp = u"""a,b,c,d 1,1,1,1.0""" # after testing replace io.stringio(temp) to filename df = 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.0 for i, v in df.iterrows(): print(type(v.a), type(v.b), type(v.c), type(v.d))