# I want to delete rows in a two-dimensional array in Python under any conditions.

Asked 2 months ago, Updated 2 months ago, 2 views

I would like to remove the line containing "Yokohama" from the 2D array below.

``````arr=[
['AA', 'BB', 'CC',]
['Lemon', '30', 'Tokyo',]
['Lemon', '20', 'Osaka'],
['Lemon', '10', 'Yokohama',]
["Peach", "15", "Tokyo"]
]
``````

I tried to delete it in np.delete, but it didn't work.
I will use it for processing about 10 million lines, so I would appreciate it if you could tell me how to be strong with the amount of data.

python3 numpy

2022-09-30 14:03

Based on the second code of the question in this article Selecting rows from a NumPyndarray, can you write as follows? Can you support up to 10 million lines? is unknown.

``````arr=arr[arr[:,2]!='Yokohama']
``````

Note:

If I wanted only rows where the second value is 4, it is easy:

``````>>test [test[:, 1]==4]
array([3,4],
[ 7,  4],
[16,  4],
...
[81,  4],
[83,  4],
[88,  4]])
``````

To sum up, it is as follows:

``````import numpy as np

arr=np.array(['AA', 'BB', 'CC'],
['Lemon', '30', 'Tokyo',]
['Lemon', '20', 'Osaka'],
['Lemon', '10', 'Yokohama',]
['Peach', '15', 'Tokyo'])

arr=arr[arr[:,2]!='Yokohama']

print(arr)

# Below is the display result.
# [['AA' 'BB' 'CC']
# ['Lemon' '30' 'Tokyo']
# ['Lemon' 20' Osaka']
# ['Peach' '15' 'Tokyo']
``````

2022-09-30 14:03

## If you have any answers or tips

Popular Tags
python x 4439
android x 1590
java x 1475
javascript x 1386
c x 903
c++ x 832
ruby-on-rails x 681
php x 678
python3 x 651
html x 631