我目前正在编写一个脚本来帮助对csv文件中的项目进行排序。正如下面的代码所示,我使用panda来读取csv文件中的两个特定列,然后将读取的数据转换为列表。我试图完成的工作(以一种简单但不充分的方式)是解析这两个匹配的列表,如下图所示:
Result of using panda to read in two specific columns of csv file. Note: that on the right column labeled Responsible are department names listed and on the right column labeled Status are status on order put into the given department.
下面是我用来解析这两个列表的代码的副本:
import pandas as pd
PHSc = 0
PHSi = 0
PHS = 0
MOB = 0
MOBc = 0
MOBi = 0
PHR = 0
PHRc = 0
PHRi = 0
SYH = 0
SYHc = 0
SYHi = 0
APS = 0
APSc = 0
APSi = 0
ANA = 0
ANAc = 0
ANAi = 0
CKC = 0
CKCc = 0
CKCi = 0
RLG = 0
RLGc = 0
RLGi = 0
DJ = 0
DJc = 0
DJi = 0
EMS = 0
EMSc = 0
EMSi = 0
CTS = 0
CTSc = 0
CTSi = 0
ROT = 0
ROTc = 0
ROTi = 0
SW = 0
SWc = 0
SWi = 0
NU = 0
NUc = 0
NUi = 0
RON = 0
RONc = 0
RONi = 0
MON = 0
MONc = 0
MONi = 0
TJS = 0
TJSc = 0
TJSi = 0
VCH = 0
VCHc = 0
VCHi = 0
NLN = 0
NLNc = 0
NLNi = 0
KND = 0
KNDc = 0
KNDi = 0
SHE = 0
SHEc = 0
SHEi = 0
DOS = 0
DOSc = 0
DOSi = 0
E1 = 0
E1c = 0
E1i = 0
MOC = 0
MOCc = 0
MOCi = 0
AC = 0
ACc = 0
ACi = 0
SL = 0
SLc = 0
SLi = 0
JER = 0
JERc = 0
JERi = 0
BLP = 0
BLPc = 0
BLPi = 0
file = input("Please enter the name of the file (include .csv): ")
data = pd.read_csv(file, usecols = \[23, 26\], skiprows = 1, names=\['Responsible', 'Status'\])
print(data)
responsible = data\['Responsible'\].tolist()
print(responsible)
status = data\['Status'\].tolist()
print(status)
for i in responsible:
for j in status:
if i == 'PHS' and j == 'Comp':
PHSc += 1
PHS += 1
break
elif i == 'PHS' and j == 'Active':
PHSi += 1
PHS += 1
break
elif i == 'MOB' and j == 'Comp':
MOBc = MOBc + 1
MOB += 1
break
elif i == 'MOB' and j == 'Active':
MOBi = MOBi + 1
MOB += 1
break
elif i == 'PHR' and j == 'Comp':
PHRc = PHRc + 1
PHR += 1
break
elif i == 'PHR' and j == 'Active':
PHRi = PHRi + 1
PHR += 1
break
elif i == 'SYH' and j == 'Comp':
SYHc = SYHc + 1
SYH += 1
break
elif i == 'SYH' and j == 'Active':
SYHi = SYHi + 1
SYH += 1
break
elif i == 'APS' and j == 'Comp':
APSc = APSc + 1
APS += 1
break
elif i == 'APS' and j == 'Active':
APSi = APSi + 1
APS += 1
break
elif i == 'ANA' and j == 'Comp':
ANAc = ANAc + 1
ANA += 1
break
elif i == 'ANA' and j == 'Active':
ANAi = ANAi + 1
ANA += 1
break
elif i == 'CKC' and j == 'Comp':
CKCc = CKCc + 1
CKC += 1
break
elif i == 'CKC' and j == 'Active':
CKCi = CKCi + 1
CKC += 1
break
elif i == 'RLG' and j == 'Comp':
RLGc = RLGc + 1
RLG += 1
break
elif i == 'RLG' and j == 'Active':
RLGi = RLGi + 1
RLG += 1
break
elif i == 'DJ' and j == 'Comp':
DJc = DJc + 1
DJ += 1
break
elif i == 'DJ' and j == 'Active':
DJi = DJi + 1
DJ += 1
break
elif i == 'EMS' and j == 'Comp':
EMSc = EMSc + 1
EMS += 1
break
elif i == 'EMS' and j == 'Active':
EMSi = EMSi + 1
EMS += 1
break
elif i == 'CTS' and j == 'Comp':
CTSc = CTSc + 1
CTS += 1
break
elif i == 'CTS' and j == 'Active':
CTSi = CTSi + 1
CTS += 1
break
elif i == 'ROT' and j == 'Comp':
ROTc = ROTc + 1
ROT += 1
break
elif i == 'ROT' and j == 'Active':
ROTi = ROTi + 1
ROT += 1
break
elif i == 'SW' and j == 'Comp':
SWc = SWc + 1
SW += 1
break
elif i == 'SW' and j == 'Active':
SWi = SWi + 1
SW += 1
break
elif i == 'NU' and j == 'Comp':
NUc = NUc + 1
NU += 1
break
elif i == 'NU' and j == 'Active':
NUi = NUi + 1
NU += 1
break
elif i == 'RON' and j == 'Comp':
RONc = RONc + 1
RON += 1
break
elif i == 'RON' and j == 'Active':
RONi = RONi + 1
RON += 1
break
elif i == 'MON' and j == 'Comp':
MONc = MONc + 1
MON += 1
break
elif i == 'MON' and j == 'Active':
MONi = MONi + 1
MON += 1
break
elif i == 'TJS' and j == 'Comp':
TJSc = TJSc + 1
TJS += 1
break
elif i == 'TJS' and j == 'Active':
TJSi = TJSi + 1
TJS += 1
break
elif i == 'VCH' and j == 'Comp':
VCHc = VCHc + 1
VCH += 1
break
elif i == 'VCH' and j == 'Active':
VCHi = VCHi + 1
VCH += 1
break
elif i == 'NLN' and j == 'Comp':
NLNc = NLNc + 1
NLN += 1
break
elif i == 'NLN' and j == 'Active':
NLNi = NLNi + 1
NLN += 1
break
elif i == 'KND' and j == 'Comp':
KNDc = KNDc + 1
KND += 1
break
elif i == 'KND' and j == 'Active':
KNDi = KNDi + 1
KND += 1
break
elif i == 'SHE' and j == 'Comp':
SHEc = SHEc + 1
SHE += 1
break
elif i == 'SHE' and j == 'Active':
SHEi = SHEi + 1
SHE += 1
break
elif i == 'DOS' and j == 'Comp':
DOSc = DOSc + 1
DOS += 1
break
elif i == 'DOS' and j == 'Active':
DOSi = DOSi + 1
DOS += 1
break
elif i == 'E1' and j == 'Comp':
E1c = E1c + 1
E1 += 1
break
elif i == 'E1' and j == 'Active':
E1i = E1i + 1
E1 += 1
break
elif i == 'MOC' and j == 'Comp':
MOCc = MOCc + 1
MOC += 1
break
elif i == 'MOC' and j == 'Active':
MOCi = MOCi + 1
MOC += 1
break
elif i == 'AC' and j == 'Comp':
ACc = ACc + 1
AC += 1
break
elif i == 'AC' and j == 'Active':
ACi = ACi + 1
AC += 1
break
elif i == 'SL' and j == 'Comp':
SLc = SLc + 1
SL += 1
break
elif i == 'SL' and j == 'Active':
SLi = SLi + 1
SL += 1
break
elif i == 'JER' and j == 'Comp':
JERc = JERc + 1
JER += 1
break
elif i == 'JER' and j == 'Active':
JERi = JERi + 1
JER += 1
break
elif i == 'BLP' and j == 'Comp':
BLPc = BLPc + 1
BLP += 1
break
elif i == 'BLP' and j == 'Active':
BLPi = BLPi + 1
BLP += 1
break
print("\\nPHS Complete: ", PHSc)
print("PHS Incomplete: ", PHSi)
print("PHS Total: ", PHS)
print("\\nMOB Complete: ", MOBc)
print("MOB Incomplete: ", MOBi)
print("MOB Total: ", MOB)
print("\\nPHR Complete: ", PHRc)
print("PHR Incomplete: ", PHRi)
print("PHR Total: ", PHR)
print("\\nSYH Complete: ", SYHc)
print("SYH Incomplete: ", SYHi)
print("SYH Total: ", SYH)
print("\\nAPS Complete: ", APSc)
print("APS Incomplete: ", APSi)
print("APS Total: ", APS)
print("\\nANA Complete: ", ANAc)
print("ANA Incomplete: ", ANAi)
print("ANA Total: ", ANA)
Code Output
可以看出,PHS Complete的值应该是49,PHS Incomplete的值应该是0,因为PHS部门完成了该给定日报表的所有必需工作。因此,我不确定我在这里做错了什么。其他print语句也发生了同样的事情。其思想是解析这两个列表,并在每次部门(例如PHS)的状态为comp。每次一个部门的状态为Active时,都执行相同的操作并递增一个不同的索引值。第三个值会递增,以查找给定日期内执行的作业总数。我确信有更有效的方法来完成此操作,但我是Python新手,并且有坚实的C++背景。任何和所有的建议将不胜感激。提前感谢!
1条答案
按热度按时间n8ghc7c11#
您可以获取按Responsible和Status列分组的计数,如下所示:
这看起来像:
由于您没有提供可以复制和粘贴的示例数据,因此您的计数当然会有所不同。
您还可以使用以下命令仅获取Responsible列中每个条目的计数:
要将所有状态放在一起,并将每种状态的计数加上两种状态的总计,您可以执行以下操作:
这看起来像: