我尝试将分配分数的字符串转换为int,但一直说不能转换。我在分配作业和考试成绩时遇到问题。错误:无法将字符串和整数相乘
"""
Student ID Assignment Name Score
abcd1234 assignment 1 76.52
efgh5678 exam 1 67.22
abcd1234 assignment 2 88.46
...
etc.
"""
mapping = {
'exam 1' : 'Exam 1',
'test 1' : 'Exam 1',
'exam 2' : 'Exam 2',
'test #2' : 'Exam 2',
'assignment 1' : 'Assignment 1',
'homework 1' : 'Assignment 1',
'first assignment' : 'Assignment 1',
'assignment 2' : 'Assignment 2',
'assignment #2' : 'Assignment 2',
'HW 2' : 'Assignment 2',
}
def map_value(value):
return mapping[value] if value in mapping else value
x = lambda value: mapping[value] if value in mapping else value
df.loc[:, 'Assignment Name'] = df['Assignment Name'].apply(x)
pivot = df.pivot(index='Student ID', columns='Assignment Name', values='Score')
print (pivot)
这是print(pivot)数据的样子:
"""
Assignment Name Assignment 1 Assignment 2 Exam 1 Exam 2
Student ID
abcd1234 76.52 79.23 88.46 87.31
...
"""
Student ID Assignment 1 Exam 1 Assignment 2 Exam 2
abcd1234 76.52 79.23 88.46 87.31
efgh5678 86.11 67.22 75.45 77.20
...
etc.
"""
"""
Student ID Assignment 1 Exam 1 Assignment 2 Exam 2 Final Score
abcd1234 76.52 79.23 88.46 87.31 82.96
efgh5678 86.11 67.22 75.45 77.20 75.64
...
etc.
"""
谢谢