简体   繁体   English

pandas dataframe 如何从基于其他列的列表单元格中删除值

[英]pandas dataframe how to remove values from cell that is a list based on other column

I have a dataframe with 2 columns that represent a list:我有一个 dataframe,其中 2 列代表一个列表:

a. b.  vals.        locs
1. 2. [1,2,3,4,5].  [2,3]
5  1. [1,7,2,4,9].  [0,1]
8. 2. [1,9,4,7,8].  [3]

I want, for each row, exclude from the columns vals all the locations that are in locs.我希望对于每一行,从 vals 列中排除 locs 中的所有位置。 so I will get:所以我会得到:

a. b.  vals.        locs.   new_vals
1. 2. [1,2,3,4,5].  [2,3].  [1,2,5]
5  1. [1,7,2,4,9].  [0,1].  [2,4,9]
8. 2. [1,9,4,7,8].  [3].    [1,9,4,8]

What is the best way to do so?最好的方法是什么?

Thanks!谢谢!

You can use a list comprehension with an internal filter based on enumerate :您可以将列表理解与基于enumerate的内部过滤器结合使用:

df['new_vals'] = [[v for i,v in enumerate(a) if i not in b]
                  for a,b in zip(df['vals'], df['locs'])]

however this will become quickly inefficient when b get large.然而,当 b 变大时,这将很快变得低效。

A much better approach would be to use python sets that enable a fast (O(1) complexity) identification of membership:一个更好的方法是使用python 集来实现快速(O(1) 复杂度)成员身份识别:

df['new_vals'] = [[v for i,v in enumerate(a) if i not in S]
                  for a,b in zip(df['vals'], df['locs']) for S in [set(b)]]

output: output:

   a  b             vals    locs      new_vals
0  1  2  [1, 2, 3, 4, 5]  [2, 3]     [1, 2, 5]
1  5  1  [1, 7, 2, 4, 9]  [0, 1]     [2, 4, 9]
2  8  2  [1, 9, 4, 7, 8]     [3]  [1, 9, 4, 8]

Use list comprehension with enumerate and converting values to set s:将列表理解与enumerate和转换值一起使用以set s:

df['new_vals'] = [[z for i, z in enumerate(x) if i not in y]
                              for x, y in zip(df['vals'], df['locs'].apply(set))]
print (df)
   a  b             vals    locs      new_vals
0  1  2  [1, 2, 3, 4, 5]  [2, 3]     [1, 2, 5]
1  5  1  [1, 7, 2, 4, 9]  [0, 1]     [2, 4, 9]
2  8  2  [1, 9, 4, 7, 8]     [3]  [1, 9, 4, 8]

One way to do this is to create a function that works on row,一种方法是创建一个适用于行的 function,

def func(row):
    ans = [v for v in row['vals'] if row['vals'].index(v) not in row['locs']]
    return ans

The call this function for each row using apply.使用 apply 为每一行调用此 function。

df['new_value'] = df.apply(func, axis=1)

This will work well, if the lists are short.如果列表很短,这会很有效。

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

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