简体   繁体   English

在列表中拆分基于 numpy 数组的列值

[英]Split numpy array based column value in list

I'am new in numpy and I want to split array 2D based on columns values if value in another list, I converted a pandas dataframe on numpy array of 2D and I have a another list, I want to split my numpy array on two others array, the first based on (if values of second column in list) and the second contains the rest of my numpy array, and I want to get the rest of my list(contains all values doesn't exist in my numpy array)我是 numpy 的新手,如果值在另一个列表中,我想根据列值拆分数组 2D,我在 numpy 2D 数组上转换了一个 Pandas 数据框,我有另一个列表,我想将我的 numpy 数组拆分为另外两个数组,第一个基于(如果列表中第二列的值),第二个包含我的 numpy 数组的其余部分,我想获取我的列表的其余部分(包含我的 numpy 数组中不存在的所有值)

numpy_data = np.array([
        [1, 'p1', 2],
        [11, 'p2', 8],
        [1, 'p8', 21],
        [13, 'p10', 2] ])

list_value = ['p1', 'p3', 'p8']

The expected output :预期输出:

data_in_list = [
        [1, 'p1', 2],
        [1, 'p8', 21]]
list_val_in_numpy = ['p1', 'p8'] # intersection of second column with my list

rest_data = [
        [11, 'p2', 8],
        [13, 'p10', 2]] 
rest_list_value = ['p3']

In my code I have found how to get first output :在我的代码中,我找到了如何获得第一个输出:

first_output =  numpy_data[np.isin(numpy_data[:,1], list_value)]    

But I couldn't find the rest of my numpy, I have tried too, Browse my list and seek if values in second column of array and then delete this row, in this case I dont need the first output (That I called data_in_list, b-coz I do what I need on it), here I need the others output但是我找不到 numpy 的其余部分,我也尝试过,浏览我的列表并查找数组第二列中的值,然后删除这一行,在这种情况下,我不需要第一个输出(我称之为 data_in_list, b-coz 我做我需要的),这里我需要其他输出

for val in l :
    row = numpy_data[np.where(numpy_data[:,1]== val)]
    row.size != 0 :
        # My custom code
        # then remove this row from my numpy, I couldn't do it

Thanks in advance提前致谢

np.isin的结果上使用 python 的 invert ~运算符:

rest = numpy_data[~np.isin(numpy_data[:,1], list_value)]    

There are multiple ways of doing this.有多种方法可以做到这一点。 I would prefer a vectorized way of using list comprehension.我更喜欢使用列表理解的矢量化方式。 But for sake of clarity here is loop way of doing the same thing.但为了清楚起见,这里是做同样事情的循环方式。

data_in_list=[]
list_val_in_numpy = []
rest_data=[]
for x in numpy_data:
    for y in x:
        if y in list_value:
            data_in_list.append(x)
            for x in list_value:
                if x == y:
                    list_val_in_numpy.append(x)
for x in numpy_data:
    if x in data_in_list:
        pass
    else:
        rest_data.append(x)

This gives you all the three lists you were looking for.这为您提供了您正在寻找的所有三个列表。 Concatenate to get the list you want exactly.连接以获得您想要的列表。

list comprehension will solve it I guess:我猜列表理解将解决它:

numpy_data = [
        [1, 'p1', 2],
        [11, 'p2', 8],
        [1, 'p8', 21],
        [13, 'p10', 2],

]

list_value = ['p1', 'p3', 'p8']

output_list = [[item] for item in numpy_data if item[1] in list_value]
print(output_list)

output:输出:

[[[1, 'p1', 2]], [[1, 'p8', 21]]]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM