Python: how to delete iteratively rows and columns from a matrix?

Viewed 178

I am a beginner, self-taught.

I want to delete row and column iteratively for a square matrix.

My first attempt was:

for i in range(0,c):
    if Atomos[i] == "H":
        d_nh=np.delete(np.delete(d, i, 0), i, 1)

My source

But the final and new matrix show that only one row and one column were deleted.

How can I remove row and column iteratively?

The content of both files is:

Matriz

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

Atomo

C
O
O
C
H
H
H
C
C
H
H
H
2 Answers

I just answered your previous question. I think you have a much better way of solving your problem now that I understand the question fully.

If you are looking to remove rows from 'Matriz' where Atomos contains 'H'. You should combine your 2 datasets into one and filter based on which rows contain 'H'

EDIT: Updated response to reflect filtering (columns and rows) in Matriz by the column names stored in Atomos

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']

Updated output


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

This will be much faster. Let me know how this works.

np.ix_ creates a selector which retrieves only the rows and columns of d for which the value of the list s is True. The first s in np.ix_(s,s) specifies which rows should be selected, and the second s specifies columns.

s = [x!= "H" for x in Atomos]
d_nh = d[np.ix_(s,s)]
Related