pandas -对数、指数、开方、判断运算及去重
对数、指数、开方、判断运算、三元运算符、去重及常用统计指标
·
1、exp、自然常数e为底的指数函数
numpy.exp():返回e的幂次方,e是一个常数为2.71828
import numpy as np
a = 1
print np.exp(a)
a = 2
print np.exp(a)
# 结果:
2.71828182846
7.38905609893
2、np.power指数运算
power(x1, x2, /, out=None, *,
where=True, casting=‘same_kind’,
order=‘K’, dtype=None,
subok=True[, signature, extobj]
)
幂运算(指数运算),对x1中的元素进行x2中元素次幂的计算。
- x1 : array_like,底数
- x2 : array_like,指数
mport numpy as np
result = np.power([-2, -1, 0, 1, 2], 3)
# [-8 -1 0 1 8]
result = np.power([-2, -1, 0, 1, 2], [3, 3, 3, 3, 3])
# [-8 -1 0 1 8]
result = np.power([-2, -1, 0, 1, 2], [0, 1, 2, 3, 4])
# [ 1 -1 0 1 16]
result = np.power([-2, -1, 0, 1, 2], np.array([[0, 1, 2, 3, 4], [0, 0, 0, 0, 0]]))
"""
[[ 1 -1 0 1 16]
[ 1 1 1 1 1]
]
"""
result = np.power([-2, -1, 0, 1, 2], np.array([[0, 1, 2, 3, 4], [0, 2, 0, 0, 2]]))
"""
[[ 1 -1 0 1 16]
[ 1 1 1 1 4]
]
"""
3、对数运算
np.log(x) #就是数学中的ln(x)
np.log2(x)
np.log10(x)
4、sqrt() 元素的开方
import numpy as np
B = np.arange(3)
print (np.sqrt(B)) # B里每个元素的开方
5、通用判断函数
np.all()
# 判断前两名同学的成绩[0:2, :]是否全及格
np.all(score[0:2, :] > 60)
False
np.any()
判断前两名同学的成绩[0:2, :]是否有大于90分的
np.any(score[0:2, :] > 80)
6、三元运算符
np.where()
# 判断成绩数据中大于60的置为1,否则为0
temp = score[:4, :4]
np.where(temp > 60, 1, 0)
# 复合逻辑需要结合np.logical_and和np.logical_or使用
# 判断大于60且小于90的换为1,否则为0
np.where(np.logical_and(temp > 60, temp < 90), 1, 0)
# 判断大于90或小于60的换为1,否则为0
np.where(np.logical_or(temp > 90, temp < 60), 1, 0)
7、去重
drop_duplicates()删除重复的行,返回的是删除重后的df
DataFrame.drop_duplicates(subset=None, keep='first', inplace=False)
"""
参数:
subset是用来指定特定的列,默认为所有列
keep
当keep='first'时,就是保留第一次出现的重复行,其余删除
当keep='last'时,就是保留最后一次出现的重复行,其余删除
当keep=False时,就是删除所有重复行
inplace是指是否直接在原数据上进行修改,默认为否
"""
7、 常用统计指标
median(a, axis) # 中位数
Compute the median along the specified axis.
mean(a, axis, dtype) # 均值
Compute the arithmetic mean along the specified axis.
std(a, axis, dtype) # 标准差
Compute the standard deviation along the specified axis.
var(a, axis, dtype) # 方差
Compute the variance along the specified axis.
np.argmax(axis=) # 最大元素对应的下标
np.argmin(axis=) # 最小元素对应的下标
num1 = np.array([[1,2,3],[2,3,4],[3,4,5],[4,5,6]])
num2 = np.mat(num1)
print(num2)
print(type(num2))
print(np.mean(num2)) # 对所有元素求均值
print(np.mean(num2,0)) # 对各列求均值
np.mat() # 函数用于将输入解释为矩阵。
np.matrix() # 函数用于从类数组对象或数据字符串返回矩阵。
np.array() # 函数用于创建一个数组。
8、参考
更多推荐
所有评论(0)