简体   繁体   English

根据列表从数据框中提取值作为字典

[英]Extracting values as a dictionary from dataframe based on list

I have a dataframe with unique value in each columns: 我在每一列中都有一个具有唯一值的数据框:

df1 = pd.DataFrame([["Phys","Shane","NY"],["Chem","Mark","LA"],
                    ["Maths","Jack","Mum"],["Bio","Sam","CT"]],
                    columns = ["cls1","cls2","cls3"])
print(df1)

    cls1    cls2    cls3
0   Phys    Shane   NY
1   Chem    Mark    LA
2   Maths   Jack    Mum
3   Bio     Sam     CT

And a list l1: 和清单l1:

l1=["Maths","Bio","Shane","Mark"]
print(l1)

['Maths', 'Bio', 'Shane', 'Mark']

Now I want to retrieve a columns from dataframe that contains elements from list and list of elements. 现在,我想从数据框中检索包含列和元素列表中的元素的列。

Expected Output : 预期产量

{'cls1' : ['Maths','Bio'], 'cls2': ['Shane','Mark']}

The code I have : 我有的代码

cls = []
for cols in df1.columns:
    mask = df1[cols].isin(l1)
    if mask.any():
        cls.append(cols)
print(cls)

The output of above code : 上面代码的输出

['cls1', 'cls2']

I'm struggling to get common elements from dataframe and list to convert it into dictionary. 我正在努力从数据框和列表中获取通用元素,以将其转换为字典。

Any suggestions are welcome. 欢迎任何建议。

Thanks. 谢谢。

Use DataFrame.isin for mask, replace non match values by indexing and reshape with stack : 使用DataFrame.isin的面具,通过索引替代非匹配值,并与重塑stack

df = df1[df1.isin(l1)].stack()
print (df)
0  cls2    Shane
1  cls2     Mark
2  cls1    Maths
3  cls1      Bio
dtype: object

Last create list by dict comprehension : 通过dict comprehension最后创建列表:

d = {k:v.tolist() for k,v in df.groupby(level=1)}
print(d)
{'cls2': ['Shane', 'Mark'], 'cls1': ['Maths', 'Bio']}

Another solution: 另一个解决方案:

d = {}
for cols in df1.columns:
    mask = df1[cols].isin(l1)
    if mask.any():
        d[cols] = df1.loc[mask, cols].tolist()

print(d)
{'cls2': ['Shane', 'Mark'], 'cls1': ['Maths', 'Bio']}

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

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