当我在 Pandas 中尝试 locale.atof 时,float' 对象没有属性 'replace' 是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32237106/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 23:49:00  来源:igfitidea点击:

What does float' object has no attribute 'replace' when I try locale.atof in Pandas?

pythonpandas

提问by Dervin Thunk

I'm trying to convert a Pandas dataframe series to float. I do locale.setlocale(locale.LC_NUMERIC, '')and then df.idh.apply(locale.atof), but it gives me the above mentioned error: AttributeError: 'float' object has no attribute 'replace'. I assume at some point it's getting something like a NaN, maybe or some other string and it does not recognize it. How do I tell applyto skip those?

我正在尝试将 Pandas 数据帧系列转换为浮动。我做了locale.setlocale(locale.LC_NUMERIC, '')然后df.idh.apply(locale.atof),但它给了我上面提到的错误:AttributeError: 'float' object has no attribute 'replace'。我假设在某个时候它会得到类似 NaN 的东西,也许或者其他一些字符串,但它无法识别它。我怎么告诉apply跳过那些?

回答by Dervin Thunk

Well, I don't know how "smart" this is, but I "fixed" it like this, at least for the time being:

好吧,我不知道这有多“聪明”,但我像这样“修复”了它,至少目前是这样:

df.idh = df.idh.astype(str).apply(locale.atof)

Please, do let me know the smart answer to this.

请让我知道这个聪明的答案。

回答by ansonw

The problem has to do with null values. Replace cannot work on numpy nulls. Your solution doesn't return an error because you convert np.NaN (nulls) into 'nan' and replace can work on the string 'nan'. The problem is though that you now have 'nan' in your column instead of np.NaN. So if you run:

问题与空值有关。替换无法处理 numpy 空值。您的解决方案不会返回错误,因为您将 np.NaN (nulls) 转换为 'nan' 并且 replace 可以处理字符串 'nan'。问题是,您现在的列中有 'nan' 而不是 np.NaN。所以如果你运行:

df[df.idh.isnull()]

It would return 0 rows even though you do have nulls in your data. The following code keeps the np.NaN while running a replace statement on that column.

即使您的数据中有空值,它也会返回 0 行。以下代码在对该列运行替换语句时保留 np.NaN。

def replace_percent(x):
    try: 
        return x.replace('%', '')
    except AttributeError:
        return np.NaN

df_not.secularism = df_not.secularism.map(replace_percent)

回答by tonante27

Could this alternative solution work for you?

这种替代解决方案对您有用吗?

csv_nn = df.replace(np.nan, 'null', regex=True)