# 【3】数据分析--10--科学计算--Pandas--4--计算

## 一、算术运算法则

import pandas as pd
import numpy as np

a= pd.DataFrame(np.arange(12).reshape(3,4))
print a
0  1   2   3
0  0  1   2   3
1  4  5   6   7
2  8  9  10  11

b = pd.DataFrame(np.arange(20).reshape(4,5))
print b
0   1   2   3   4
0   0   1   2   3   4
1   5   6   7   8   9
2  10  11  12  13  14
3  15  16  17  18  19

print a+b
0     1     2     3   4
0   0.0   2.0   4.0   6.0 NaN
1   9.0  11.0  13.0  15.0 NaN
2  18.0  20.0  22.0  24.0 NaN
3   NaN   NaN   NaN   NaN NaN

print a*b
0     1      2      3   4
0   0.0   1.0    4.0    9.0 NaN
1  20.0  30.0   42.0   56.0 NaN
2  80.0  99.0  120.0  143.0 NaN
3   NaN   NaN    NaN    NaN NaN


方法 说明
.sub(d, **argws) 类型间减法运算，可选参数
.mul(d, **argws) 类型间乘法运算，可选参数
.div(d, **argws) 类型间除法运算，可选参数


import pandas as pd
import numpy as np

a= pd.DataFrame(np.arange(12).reshape(3,4))
print a
0  1   2   3
0  0  1   2   3
1  4  5   6   7
2  8  9  10  11

b = pd.DataFrame(np.arange(20).reshape(4,5))
print b
0   1   2   3   4
0   0   1   2   3   4
1   5   6   7   8   9
2  10  11  12  13  14
3  15  16  17  18  19

print e
0      1      2      3      4
0    0.0    2.0    4.0    6.0  104.0
1    9.0   11.0   13.0   15.0  109.0
2   18.0   20.0   22.0   24.0  114.0
3  115.0  116.0  117.0  118.0  119.0

f = a.mul(b,fill_value = 0)
print f
0     1      2      3    4
0   0.0   1.0    4.0    9.0  0.0
1  20.0  30.0   42.0   56.0  0.0
2  80.0  99.0  120.0  143.0  0.0
3   0.0   0.0    0.0    0.0  0.0


import pandas as pd
import numpy as np

b = pd.DataFrame(np.arange(20).reshape(4,5))
print b
0   1   2   3   4
0   0   1   2   3   4
1   5   6   7   8   9
2  10  11  12  13  14
3  15  16  17  18  19

c =pd.Series(np.arange(4))
print c
0    0
1    1
2    2
3    3
dtype: int64

print c-10
0   -10
1    -9
2    -8
3    -7
dtype: int64

print b-c
0     1     2     3   4
0   0.0   0.0   0.0   0.0 NaN
1   5.0   5.0   5.0   5.0 NaN
2  10.0  10.0  10.0  10.0 NaN
3  15.0  15.0  15.0  15.0 NaN

print b.sub(c,axis=0)
0   1   2   3   4
0   0   1   2   3   4
1   4   5   6   7   8
2   8   9  10  11  12
3  12  13  14  15  16


#### 减法 sub

>>> a = pd.DataFrame([2, 1, 1, np.nan], index=['a', 'b', 'c', 'd'],
...                  columns=['one'])
>>> a
one
a  2.0
b  1.0
c  1.0
d  NaN
>>> b = pd.DataFrame(dict(one=[1, np.nan, 1, np.nan],
...                       two=[3, 2, np.nan, 2]),
...                  index=['a', 'b', 'd', 'e'])
>>> b
one  two
a  1.0  3.0
b  NaN  2.0
d  1.0  NaN
e  NaN  2.0
>>> a.sub(b, fill_value=0)
one  two
a  1.0  -3.0
b  1.0  -2.0
c  1.0  NaN
d  -1.0  NaN
e  NaN  -2.0


## 二、 比较运算法则

import pandas as pd
import numpy as np

b = pd.DataFrame(np.arange(12).reshape(3,4))
d = pd.DataFrame(np.arange(12,0,-1).reshape(3,4))
c =pd.Series(np.arange(4))

print b
0  1   2   3
0  0  1   2   3
1  4  5   6   7
2  8  9  10  11

print d
0   1   2  3
0  12  11  10  9
1   8   7   6  5
2   4   3   2  1

print a>d
0      1      2      3
0  False  False  False  False
1  False  False  False   True
2   True   True   True   True

print a==d
0      1      2      3
0  False  False  False  False
1  False  False   True  False
2  False  False  False  False

print a>c
0      1      2      3
0  False  False  False  False
1   True   True   True   True
2   True   True   True   True

print c>0
0    False
1     True
2     True
3     True
dtype: bool


### 3.1 max

from pandas.core.frame import DataFrame
a=[1,2,10,4]#列表a
b=[5,6,7,8]#列表b
bb=['a','b','z','e']#列表b

c={"a" : a,
"b" : b,
"bb" : bb}#将列表a，b转换成字典

data=DataFrame(c)#将字典转换成为数据框

print data

data.drop(columns=['bb'])

print '\n test1'
print data.max(axis=0)   # axis=0 获得每一列的最大值；axis=1，获得每一行的最大值
print type(data.max(axis=0))

print '\n test2'
print data.max(axis=0).values  #获得series的值
print data.max(axis=0).index   # 获得series的索引

print '\n test3'

print data.max(axis=1)

print(data)


    a  b bb
0   1  5  a
1   2  6  b
2  10  7  z
3   4  8  e

test1
a     10
b      8
bb     z
dtype: object
<class 'pandas.core.series.Series'>

test2
[10 8 'z']
Index([u'a', u'b', u'bb'], dtype='object')

test3
0    a
1    b
2    z
3    e
dtype: object
a  b bb
0   1  5  a
1   2  6  b
2  10  7  z
3   4  8  e