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

Asked 2 years ago, Updated 2 years ago, 52 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.
Thank you for your cooperation.

python3 numpy

2022-09-30 14:03

1 Answers

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


© 2024 OneMinuteCode. All rights reserved.