Pretty new to Python.
(Python的新手。)
I'm trying to create a function which should look at a csv file, with an ID number, Name, and then N collumns of numbers from different tests and then scale/round the numbers so they can be compared to the Danish grading system from [-3, 00, 02, 4, 7, 10, 12]. (我正在尝试创建一个函数,该函数应查看一个csv文件,该文件具有ID号,名称,然后是来自不同测试的N个数字列,然后缩放/四舍五入这些数字,以便可以将它们与丹麦的评分系统进行比较[-3,00,02,4,4,7,10,12]。)
My script below does exactly that, but my function only returns the last result of the DF. (我下面的脚本正是这样做的,但是我的函数仅返回DF的最后结果。)
Heres the CSV i use for testing:
(这是我用于测试的CSV文件:)
StudentID,Name,Assignment1,Assignment2,Assignment3
s123456,Michael Andersen,7,5,4
s123789,Bettina Petersen,12,3,10
s123468,Thomas Nielsen,-3,7,2
s123579,Marie Hansen,10,12,12
s123579,Marie Hansen,10,12,12
s127848, Andreas Nielsen,2,2,2
s120799, Mads Westergaard,12,12,10
Its worth to mention that i need these functions separate, for my main script.
(值得一提的是,对于我的主脚本,我需要将这些功能分开。)
I've made a simple function which loads the file using pandas: (我做了一个简单的函数,该函数使用pandas加载文件:)
import pandas as pd
def dataLoad(filename):
grades = pd.read_csv(filename)
return grades
then i've made this script for the rounding of the numbers:
(然后我就用这个脚本对数字进行了四舍五入:)
# Importing modules
import pandas as pd
import numpy as np
#Loading in the function dataLoad
from dataLoad import dataLoad
#Defining my data witht the function
grades=dataLoad('Karakterer.csv')
def roundGrade(grades):
#Dropping the two first collumns of the pd.DF
grades=grades.drop(['StudentID','Name'],axis=1)
#Making the pd.DF into a numpy array
sample_grades=np.array(grades)
#Setting the parameters of the scale to round up to
grade_Scale = np.array([-3,0,2,4,7,10,12])
#Defining i, so i get gradually bigger with each cycle
i=0
#Making a for loop, which rounds every number in every row of the given array
for i in range(0,len(grades)):
grouped = [min(grade_Scale,key=lambda x:abs(grade-x)) for grade in sample_grades[i,:]]
#Making i 1 time bigger for each cycle
i=i+1
return grouped
Tell if you need some more information about the script, cheers guys!
(告诉大家您是否需要有关脚本的更多信息,干杯!)
ask by Mads Westergaard translate from so