定義:是一種特殊的并行計(jì)算的方式,可以同一時(shí)間執(zhí)行多次操作,通常是對(duì)不同的數(shù)據(jù)執(zhí)行同一個(gè)或同一批指令鸭栖。主要用于pandas的Series系列和DataFrame數(shù)據(jù)框智哀。
1.生成等差數(shù)組
numpy.arange(start, end, step)
,取值前閉后開宠互。
import numpy
r = numpy.arange(0.1, 1, 0.1)
Out[180]: array([ 0.1, 0.2, 0.3, 0.4, 0.5,
0.6, 0.7, 0.8, 0.9])
2.四則計(jì)算與函數(shù)計(jì)算
相同位置的數(shù)據(jù)進(jìn)行四則或函數(shù)計(jì)算,返回的結(jié)果保存在相同位置。
#numpy的乘方函數(shù)power
numpy.power(r, 5) #r的五次方
Out[182]:
array([ 1.00000000e-05, 3.20000000e-04, 2.43000000e-03,
1.02400000e-02, 3.12500000e-02, 7.77600000e-02,
1.68070000e-01, 3.27680000e-01, 5.90490000e-01])
3.比較運(yùn)算
r > 0.3
Out[183]: array([False, False, True, True, True,
True, True, True, True], dtype=bool)
#結(jié)合過濾一起使用耕陷,如果是True就保留,如果是False就過濾
r[r>0.3]
Out[184]: array([ 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9])
4.矩陣運(yùn)算
numpy.dot(r, r.T)
Out[185]: 2.8500000000000005
5.數(shù)據(jù)框運(yùn)算
numpy.random.randn
生成隨機(jī)數(shù)的數(shù)據(jù)框
from pandas import DataFrame
df = DataFrame({
'column1': numpy.random.randn(5),
'column2': numpy.random.randn(5),
'column3': numpy.random.randn(5)
})
Out[187]:
column1 column2 column3
0 -0.611932 -1.690942 0.092462
1 -0.765934 0.794254 1.123755
2 -1.591180 0.340763 -0.232495
3 1.463467 -0.588498 -0.208558
4 -0.338739 0.907178 -1.146147
#apply()調(diào)用數(shù)據(jù)框的每一列傳遞給min函數(shù)据沈,由其選出每一列的最小值哟沫。
#二維數(shù)組有兩個(gè)軸:第0軸沿行垂直往下,第1軸沿列水平延伸锌介。
df.apply(min, axis=0) #axis=0為默認(rèn)值
Out[188]:
column1 -1.591180
column2 -1.690942
column3 -1.146147
dtype: float64
df.apply(min, axis=1)
Out[189]:
0 -1.690942
1 -0.765934
2 -1.591180
3 -0.588498
4 -1.146147
dtype: float64
#判斷每個(gè)列嗜诀,值是否都大于0
df.apply(
lambda x: numpy.all(x>0),
axis=1
)
Out[191]:
0 False
1 False
2 False
3 False
4 False
dtype: bool
#結(jié)合過濾
df[df.apply(
lambda x: numpy.all(x>0),
axis=1
)]
Out[192]:
Empty DataFrame
Columns: [column1, column2, column3]
Index: []