[英]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 ![]() |
p0 ![]() |
banana![]() |
r2 ![]() |
p4 ![]() |
chocolate![]() |
r4 ![]() |
p89 ![]() |
apple![]() |
r6 ![]() |
p5 ![]() |
milk![]() |
r7 ![]() |
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.由于
r2
和p4
出现在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.类似地,
r6
和p5
出现在两个列表中的相同 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']]
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']]
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
&
组合zip
will ignore the rest element of the longer list. zip
将忽略较长列表的 rest 元素。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.