python - 如何在数据框中满足条件的情况下检索列名和行名?
问题描述
如果列的总和为 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})
解决方案
有了熊猫,你可以做到
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
推荐阅读
- swift - SwiftUI:为表单中的单元格设置动画
- pkcs#11 - 如何通过 SUNPKCS 提供商从 hsm 获取 al 别名?
- python - 与 VISA 的设备通信在 Python 中工作,但在 C++ 中不工作
- apk - 安装前如何知道apk版本?
- python - 嗨,当我运行以下代码时,我的计数为 0,为什么,我希望它显示文件中的行数
- php - 我想生成一个常规数组
- tensorflow - 使用 tensorflow 2.0 中的 tf.summary 保存非急切张量
- azure - Azure IoT 远程监控 Web UI 未在本地计算机上启动 - 未经授权
- php - 如何通过 HTML 表单传递 MediaRecorder 录制的视频
- sql - 重新构建查询以使用具有相似数据但之间没有每个日期的表