numpy、pandas入门

numpy入门:
1、数组创建

import pandas as pd
import numpy as np
#使用numpy创建数组
t1= np.arange(0,9)	#9取不到
t2=np.array([1,2,6],dtype=bool)
print(t1)
print(t2)
#更改数据类型
t3=np.array([1.9,2.4,6.3],dtype=int)
print(t3)
t4=t3.astype(float)
print(t4)

#小数确定精度
t6=np.array([1.92222,2.42222,6.32222])
t5=np.round(t6,2)
print(t5)
[0 1 2 3 4 5 6 7 8]
[ True  True  True]
[1 2 6]
[1. 2. 6.]
[1.92 2.42 6.32]

2、数组变形

import pandas as pd
import numpy as np
#数组变形
t1= np.arange(0,24)
t2=t1.reshape(2,3,4)	#2块、三行、4元素
print(t1)
print(t2)
#一维数组
t3=t2.reshape(24)	
t4=t2.flatten()
print(t3)
print(t4)
[ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23]
[[[ 0  1  2  3]
  [ 4  5  6  7]
  [ 8  9 10 11]]

 [[12 13 14 15]
  [16 17 18 19]
  [20 21 22 23]]]
[ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23]
[ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23]

Process finished with exit code 0

3、数组计算

import pandas as pd
import numpy as np
#数组计算
t1= np.arange(1,10).reshape(3,3)
t2= np.arange(3,12).reshape(3,3)
print("t1=\n",t1)
print("t2=\n",t2)
print("t1+t2=\n",t1+t2)
print("t1*t2=\n",t1*t2)
print("t1+5=\n",t1+5)
t1=
 [[1 2 3]
 [4 5 6]
 [7 8 9]]
t2=
 [[ 3  4  5]
 [ 6  7  8]
 [ 9 10 11]]
t1+t2=
 [[ 4  6  8]
 [10 12 14]
 [16 18 20]]
t1*t2=
 [[ 3  8 15]
 [24 35 48]
 [63 80 99]]
t1+5=
 [[ 6  7  8]
 [ 9 10 11]
 [12 13 14]]

Process finished with exit code 0

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值