数据运算
算术运算
df
>>>
Z1 Z2 C1 C2
0 36 37 1 2.2
1 47 9 3 4.6
2 28 92 5 6.4
3 9 2 7 8.9
# 两列相加
df['Z1'] + df['C1']
>>>
0 37
1 50
2 33
3 16
dtype: int64
#两列相减
df['Z1'] - df['C1']
>>>
0 35
1 44
2 23
3 2
dtype: int64
#两列想乘
df['Z1'] * df['C1']
>>>
0 36
1 141
2 140
3 63
dtype: int64
# 两列相除
df['Z1'] / df['C1']
>>>
0 36.000000
1 15.666667
2 5.600000
3 1.285714
dtype: float64
比较运算
df['C1'] > df['C2']
>>>
0 False
1 False
2 False
3 False
dtype: bool
df['C1'] < df['C2']
>>>
0 True
1 True
2 True
3 True
dtype: bool
df['C1'] != df['C2']
>>>
0 True
1 True
2 True
3 True
dtype: bool
汇总运算
count 非空值计数
count()默认求每一列的非空数值的个数。添加参数axis = 1求每一行的非空数值的个数
df.count() #求每一列的非空数值个数
>>>
Z1 4
Z2 4
C1 4
C2 4
dtype: int64
df.count(axis = 1) #求每一行的非空数值个数
>>>
0 4
1 4
2 4
3 4
dtype: int64
df['Z2'].count() #单独查看Z2列的非空值个数
>>>
4
sum 求和
df.sum() #默认对每一列求和
>>>
Z1 120.0
Z2 140.0
C1 16.0
C2 22.1
dtype: float64
df.sum(axis = 1) #默认对每一行求和
>>>
0 76.2
1 63.6
2 131.4
3 26.9
dtype: float64
df['C1'].sum() #针对c1列求和
>>>
16