Pandas--运算符与运算函数

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import pandas as pd

# 1.算术运算符
# 1.1 加法运算符
# 1.1.1 Series加法运算
s1 = pd.Series([1, 2, 3, 4, 5], index=list('abcde'))
s2 = pd.Series([6, 7, 8, 9, 10], index=list('abcde'))
print(s1 + s2)
# a     7
# b     9
# c    11
# d    13
# e    15
# dtype: int64

# 1.1.2 DataFrame加法运算
df1 = pd.DataFrame([[1, 2, 3], [4, 5, 6], [7, 8, 9]], index=list('abc'), columns=list('ABC'))
df2 = pd.DataFrame([[10, 20, 30], [40, 50, 60], [70, 80, 90]], index=list('abc'), columns=list('ABC'))
print(df1 + df2)
#     A   B   C
# a  11  22  33
# b  44  55  66
# c  77  88  99

# 1.2 减法运算符
# 1.2.1 Series减法运算
print(s1 - s2)
# a   -5
# b   -5
# c   -5
# d   -5
# e   -5
# dtype: int64

# 1.2.2 DataFrame减法运算
print(df1 - df2)
#    A   B   C
# a -9 -18 -27
# b -36 -45 -54
# c -63 -72 -81

# 1.3 乘法运算符
# 1.3.1 Series乘法运算
print(s1 * s2)
# a     6
# b    14
# c    24
# d    36
# e    50
# dtype: int64

# 1.3.2 DataFrame乘法运算
print(df1 * df2)
#      A    B    C
# a   10   40   90
# b  160  250  360
# c  490  640  810

# 1.4 除法运算符
# 1.4.1 Series除法运算
print(s1 / s2)
# a    0.166667
# b    0.285714
# c    0.375000
# d    0.444444
# e    0.500000
# dtype: float64

# 1.4.2 DataFrame除法运算
print(df1 / df2)
#           A         B         C
# a  0.100000  0.100000  0.100000
# b  0.100000  0.100000  0.100000
# c  0.100000  0.100000  0.100000

# 1.5 求余运算符
# 1.5.1 Series求余运算
print(s1 % s2)
# a    1
# b    2
# c    3
# d    4
# e    5
# dtype: int64

# 1.5.2 DataFrame求余运算
print(df1 % df2)
#    A   B   C
# a  1   2   3
# b  4   5   6
# c  7   8   9

# 1.6 求幂运算符
# 1.6.1 Series求幂运算
print(s1 ** s2)
# a             1
# b           128
# c          6561
# d        262144
# e      9765625
# dtype: int64

# 1.6.2 DataFrame求幂运算
print(df1 ** df2)
#             A           B           C
# a           1         1024     5904900
# b  1099511627776  97656250000  2176782336
# c  576650390625  85899345920  3486784401

# 2.比较运算符
# 2.1 Series比较运算
print(s1 > s2)
# a    False
# b    False
# c    False
# d    False
# e    False
# dtype: bool

# 2.2 DataFrame比较运算
print(df1 > df2)
#       A      B      C
# a  False  False  False
# b  False  False  False
# c  False  False  False
    
# 3.逻辑运算符
# 3.1 Series逻辑运算
print(s1 & s2)
# a     True
# b     True
# c     True
# d     True
# e     True
# dtype: bool

# 3.2 DataFrame逻辑运算
print(df1 & df2)
#       A      B      C
# a  True   True   True
# b  True   True   True
# c  True   True   True

# 4.运算函数
# 4.1 Series运算函数
# 4.1.1 Series求和运算
print(s1.sum())
# 15

# 4.1.2 Series求平均值运算
print(s1.mean())
# 3.0

# 4.1.3 Series求最大值运算
print(s1.max())
# 5

# 4.1.4 Series求最小值运算
print(s1.min())
# 1

# 4.1.5 Series求中位数运算
print(s1.median())
# 3.0

