简体   繁体   English

将两个列表列表与 dataframe 列进行比较 python

[英]comparing two list of lists with a dataframe column python

I want to compare two list of lists with a dataframe column.我想将两个列表列表与 dataframe 列进行比较。
list1=[[r2,r4,r6],[r6,r7]]
list2=[[p4,p5,p8],[p86,p21,p0,p94]]

Dataset:数据集:

rid摆脱 pid进程号 value价值
r2 r2 p0 p0 banana香蕉
r2 r2 p4 p4 chocolate巧克力
r4 r4 p89 p89 apple苹果
r6 r6 p5 p5 milk牛奶
r7 r7 p0 p0 bread面包

Output: Output:

[[chocolate,milk],[bread]]

As r2 and p4 occur in the list1[0] , list2[0] and in the same row in dataset, so chocolate must be stored.由于r2p4出现在list1[0]list2[0]和数据集中的同一行中,因此必须存储chocolate Similarly r6 and p5 occur in both lists at same position and in the same row in dataset, milk must be stored.类似地, r6p5出现在两个列表中的相同 position 和数据集中的同一行中,必须存储milk

You can do it as follows:您可以按如下方式进行:

from itertools import product

df = pd.DataFrame({'rid': {0: 'r2', 1: 'r2', 2: 'r4', 3: 'r6', 4: 'r7'},
 'pid': {0: 'p0', 1: 'p4', 2: 'p89', 3: 'p5', 4: 'p0'},
 'value': {0: 'banana', 1: 'chocolate', 2: 'apple', 3: 'milk', 4: 'bread'}})
list1 = [['r2','r4','r6'],['r6','r7']]
list2 = [['p4','p5','p8'],['p86','p21','p0','p94']]

# Generate all possible associations.
associations = (product(l1, l2) for l1, l2 in zip(list1, list2))

# Index for speed and convenience of the lookup.
df = df.set_index(['rid', 'pid']).sort_index()

output = [[df.loc[assoc, 'value'] for assoc in assoc_list if assoc in df.index] 
          for assoc_list in associations]

print(output)
[['chocolate', 'milk'], ['bread']]

Answer回答

result = []
for l1, l2 in zip(list1, list2):
    res = df.loc[df["rid"].isin(l1) & df["pid"].isin(l2)]["value"].tolist()
    result.append(res)
[['chocolate', 'milk'], ['bread']]

Explain解释

  • zip will combine the two lists, equivalent to zip将合并两个列表,相当于
for i in range(len(list1)):
    l1 = list1[i]
    l2 = list2[i]
  • df["rid"].isin(l1) & df["pid"].isin(l2) will combine the condition with and operator & df["rid"].isin(l1) & df["pid"].isin(l2)将条件与and operator &组合

Attation署名

  • The length of list1 and list2 must be equal, otherwise, zip will ignore the rest element of the longer list. list1 和 list2 的长度必须相等,否则zip将忽略较长列表的 rest 元素。

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

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