首页 > 解决方案 > 如何在数据框中满足条件的情况下检索列名和行名?

问题描述

如果列的总和为 1,并且如果满足我想检索字典中的列名和行号,我需要检查一个条件。 在此处输入图像描述

输出应该是list1=({8:1004},{9:1001}).

我尝试了一些 python 代码,但无法继续使用这些代码。

list1=[]
for Emp in SkillsA:
        sum_row = (SkillsA.sum(axis=0))
#print(sum_row)
        # print((Skills_A[0]))
        if sum_row[Emp] == 1:
            #print(Emp)
            for ws in SkillsA:
#                if SkillsA[ws][Emp] == 1:
                    print(SkillsA[ws][Emp])
                    #list1.update({Emp:ws})

标签: pythonpandas

解决方案


有了熊猫,你可以做到

import pandas as pd

#   Import data
df = pd.read_excel("location_file")

#   Create a dictionary
dict = dict()

#   Iterate in columns
for i in df.columns:
        if df[i].sum() == 1:   
            dict[i] = df.Employee_No[df[i] == 1] # Add filter data to dict

推荐阅读