# 4.1.6 Series求标准差运算
print(s1.std())
# 1.4142135623730951

# 4.1.7 Series求方差运算
print(s1.var())
# 2.0

# 4.1.8 Series求绝对值运算
print(s1.abs())
# a    1
# b    2
# c    3
# d    4
# e    5
# dtype: int64

# 4.1.9 Series求累加运算
print(s1.cumsum())
# a     1
# b     3
# c     6
# d    10
# e    15
# dtype: int64

# 4.1.10 Series求累乘运算
print(s1.cumprod())
# a      1
# b      2
# c      6
# d     24
# e    120
# dtype: int64

# 4.1.11 Series求累差运算
print(s1.diff())
# a    NaN
# b    1.0
# c    1.0
# d    1.0
# e    1.0
# dtype: float64

# 4.1.12 Series求累积百分比运算
print(s1.pct_change())
# a         NaN
# b    1.000000
# c    0.500000
# d    0.333333
# e    0.250000
# dtype: float64

# 4.2 DataFrame运算函数
# 4.2.1 DataFrame求和运算
print(df1.sum())
# A    6
# B    15
# C    28
# dtype: int64
    
# 4.2.2 DataFrame求平均值运算
print(df1.mean())
# A    2.0
# B    5.0
# C    9.333333
# dtype: float64

# 4.2.3 DataFrame求最大值运算
print(df1.max())
# A    3
# B    6
# C    9
# dtype: int64

# 4.2.4 DataFrame求最小值运算
print(df1.min())
# A    1
# B    2
# C    3
# dtype: int64

# 4.2.5 DataFrame求中位数运算
print(df1.median())
# A    2.0
# B    5.0
# C    8.0
# dtype: float64

# 4.2.6 DataFrame求标准差运算
print(df1.std())
# A    1.0
# B    1.0
# C    1.0
# dtype: float64

# 4.2.7 DataFrame求方差运算
print(df1.var())
# A    1.0
# B    1.0
# C    1.0
# dtype: float64

# 4.2.8 DataFrame求绝对值运算
print(df1.abs())
#    A  B  C
# a  1  2  3
# b  2  4  6
# c  3  6  9

# 4.2.9 DataFrame求累加运算
print(df1.cumsum())
#    A   B   C
# a  1   2   3
# b  3   6   9
# c  6  12  18

# 4.2.10 DataFrame求累乘运算
print(df1.cumprod())
#    A   B    C
# a  1   2    3
# b  2   8   18
# c  6  48  162

# 4.2.11 DataFrame求累差运算
print(df1.diff())
#      A    B    C
# a  NaN  NaN  NaN
# b  1.0  2.0  3.0
# c  1.0  2.0  3.0

# 4.2.12 DataFrame求累积百分比运算
print(df1.pct_change())
#      A    B    C
# a  NaN  NaN  NaN
# b  1.0  1.0  1.0
# c  0.5  0.5  0.5

# 5.排序函数
# 5.1 Series排序函数
# 5.1.1 Series排序
print(s1.sort_values())
# a    1
# b    2
# c    3
# d    4
# e    5
# dtype: int64

# 5.1.2 Series降序排序
print(s1.sort_values(ascending=False))
# e    5
# d    4
# c    3
# b    2
# a    1
# dtype: int64
    
# 5.1.3 Series根据索引排序
print(s1.sort_index())
# a    1
# b    2
# c    3
# d    4
# e    5
# dtype: int64

# 5.1.4 Series根据索引降序排序
print(s1.sort_index(ascending=False))
# e    5
# d    4
# c    3
# b    2
# a    1
# dtype: int64
    
# 5.2 DataFrame排序函数
# 5.2.1 DataFrame排序
print(df1.sort_values(by='A'))
#    A  B  C
# a  1  2  3
# b  2  4  6
# c  3  6  9

