【基本工具】S02E09 Series与DataFrame对象的数值运算

# 0.本集概览

1.DataFrame和Series一元运算会作用于每一个数据元素
2.DataFrame间和Sereis间的二元运算会自动对齐索引，并进行缺失值处理
3.DataFrame和Series之间可以进行行、列两方向上的运算，同样会对齐索引

# 1.一元运算作用于每一个数据元素

import pandas as pd
import numpy as np

ser = pd.Series([2,4,6,8],
index=['a','b','c','d'])
print(ser)
print(np.exp(ser))

a    2
b    4
c    6
d    8
dtype: int64

a       7.389056
b      54.598150
c     403.428793
d    2980.957987
dtype: float64

DataFrame数据对象的操作也是一样的：

import pandas as pd
import numpy as np

rng = np.random.RandomState(18)
df = pd.DataFrame(rng.randint(0,10,(3,4)),
columns=['a','b','c','d'])
print(df)
print(np.sin(df * np.pi / 4))

   a  b  c  d
0  3  8  5  1
1  2  2  8  8
2  2  1  5  5

a             b             c             d
0  0.707107 -2.449294e-16 -7.071068e-01  7.071068e-01
1  1.000000  1.000000e+00 -2.449294e-16 -2.449294e-16
2  1.000000  7.071068e-01 -7.071068e-01 -7.071068e-01

# 2.二元运算

## 2.1.基本方法

import pandas as pd
import numpy as np

ser1 = pd.Series({'a':10,'b':20,'d':40})
ser2 = pd.Series({'b':2,'c':3,'d':4})
print(ser1 / ser2)

a     NaN
b    10.0
c     NaN
d    10.0
dtype: float64

## 2.2.缺失值的处理

import pandas as pd
import numpy as np

ser1 = pd.Series({'a':10,'b':20,'d':40})
ser2 = pd.Series({'b':2,'c':3,'d':4})
print(ser1.add(ser2, fill_value=0))

a    10.0
b    22.0
c     3.0
d    44.0
dtype: float64

DataFrame间的运算，当然也同样会进行索引对齐：

import pandas as pd
import numpy as np

rng = np.random.RandomState(10)
A = pd.DataFrame(rng.randint(0, 20, (2, 2)),
columns=['A', 'B'])
B = pd.DataFrame(rng.randint(0, 10, (3, 3)),
columns=['B', 'C', 'A'])
print(A)
print(B)
print(A + B)

    A  B
0   9  4
1  15  0

B  C  A
0  1  9  0
1  1  8  9
2  0  8  6

A    B   C
0   9.0  5.0 NaN
1  24.0  1.0 NaN
2   NaN  NaN NaN