import pandas as pd
money=pd.Series([100,20,None])
money-15
Out[4]:
0 85.0
1 5.0
2 NaN
dtype: float64
# do substraction with - operator, missing values are ignored
# the .sub method allows you to specify a 'fill_value' parameter to use in place of missing values
money.sub(15,fill_value=0)
Out[7]:
0 85.0
1 5.0
2 -15.0
dtype: float64
# some explanation:
# Python interprets the score*2.5 expression as score._mul_(2.5)
#._mul_() is a special method that always begin and end with two underscores
# so, they also are called `dunder` methods as the method that implements
# the operator is surrounded by double underscores (dunder, 'double underscores')
值得注意的是,这里仅限于数值型Series的运算。
如果是字符串就不能这么简单地比较了。