草庐IT

float_precision

全部标签

python - 在 pandas 0.10.1 上使用 pandas.read_csv 指定 dtype float32

我正在尝试使用pandasread_csv方法读取一个简单的空格分隔文件。但是,Pandas似乎没有遵守我的dtype论点。也许我指定的不正确?我已将我对read_csv的有点复杂的调用提炼为这个简单的测试用例。我实际上在我的“真实”场景中使用了converters参数,但为了简单起见,我删除了它。下面是我的ipythonsession:>>>cattest.outab0.763980.813940.321360.91063>>>importpandas>>>importnumpy>>>x=pandas.read_csv('test.out',dtype={'a':numpy.floa

python - 标签编码器 : TypeError: '>' not supported between instances of 'float' and 'str'

即使处理缺失值,我也面临多个变量的此错误。例如:le=preprocessing.LabelEncoder()categorical=list(df.select_dtypes(include=['object']).columns.values)forcatincategorical:print(cat)df[cat].fillna('UNK',inplace=True)df[cat]=le.fit_transform(df[cat])#print(le.classes_)#print(le.transform(le.classes_))-----------------------

python - 标签编码器 : TypeError: '>' not supported between instances of 'float' and 'str'

即使处理缺失值,我也面临多个变量的此错误。例如:le=preprocessing.LabelEncoder()categorical=list(df.select_dtypes(include=['object']).columns.values)forcatincategorical:print(cat)df[cat].fillna('UNK',inplace=True)df[cat]=le.fit_transform(df[cat])#print(le.classes_)#print(le.transform(le.classes_))-----------------------

python - 如何正确舍入半 float ?

我正面临round()函数的奇怪行为:foriinrange(1,15,2):n=i/2print(n,"=>",round(n))此代码打印:0.5=>01.5=>22.5=>23.5=>44.5=>45.5=>66.5=>6我希望浮点值总是向上取整,但相反,它被四舍五入到最接近的偶数。为什么会出现这种行为,获得正确结果的最佳方法是什么?我尝试使用fractions但结果是一样的。 最佳答案 NumericTypessection明确记录此行为:round(x[,n])xroundedtondigits,roundinghalft

python - 如何正确舍入半 float ?

我正面临round()函数的奇怪行为:foriinrange(1,15,2):n=i/2print(n,"=>",round(n))此代码打印:0.5=>01.5=>22.5=>23.5=>44.5=>45.5=>66.5=>6我希望浮点值总是向上取整,但相反,它被四舍五入到最接近的偶数。为什么会出现这种行为,获得正确结果的最佳方法是什么?我尝试使用fractions但结果是一样的。 最佳答案 NumericTypessection明确记录此行为:round(x[,n])xroundedtondigits,roundinghalft

python - pandas - 将 df.index 从 float64 更改为 unicode 或字符串

我想将数据帧的索引(行)从float64更改为字符串或unicode。我认为这可行,但显然不行:#checktypetype(df.index)'pandas.core.index.Float64Index'#changetypetounicodeifnotisinstance(df.index,unicode):df.index=df.index.astype(unicode)错误信息:TypeError:Settingdtypetoanythingotherthanfloat64orobjectisnotsupported 最佳答案

python - pandas - 将 df.index 从 float64 更改为 unicode 或字符串

我想将数据帧的索引(行)从float64更改为字符串或unicode。我认为这可行,但显然不行:#checktypetype(df.index)'pandas.core.index.Float64Index'#changetypetounicodeifnotisinstance(df.index,unicode):df.index=df.index.astype(unicode)错误信息:TypeError:Settingdtypetoanythingotherthanfloat64orobjectisnotsupported 最佳答案

python - 将 float 转换为美元和美分

首先,我尝试过这篇文章(以及其他):CurrencyformattinginPython.它对我的变量没有影响。我最好的猜测是因为我使用的是Python3,而那是Python2的代码。(除非我忽略了某些东西,因为我是Python新手)。我想将float(例如1234.5)转换为字符串,例如“$1,234.50”。我该怎么做呢?为了以防万一,这是我编译的代码,但不影响我的变量:money=float(1234.5)locale.setlocale(locale.LC_ALL,'')locale.currency(money,grouping=True)同样失败:money=float(1

python - 将 float 转换为美元和美分

首先,我尝试过这篇文章(以及其他):CurrencyformattinginPython.它对我的变量没有影响。我最好的猜测是因为我使用的是Python3,而那是Python2的代码。(除非我忽略了某些东西,因为我是Python新手)。我想将float(例如1234.5)转换为字符串,例如“$1,234.50”。我该怎么做呢?为了以防万一,这是我编译的代码,但不影响我的变量:money=float(1234.5)locale.setlocale(locale.LC_ALL,'')locale.currency(money,grouping=True)同样失败:money=float(1

python - Python中 float 的二进制表示(位不是十六进制)

如何将字符串作为32位float的二进制IEEE754表示?示例1.00->'00111111100000000000000000000000' 最佳答案 您可以使用struct包来做到这一点:importstructdefbinary(num):return''.join('{:0>8b}'.format(c)forcinstruct.pack('!f',num))将其打包为网络字节序float,然后将每个生成的字节转换为8位二进制表示形式并将它们连接起来:>>>binary(1)'001111111000000000000000