# 5.2.2 DataFrame降序排序
print(df1.sort_values(by='A',ascending=False))
#    A  B  C
# c  3  6  9
# b  2  4  6
# a  1  2  3

# 5.2.3 DataFrame根据索引排序
print(df1.sort_index())
#    A  B  C
# a  1  2  3
# b  2  4  6
# c  3  6  9

# 5.2.4 DataFrame根据索引降序排序
print(df1.sort_index(ascending=False))
#    A  B  C
# c  3  6  9
# b  2  4  6
# a  1  2  3

# 6.统计函数
# 6.1 Series统计函数
# 6.1.1 Series求和运算
print(s1.sum())
# 15
    
# 6.1.2 Series求平均值运算
print(s1.mean())
# 3.0

# 6.1.3 Series求最大值运算
print(s1.max())
# 5

# 6.1.4 Series求最小值运算
print(s1.min())
# 1

# 6.1.5 Series求中位数运算
print(s1.median())
# 3.0

# 6.1.6 Series求标准差运算
print(s1.std())
# 1.4142135623730951

# 6.1.7 Series求方差运算
print(s1.var())
# 2.0

# 6.1.8 Series求绝对值运算
print(s1.abs())
# a    1
# b    2
# c    3
# d    4
# e    5
# dtype: int64

# 6.1.9 Series求累加运算
print(s1.cumsum())
# a     1
# b     3
# c     6
# d    10
# e    15
# dtype: int64

# 6.1.10 Series求累乘运算
print(s1.cumprod())
# a      1
# b      2
# c      6
# d     24
# e    120
# dtype: int64

# 6.1.11 Series求累差运算
print(s1.diff())
# a    NaN
# b    1.0
# c    1.0
# d    1.0
# e    1.0
# dtype: float64

# 6.1.12 Series求累积百分比运算
print(s1.pct_change())
# a         NaN
# b    1.000000
# c    0.500000
# d    0.333333
# e    0.250000
# dtype: float64

# 6.2 DataFrame统计函数
# 6.2.1 DataFrame求和运算
print(df1.sum())
# A     6
# B    12
# C    18
# dtype: int64
    
# 6.2.2 DataFrame求平均值运算
print(df1.mean())
# A    2.0
# B    4.0
# C    6.0
# dtype: float64
    
# 6.2.3 DataFrame求最大值运算
print(df1.max())
# A    3
# B    6
# C    9
# dtype: int64

# 6.2.4 DataFrame求最小值运算
print(df1.min())
# A    1
# B    2
# C    3
# dtype: int64

# 6.2.5 DataFrame求中位数运算
print(df1.median())
# A    2.0
# B    4.0
# C    6.0
# dtype: float64
    
# 6.2.6 DataFrame求标准差运算
print(df1.std())
# A    1.0
# B    2.0
# C    3.0
# dtype: float64

# 6.2.7 DataFrame求方差运算
print(df1.var())
# A    1.0
# B    4.0
# C    9.0
# dtype: float64

# 6.2.8 DataFrame求绝对值运算
print(df1.abs())
#    A  B  C
# a  1  2  3
# b  2  4  6
# c  3  6  9

# 6.2.9 DataFrame求累加运算
print(df1.cumsum())
#    A   B   C
# a  1   2   3
# b  3   6   9
# c  6  12  18

# 6.2.10 DataFrame求累乘运算
print(df1.cumprod())
#    A   B   C
# a  1   2   3
# b  2   8  18
# c  6  48  162

# 6.2.11 DataFrame求累差运算
print(df1.diff())
#      A    B    C
# a  NaN  NaN  NaN
# b  1.0  2.0  3.0
# c  1.0  2.0  3.0

# 6.2.12 DataFrame求累积百分比运算
print(df1.pct_change())
#      A    B    C
# a  NaN  NaN  NaN
# b  1.0  1.0  1.0
# c  0.5  0.5  0.5
    

你可能感兴趣的:(Python自学记录,pandas,python,开发语言)