【问题标题】:Dealing with 0 and -0 in Pandas and NumPy [duplicate]在 Pandas 和 NumPy 中处理 0 和 -0 [重复]
【发布时间】:2015-04-01 02:18:18
【问题描述】:

在 pandas 中进行元素乘法后,我最终在一个数据框元素中得到一个负零值,而在另一个数据框元素中得到一个常规零值。 Python 表明它们是相等的,但显示它们的方式不同。它们在哪个级别上不相等,这将如何影响以后的计算?

In[100]:
import numpy as np
import pandas as pd

In[101]:
df_a = pd.DataFrame([[-5.2,3.1,2.8],[1,2,3],[4,5,4]], columns=['Col0','Col1','Col2'], index=['Row0','Row1','Row2'])
df_b = pd.DataFrame([[0,0,2],[1,2,3],[4,5,4]], columns=['Col0','Col1','Col2'], index=['Row0','Row1','Row2'])
df_c = df_a * df_b

In[102]: print df_a
Out[102]:
      Col0  Col1  Col2
Row0  -5.2   3.1   2.8
Row1   1.0   2.0   3.0
Row2   4.0   5.0   4.0

In[103]: print df_b
Out[103]:
      Col0  Col1  Col2
Row0     0     0     2
Row1     1     2     3
Row2     4     5     4

In[104]: print df_c
Out[104]:
      Col0  Col1  Col2
Row0    -0     0   5.6
Row1     1     4   9.0
Row2    16    25  16.0

In[105]:
negative_zero = df_c.iloc[0,0]
positive_zero = df_c.iloc[0,1]
print(negative_zero == positive_zero)

Out[105]:
True

In[106]: print(type(negative_zero))
Out[106]: <type 'numpy.float64'>

In[107]: print(type(positive_zero))
Out[107]: <type 'numpy.float64'>

【问题讨论】:

    标签: python python-2.7 numpy pandas


    【解决方案1】:

    -0 等于 0。带符号的零不应影响任何进一步的计算。

    你可以在stackoverflow上查看WikipediaIEEE-standardvariousotherarticles关于这个主题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-09-07
      • 2016-09-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-31
      • 2013-10-09
      • 2020-07-29
      相关资源
      最近更新 更多