【发布时间】:2020-07-18 09:21:13
【问题描述】:
我正在做一个项目,需要我计算一个学生在场的次数和他在不同学科课程中缺席的次数,并计算他的出勤率。我有他的出勤记录如下
Attend Date Subject
96 Present 09-04-2020 AM-II
69 Present 16-04-2020 AM-II
61 Present 20-04-2020 AM-II
49 Present 22-04-2020 AM-II
45 Present 23-04-2020 AM-II
... ... ... ...
14 Present 12-04-2020 LMS
13 Absent 18-04-2020 LMS
11 Absent 19-04-2020 LMS
10 Present 25-04-2020 LMS
9 Present 26-04-2020 LMS
我正在使用 python 的 pandas 库来计算每个唯一主题出现“出席”的次数和“缺席”的次数,但我无法这样做。这就是我正在做的事情。
data=pd.read_csv("data1.csv")
#sorting data frame by Team and then By names
data.sort_values(["Subject", "Date"], axis=0,
ascending=True, inplace=True)
p = 0
a = 0
total = 0
attpercent = {}
data.set_index(["Subject"], inplace = True,
append = True, drop = False)
temp = ""
data = data.infer_objects()
for Subject, Attend in data.iterrows()
if(temp == ""):
temp = Subject
if Attend == "Present":
p = p + 1
else:
a = a + 1
else:
if(temp == Subject):
if Attend == "Present":
p = p + 1
else:
a = a + 1
else:
total = a + p
attpercent[temp] = (p * 100) / total
a = 0
p = 0
temp = Subject
if Attend == "Present":
p = p + 1
else:
a = a + 1
print(attpercent)
显示错误:
TypeError Traceback (most recent call last)
<ipython-input-65-9d7243427e5f> in <module>
18 data = data.infer_objects()
19 for Subject, Attend in data.iterrows():
---> 20 Attend = str(Attend)
21 if(temp == ""):
22 temp = Subject
TypeError: 'Series' object is not callable
我是第一次使用 pandas,所以对它了解不多。我尝试使用infer_objects 和astypes() 转换列的类型,但仍然出现相同的错误。请帮忙。
【问题讨论】:
-
Iterrows() 不返回列它将返回行,您应该使用
for index, row in data.iterrow()tehn 您可以通过row[Attend]和row['Subject']访问列值
标签: python pandas numpy dataframe import-from-excel