pandas 从numpy数组中删除一个元素
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/50733987/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Delete an element from numpy array
提问by Piyush S. Wanare
I am working with numpy array
as follows:
我的工作numpy array
如下:
input_series = ['BUY' 'SELL' 'BUY' 'SELL' 'BUY' 'SELL' 'SELL' 'SELL' 'BUY' 'SELL' nan nan
nan nan nan nan nan nan nan]
I am searching for particular values in array and if element exist then delete
我正在搜索数组中的特定值,如果元素存在则删除
I have done this as follows:
我这样做了如下:
delete_indices = list()
val = ['BUY','SELL','No','YES']
found_index = np.where(lowercase_series_nparray == val)
delete_indices.append(found_index)
delete_indices
getting as follows:
delete_indices
得到如下:
[(array([], dtype=int64),), (array([], dtype=int64),), (array([], dtype=int64),), (array([], dtype=int64),), (array([], dtype=int64),), (array([], dtype=int64),), (array([], dtype=int64),), (array([], dtype=int64),), (array([], dtype=int64),), (array([], dtype=int64),), (array([], dtype=int64),), (array([], dtype=int64),), (array([], dtype=int64),), (array([], dtype=int64),), (array([], dtype=int64),), (array([0, 2, 4, 8], dtype=int64),), (array([1, 3, 5, 6, 7, 9], dtype=int64),), (array([], dtype=int64),), (array([], dtype=int64),), (array([], dtype=int64),)]
Aftre that I am trying to delete with :
在我尝试删除之后:
new_output_series = numpy.delete(input_series, delete_indices)
But getting error as setting an array element with a sequence.
但是得到错误 setting an array element with a sequence.
回答by Joe
If from an array like this:
如果来自这样的数组:
input_series = np.array(['BUY', 'a', 'b', 'SELL', 'YES', 'SELL', 'No', 'c', 'd', 'SELL'])
you want to remove these elements:
你想删除这些元素:
['BUY','SELL','No','YES']
Just set these as an array:
只需将这些设置为数组:
val = np.array(['BUY','SELL','No','YES'])
and then:
进而:
new_output_series = np.setdiff1d(input_series,val)
Output:
输出:
['a' 'b' 'c' 'd']
回答by Nikhil Verma
Below statement gives you the indexes you need:
下面的语句为您提供了所需的索引:
found_index = np.in1d(input_series, val).nonzero()[0]
and then:
进而:
new_array = numpy.delete(input_series, found_index)