散乱1

逆序数组的方法:
如数组x,
x[::-1]
注:当步长值为负时,start参数和stop参数默认是被交换的。

数组也可以用布尔索引。

list.index()返回列表元素的序号。

列表的方法,其实也算是一种函数。

对于嵌套列表来说,list[0][0]返回子列表的元素。

数组和dataframe都有.shape

pd.concat连接多个dataframe

pd.merge()类似于SQL的联结。

astype()方法,转换数据类型。

缺失值的处理:删它填它不管它

list.extend() 追加列表


sorted() function sorts data in numerical or alphabetical order and returns a new list


import csv
csvfile=open(r'')
for row in csv.reader(csvfile):
    print(row)

csvfile.close()


round(number,[,ndigits]) 当只有一个参数时,返回integer

np.random.normal(a,b,c) a:distribution mean,b:distribution standard dev,c:number of samples;

任何通用函数的reduce方法,会对给定的元素和操作重复执行,直至得到单个的结果:
x为数组
np.add.reduce(x) 返回数组中所有元素的和;
np.multiply.reduce(x) 返回数组中所有元素和乘积;


如何需要存储每次计算的中间结果,可以使用accumulate:
in[88]:np.add.accumulate(np.arange(1,5))
Out[88]: array([ 1,  3,  6, 10], dtype=int32)

in[88]:np.multiply.accumulate(np.arange(1,5))
Out[89]: array([ 1,  2,  6, 24], dtype=int32)


外积:
任何通用函数都可以用outer方法获得两个不同输入数组所有元素对的函数运算结果。
可用此法生成乘法表:
x=np.arange(1,10)

np.multiply.outer(x,x)
Out[91]: 
array([[ 1,  2,  3,  4,  5,  6,  7,  8,  9],
       [ 2,  4,  6,  8, 10, 12, 14, 16, 18],
       [ 3,  6,  9, 12, 15, 18, 21, 24, 27],
       [ 4,  8, 12, 16, 20, 24, 28, 32, 36],
       [ 5, 10, 15, 20, 25, 30, 35, 40, 45],
       [ 6, 12, 18, 24, 30, 36, 42, 48, 54],
       [ 7, 14, 21, 28, 35, 42, 49, 56, 63],
       [ 8, 16, 24, 32, 40, 48, 56, 64, 72],
       [ 9, 18, 27, 36, 45, 54, 63, 72, 81]])


np.sum()
np.min()
np.max()

对于min、max、sum等其它numpy聚合,一种更简洁的语法形式是数组对象直接调用这些方法:如np.array.min()等等

numpy大多数的聚合都有对nan值的安全处理策略(NaN-safe),即计算时忽略所有的缺失值:

np.nansum(np.array([1,3,2,np.nan]))  返回nan

np.nansum(np.array([1,3,2,np.nan]))  返回6.0

np.percentile(x,q)计算数组的分位数:
如np.percentile(heights,25)


 

猜你喜欢

转载自blog.csdn.net/gulie8/article/details/89001428
今日推荐