DataFrame的计算
import numpy as np
import pandas as pd
1. DataFrame数值计算
DataFrame.add(other, axis='columns', level=None, fill_value=None)
: +DataFrame.sub(other, axis='columns', level=None, fill_value=None)
: -DataFrame.mul(other, axis='columns', level=None, fill_value=None)
: *DataFrame.div(other, axis='columns', level=None, fill_value=None)
: /DataFrame.floordiv(other, axis='columns', level=None, fill_value=None)
: //DataFrame.mod(other, axis='columns', level=None, fill_value=None)
: %DataFrame.pow(other, axis='columns', level=None, fill_value=None)
: **
参数:
other: scalar, sequence, Series, or DataFrame
axis : {0 or 'index', 1 or 'columns'}
level : int or label
fill_value : float or None, default None
# 定义对象
>>> df = pd.DataFrame(data=np.array([(x, x+1, x+2, x+3, x+4) for x in range(0,25,5)],
dtype=[('col1', 'i4'), ('col2', 'i8'), ('col3', 'i4'), ('col4','f8'), ('col5', 'b')]))
>>> df
col1 col2 col3 col4 col5
0 0 1 2 3.0 4
1 5 6 7 8.0 9
2 10 11 12 13.0 14
3 15 16 17 18.0 19
4 20 21 22 23.0 24
>>> df + 2
col1 col2 col3 col4 col5
0 2 3 4 5.0 6
1 7 8 9 10.0 11
2 12 13 14 15.0 16
3 17 18 19 20.0 21
4 22 23 24 25.0 26
>>> df - [1, 2, 3, 2, 1]
col1 col2 col3 col4 col5
0 -1 -1 -1 1.0 3
1 4 4 4 6.0 8
2 9 9 9 11.0 13
3 14 14 14 16.0 18
4 19 19 19 21.0 23
>>> df.mul(pd.DataFrame({'col2': (1, 2, 3, 4, 5)}), fill_value=1)
col1 col2 col3 col4 col5
0 0.0 1 2.0 3.0 4.0
1 5.0 12 7.0 8.0 9.0
2 10.0 33 12.0 13.0 14.0
3 15.0 64 17.0 18.0 19.0
4 20.0 105 22.0 23.0 24.0
>>> df.div(pd.DataFrame({'col2': (1, 2, 3, 4, 5)}), fill_value=10)
col1 col2 col3 col4 col5
0 0.0 1.000000 0.2 0.3 0.4
1 0.5 3.000000 0.7 0.8 0.9
2 1.0 3.666667 1.2 1.3 1.4
3 1.5 4.000000 1.7 1.8 1.9
4 2.0 4.200000 2.2 2.3 2.4
>>> df // 2
col1 col2 col3 col4 col5
0 0 0 1 1.0 2
1 2 3 3 4.0 4
2 5 5 6 6.0 7
3 7 8 8 9.0 9
4 10 10 11 11.0 12
>>> df % 2
col1 col2 col3 col4 col5
0 0 1 0 1.0 0
1 1 0 1 0.0 1
2 0 1 0 1.0 0
3 1 0 1 0.0 1
4 0 1 0 1.0 0
>>> df ** 2
col1 col2 col3 col4 col5
0 0 1 4 9.0 16
1 25 36 49 64.0 81
2 100 121 144 169.0 -60
3 225 256 289 324.0 105
4 400 441 484 529.0 64
2. DataFrame布尔运算:
DataFrame.lt(other, axis='columns', level=None)
: <DataFrame.gt(other, axis='columns', level=None)
: >DataFrame.le(other, axis='columns', level=None)
: <=DataFrame.ge(other, axis='columns', level=None)
: >=DataFrame.ne(other, axis='columns', level=None)
: !=DataFrame.eq(other, axis='columns', level=None)
: ==
参数:
other: Series or scalar value
fill_value: None or float value, default None
在计算之前,用于填充缺失值。level: int or name
>>> df < 10
col1 col2 col3 col4 col5
0 True True True True True
1 True True True True True
2 False False False False False
3 False False False False False
4 False False False False False
>>> df > [5, 10, 5, 20, 5]
col1 col2 col3 col4 col5
0 False False False False False
1 False False True False True
2 True True True False True
3 True True True False True
4 True True True True True
>>> df.le(pd.DataFrame({'col2': (2, 6, 8, 10, 12)}))
col1 col2 col3 col4 col5
0 False True False False False
1 False True False False False
2 False False False False False
3 False False False False False
4 False False False False False
>>> df.ge(pd.DataFrame({'col2': (1, 15, 30, 4, 5)}))
col1 col2 col3 col4 col5
0 False True False False False
1 False False False False False
2 False False False False False
3 False True False False False
4 False True False False False
6
df != 6
>>> df != 6
col1 col2 col3 col4 col5
0 True True True True True
1 True False True True True
2 True True True True True
3 True True True True True
4 True True True True True
>>> df == 4
col1 col2 col3 col4 col5
0 False False False False True
1 False False False False False
2 False False False False False
3 False False False False False
4 False False False False False