进行 pandas 系列计算后不同的小数点

different decimal points after doing pandas series calculation

我正在尝试在 pandas 系列中进行计算。代码没有错误。

# create pandas series using dictionary
student1 = pd.Series({'science':100, 'english':80, 'math':90})
student2 = pd.Series({'math':80, 'science':90, 'english':80})

# calculate by score
addition = student1 + student2               
subtraction = student1 - student2            
multiplication = student1 * student2        
division = student1 / student2               

# series to dataframe
result = pd.DataFrame([addition, subtraction, multiplication, division], 
                      index=['addition', 'subtraction', 'multiplication', 'division'])
print(result)

结果如下。 我想知道为什么所有列的小数点都有不同的结果。

                english      math      science
addition          160.0   170.000   190.000000
subtraction         0.0    10.000    10.000000
multiplication   6400.0  7200.000  9000.000000
division            1.0     1.125     1.111111

而不是简单地写这个:-

division = student1 / student2

利用round()方法:-

division=round(student1 / student2,2)

这是因为你的部门:-

student1 / student2

以上代码给出:-

english    1.000000
math       1.125000
science    1.111111
dtype: float64

pandas精度的默认值为6