python数组如何添加整行或整列
作者:johnjim0816 时间:2022-04-29 16:39:30
python数组添加整行或整列
代码如下,见注释:
import numpy as np
m_list=[[1,2,3],[3,4,5]]
m_arr=np.array(m_list) # 转为数组
print(np.append(m_arr,[[1,1,1]],axis=0)) # 添加整行元素
print(np.append(m_arr,[[1],[1]],axis=1)) # 添加整列
ones_row=np.ones(len(m_arr[0]),int) # 生成全为1的一维列表,即【1,1,1】
print(np.append(m_arr,[ones_row],axis=0))
ones_col=[[1] for i in range(len(m_arr))] # 生成全为1的二维嵌套列表,即[[1],[1]]
print(np.append(m_arr,ones_col,axis=1))
结果如下:
[[1 2 3]
[3 4 5]
[1 1 1]]
[[1 2 3 1]
[3 4 5 1]]
[[1 2 3]
[3 4 5]
[1 1 1]]
[[1 2 3 1]
[3 4 5 1]]
使用numpy的方法delete删除数组的整行和整列
1.首先要导入第三方库 numpy
import numpy as np
2.利用delete()删除数组整行和整列的通用格式
np.delete(arr, obj, axis=None)
2.1.参数:
arr
: 输入数组obj
: 切片,整数,表示哪个子数组要被移除axis
: 删除子数组的轴axis = 0
: 表示删除数组的行axis = 1
: 表示删除数组的列axis = None
:表示把数组按一维数组平铺在进行索引删除
2.2.返回值:一个新的子数组
3.具体实现历程
删除单行操作:
arr=np. array([[1,2,3],[4,5,6],[7,8,9]])
print arr
arr=np. delete(arr,2, axis=0)
print arr
删除多行操作:
arr=np. array([[1,2,3],[4,5,6],[7,8,9]])
print arr
arr=np. delete(arr,[1,2], axis=0)
print arr
删除单列操作:
arr=np. array([[1,2,3],[4,5,6],[7,8,9]])
print arr
arr=np. delete(arr,1, axis=1)
print arr
删除多列操作:
arr=np. array([[1,2,3],[4,5,6],[7,8,9]])
print arr
arr=np. delete(arr,[1,2], axis=1)
print arr
axis = None 操作
arr=np. array([[1,2,3],[4,5,6],[7,8,9]])
print arr
arr=np. delete(arr,1, axis=None)
print arr
来源:https://johnjim0816.blog.csdn.net/article/details/106600466
标签:python,数组,整行,整列
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
MySQL Order By索引优化
2011-01-04 19:56:00
详解Django之auth模块(用户认证)
2021-12-11 21:09:20
![](https://img.aspxhome.com/file/2023/4/134494_0s.png)
Tensorflow 训练自己的数据集将数据直接导入到内存
2023-08-18 14:30:14
![](https://img.aspxhome.com/file/2023/5/75965_0s.png)
pandas ix &iloc &loc的区别
2023-03-12 16:31:54
python线程优先级队列知识点总结
2022-01-04 21:25:46
Python+OpenCV图像处理——实现轮廓发现
2023-12-12 23:30:31
![](https://img.aspxhome.com/file/2023/5/97505_0s.png)
Python基于Tensorflow2.X实现汽车油耗预测
2021-05-05 05:25:22
![](https://img.aspxhome.com/file/2023/3/72743_0s.png)
python爬虫 批量下载zabbix文档代码实例
2022-11-07 11:10:29
Python实现程序判断季节的代码示例
2022-04-04 13:30:22
![](https://img.aspxhome.com/file/2023/1/76941_0s.png)
python 读取yaml文件的两种方法(在unittest中使用)
2021-04-20 17:38:30
python实现图片转换成素描和漫画格式
2023-09-20 14:44:25
![](https://img.aspxhome.com/file/2023/1/80711_0s.jpg)
pyqt5对用qt designer设计的窗体实现弹出子窗口的示例
2023-01-28 12:58:07
ASP中生成文件的两种方式
2007-08-30 09:49:00
Python Excel vlookup函数实现过程解析
2022-05-24 06:12:07
![](https://img.aspxhome.com/file/2023/5/112365_0s.png)
python for循环内输出和外输出方式
2022-09-25 10:19:06
![](https://img.aspxhome.com/file/2023/9/124129_0s.jpg)
python中defaultdict字典功能特性介绍
2022-06-07 23:27:45
![](https://img.aspxhome.com/file/2023/7/99037_0s.png)
详解python调度框架APScheduler使用
2021-11-05 22:55:36
python检测某个变量是否有定义的方法
2021-05-17 18:45:01
Python urlopen()函数 示例分享
2022-08-15 01:00:58
如何基于Python制作有道翻译小工具
2023-10-01 21:00:58
![](https://img.aspxhome.com/file/2023/2/62922_0s.png)