pd.read_feather问题,带小数/千位分隔符和浮点舍入问题

问题描述 投票:2回答:1

我想使用.ftr文件来快速分析数百个表。不幸的是,我在使用小数和千位分隔符时遇到了一些问题,类似于that post,只是read_feather不允许使用decimal=',', thousands='.'选项。我尝试了以下方法:

df['numberofx'] = (
    df['numberofx']
    .apply(lambda x: x.str.replace(".","", regex=True)
                      .str.replace(",",".", regex=True))

导致

AttributeError: 'str' object has no attribute 'str'

当我将其更改为]时>

df['numberofx'] = (
    df['numberofx']
    .apply(lambda x: x.replace(".","").replace(",","."))

我在结果中收到一些奇怪的(四舍五入)错误,例如对于某些大于1k的数字,则为22359999999999998而不是2236。所有低于1k的值都是实际结果的10倍,这可能是因为删除了“。”。浮点数并创建该数字的整数。

正在尝试

df['numberofx'] = df['numberofx'].str.replace('.', '', regex=True)

还会导致结果出现一些奇怪的现象,因为某些数字在10 ^ 12处,而其他数字则应保持在10 ^ 3。

Here is how I create my .ftr files from multiple Excel files。我知道我可以简单地从Excel文件创建DataFrames,但这会使我的日常计算速度变慢。

我该如何解决这个问题?


EDIT

:问题似乎出在与非美国标准有关的小数点和千位分隔符的excel文件中,以df格式读取,而不是保存为羽毛。使用pd.read_excel(f, encoding='utf-8', decimal=',', thousands='.')选项读取Excel文件解决了我的问题。这导致了下一个问题:

为什么将浮点数保存在羽毛文件中会导致奇怪的舍入错误,例如将2.235更改为2.2359999999999998?

我想使用.ftr文件来快速分析数百个表。不幸的是,我对小数和千位分隔符有一些问题,类似于该帖子,只是read_feather不允许...

python pandas rounding decimal-point feather
1个回答
0
投票

您的代码中的问题是:

© www.soinside.com 2019 - 2024. All rights reserved.