python - Python:如何从矩阵中迭代地删除行和列?
问题描述
我是初学者,自学成才。
我想为方阵迭代地删除行和列。
我的第一次尝试是:
for i in range(0,c):
if Atomos[i] == "H":
d_nh=np.delete(np.delete(d, i, 0), i, 1)
但是最终的和新的矩阵显示只删除了一行和一列。
如何迭代地删除行和列?
这两个文件的内容是:
马特里兹
0.000000000000000000e+00 1.205475107329385898e+00 1.342931901022796248e+00 2.343013632351988562e+00 3.227383136403330077e+00 2.640130058756467779e+00 2.640148435557436279e+00 1.478495377186577864e+00 2.442752671162299549e+00 3.440470104931585560e+00 2.650641510969556247e+00 2.173942147030341054e+00
1.205475107329385898e+00 0.000000000000000000e+00 2.245467917547002035e+00 2.644315603095303224e+00 3.702905546101439072e+00 2.635453659417908323e+00 2.635572456117051487e+00 2.391886453689349601e+00 2.871975783234887114e+00 3.947951548910517250e+00 2.593644960074543704e+00 3.289694675733229268e+00
1.342931901022796248e+00 2.245467917547002035e+00 0.000000000000000000e+00 1.418915551312474932e+00 2.015476882415431881e+00 2.069308813492318766e+00 2.069295883966994598e+00 2.323619373652348497e+00 3.560975969980455957e+00 4.431347320573396864e+00 3.951843753512012114e+00 2.436642114389359737e+00
2.343013632351988562e+00 2.644315603095303224e+00 1.418915551312474932e+00 0.000000000000000000e+00 1.086884605635873857e+00 1.092126176004005522e+00 1.092126228351472994e+00 3.641924623709103415e+00 4.772348473634059118e+00 5.725281935435472036e+00 4.948741644534886852e+00 3.855293676517856927e+00
3.227383136403330077e+00 3.702905546101439072e+00 2.015476882415431881e+00 1.086884605635873857e+00 0.000000000000000000e+00 1.791611832133639171e+00 1.791607398071044743e+00 4.336840746006843439e+00 5.570012200282658199e+00 6.444369626625309877e+00 5.876935928592362579e+00 4.304036910039308772e+00
2.640130058756467779e+00 2.635453659417908323e+00 2.069308813492318766e+00 1.092126176004005522e+00 1.791611832133639171e+00 0.000000000000000000e+00 1.774322615322815988e+00 3.999843247699306215e+00 5.001451201004137204e+00 5.992370839831868246e+00 5.038926795069470721e+00 4.349546588337785735e+00
2.640148435557436279e+00 2.635572456117051487e+00 2.069295883966994598e+00 1.092126228351472994e+00 1.791607398071044743e+00 1.774322615322815988e+00 0.000000000000000000e+00 3.999902964280430240e+00 5.001556219427222061e+00 5.992449776200326816e+00 5.039085741282741004e+00 4.349558376763067891e+00
1.478495377186577864e+00 2.391886453689349601e+00 2.323619373652348497e+00 3.641924623709103415e+00 4.336840746006843439e+00 3.999843247699306215e+00 3.999902964280430240e+00 0.000000000000000000e+00 1.324770443414402976e+00 2.107792016824585168e+00 2.085364895492880954e+00 1.079295724832157077e+00
2.442752671162299549e+00 2.871975783234887114e+00 3.560975969980455957e+00 4.772348473634059118e+00 5.570012200282658199e+00 5.001451201004137204e+00 5.001556219427222061e+00 1.324770443414402976e+00 0.000000000000000000e+00 1.076370750308789148e+00 1.078101361047288487e+00 2.119237286319515245e+00
3.440470104931585560e+00 3.947951548910517250e+00 4.431347320573396864e+00 5.725281935435472036e+00 6.444369626625309877e+00 5.992370839831868246e+00 5.992449776200326816e+00 2.107792016824585168e+00 1.076370750308789148e+00 0.000000000000000000e+00 1.841888017015948842e+00 2.494970001809259763e+00
2.650641510969556247e+00 2.593644960074543704e+00 3.951843753512012114e+00 4.948741644534886852e+00 5.876935928592362579e+00 5.038926795069470721e+00 5.039085741282741004e+00 2.085364895492880954e+00 1.078101361047288487e+00 1.841888017015948842e+00 0.000000000000000000e+00 3.067298402780731070e+00
2.173942147030341054e+00 3.289694675733229268e+00 2.436642114389359737e+00 3.855293676517856927e+00 4.304036910039308772e+00 4.349546588337785735e+00 4.349558376763067891e+00 1.079295724832157077e+00 2.119237286319515245e+00 2.494970001809259763e+00 3.067298402780731070e+00 0.000000000000000000e+00
原子弹
C
O
O
C
H
H
H
C
C
H
H
H
解决方案
我刚刚回答了你之前的问题。既然我完全理解了这个问题,我认为你有更好的方法来解决你的问题。
如果您要从 Atomos 包含“H”的“Matriz”中删除行。您应该将 2 个数据集合并为一个,并根据哪些行包含“H”进行过滤
编辑:更新响应以通过存储在 Atomos 中的列名反映 Matriz 中的过滤(列和行)
Matriz = pd.read_csv('test1.txt', delim_whitespace=True, header=None)
Atomos = pd.read_csv('test.txt',names=['index'])
columns = Atomos['index'].to_list()
Matriz.columns = columns
combined = pd.concat([Atomos,Matriz], axis=1)
combined[['C','O']][combined['index'] == 'H']
更新的输出
C C C C O O
4 3.227383 1.086885 4.336841 5.570012 3.702906 2.015477
5 2.640130 1.092126 3.999843 5.001451 2.635454 2.069309
6 2.640148 1.092126 3.999903 5.001556 2.635572 2.069296
9 3.440470 5.725282 2.107792 1.076371 3.947952 4.431347
10 2.650642 4.948742 2.085365 1.078101 2.593645 3.951844
11 2.173942 3.855294 1.079296 2.119237 3.289695 2.436642
这会快得多。让我知道这是如何工作的。
推荐阅读
- angular-ui-grid - 为什么 UI-Grid 选择会抛出 JS 错误
- powershell - 从 CSV 文件导入数据时在 PowerShell 中将字符串转换为布尔值
- javascript - 如何克隆一个构造函数,以便它构造一个原始类型的副本,其行为与原始类型一样,但有自己的原型?
- html - 重叠文件夹选项卡外观
- powershell - 如何将变量的内容转换为字符串?
- html - 当我将正确的数据传递给它们时,为什么我的复选框不会被选中?
- sql-server - SQL IN 通配符
- python - 如何在工作表上以另一种格式(python 日期到文本)显示日期值
- r - R 3.5.2 版本中的 termtrc
- c++ - 限制#include的范围