简体   繁体   English

Pandas 中的两个列表比较

[英]Two list compare in Pandas

I have two list, first one with names only and second one with names and values corresponding to names.我有两个列表,第一个只有名字,第二个有名字和与名字对应的值。 What I am trying to achieve is to find in second list all names that presented in first list with its values in second one.我想要实现的是在第二个列表中找到第一个列表中显示的所有名称,其值在第二个列表中。 I tried this way, but missing the corresponding numbers我试过这种方式,但缺少相应的数字

matches = []
for i in list(first):
    if i in list(second]):
       matches.append(i)

Plz Help请帮助

if it has names and values corresponding to names - it is not a list, but dict or pd.DataFrame如果它具有与名称对应的名称和值 - 它不是列表,而是 dict 或 pd.DataFrame

for pd.DataFrame:对于 pd.DataFrame:

matches = list2[list2.name.isin (list1)]

for dict:对于字典:

matches = {}
for i, v in dict2.items():
    if i in list1:
       matches[i] = v

or just matches = {i:v for i in dict2 if i in list1}或者只matches = {i:v for i in dict2 if i in list1}

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

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