pandas 还有一个重要的功能,就是他可以对不同索引的对象进行算数运算。
对象相加, 如果存在不同的索引对,则结果的索引就是该索引对的并集。先来个例子
Series
In [33]: s1 = Series([7.3, -2.5, 3.4, 1.5], index=['a', 'c', 'd', 'e'])In [34]: s2 = Series([-2.1, 3.6, -1.5, 4, 3.1], index=['a', 'c', 'e', 'f', 'g'])In [35]: s1Out[35]:a 7.3c -2.5d 3.4e 1.5dtype: float64In [36]: s2Out[36]:a -2.1c 3.6e -1.5f 4.0g 3.1dtype: float64In [37]: s1 + s2Out[37]:a 5.2c 1.1d NaNe 0.0f NaNg NaNdtype: float64
In [38]: s3 = Series([-2.1, 3.6, -1.5, 4, 3.1], index=['a', 'c', 'e', 'f', 'g'])In [39]: s1 + s2 + s3Out[39]:a 3.1c 4.7d NaNe -1.5f NaNg NaNdtype: float64也就是说NaN值不会变
DataFrame
add 用于加法(+)方法sub 用于减法(-)方法div 用于除法(/)方法mul 用于乘法(*)方法
In [45]: df1 = DataFrame(np.arange(9.).reshape((3,3)), columns=list('bcd'), index=['Ohio', "Texas", "Colorado"])In [46]: df2 = DataFrame(np.arange(12.).reshape((4,3)), columns=list('bde'), index=["Uhah", 'Ohio', "Texas", "Oregon"])
In [47]: df1 + df2Out[47]: b c d eColorado NaN NaN NaN NaNOhio 3.0 NaN 6.0 NaNOregon NaN NaN NaN NaNTexas 9.0 NaN 12.0 NaNUhah NaN NaN NaN NaN那么可以使用add方法,传入df2一个fill_valued参数In [8]: df1.add(df2, fill_value=0)Out[8]: b c d eColorado 6.0 7.0 8.0 NaNOhio 3.0 1.0 6.0 5.0Oregon 9.0 NaN 10.0 11.0Texas 9.0 4.0 12.0 8.0Uhah 0.0 NaN 1.0 2.0
DataFrame和Series之间的运算
Series
In [40]: arr = np.arange(12.).reshape((3, 4))In [41]: arrOut[41]:array([[ 0., 1., 2., 3.], [ 4., 5., 6., 7.], [ 8., 9., 10., 11.]])In [42]: arr[0]Out[42]: array([ 0., 1., 2., 3.])In [43]: arr - arr[0]Out[43]:array([[ 0., 0., 0., 0.], [ 4., 4., 4., 4.], [ 8., 8., 8., 8.]])
DataFrame
In [44]: frame = DataFrame(np.arange(12.).reshape((4,3)), columns=list('bde'), index=["Uhah", 'Ohio', "Texas", "Oregon"])In [45]: series = frame.ix[0]In [46]: frame - seriesOut[46]: b d eUhah 0.0 0.0 0.0Ohio 3.0 3.0 3.0Texas 6.0 6.0 6.0Oregon 9.0 9.0 9.0
注意:如果某个索引值在DataFrame的列或Series的索引中找不到, 则参与运算的两个对象就会被重新索引以形成并集
In [47]: series2 = Series(range(3), index=['b', 'e', 'f'])In [48]: frame + series2Out[48]: b d e fUhah 0.0 NaN 3.0 NaNOhio 3.0 NaN 6.0 NaNTexas 6.0 NaN 9.0 NaNOregon 9.0 NaN 12.0 NaN
如果希望列在行上广播,必须使用算术运算方法
In [63]: frame.sub(series, axis=0)Out[63]: b d eUhah -1.0 0.0 1.0Ohio -1.0 0.0 1.0Texas -1.0 0.0 1.0Oregon -1.0 0.0 1.0