1

I am building a program that prompts the user to load a csv file. The csv file always contains the columns StudentID, Name, Assignment1,2,3... The number of assignments varies.

I am trying to display a list of names, with one grade for each assignment and the final grade which I calculate from my imported function computeFinalGrade.

How can I change the last line my code to print the grades and the final grades for the right number of assignments?

Desired output: if the csv only has "Assignment1", the output should be:

"Michael Andersen has obtained the grades 10. The final grade is 10"

if the csv contains 5 assignments, the output should be:

"Michael Andersen has obtained the grades 10,7,12,7,10. The final grade is 10"

My code:

grades = np.genfromtxt(filename, delimiter=";", skip_header=1)
e = grades[:,2:]
f = computeFinalGrades(e)
df = pd.read_csv(filename,sep=';')
df['FinalGrade'] = f
for index, row in df.iterrows():
    print('{} has obtained the grades {}, {}, {}. The final grade is {}'.format(row['Name'],row['Assignment1'],row['Assignment2'],row['Assignment3'],row['FinalGrade']))

1 Answer 1

1

Use:

df = pd.read_csv(filename,sep=';')
#filter DataFrame by positions
df1 = df.iloc[:,2:]
#count computeFinalGrades
f = computeFinalGrades(df1.values)

#all Assignments convert to joined string
df['Assignment'] = df1.astype(str).apply(', '.join, axis=1)
df['FinalGrade'] = f

#zip columns together and loop
for name, assign, final in zip(df['Name'],df['Assignment'],df['FinalGrade']):
    #python 3.6+ f-strings
    print(f'{name} has obtained the grades {assign}. The final grade is {final}')
    #python bellow with format
    print('{} has obtained the grades {}. The final grade is {}'.format(name, assign, final)) 

Sample:

df = pd.DataFrame({
        'Name':list('abcd'),
         'StudentID':[7,8,9,4],
         'Assignment1':[1,3,5,7],
         'Assignment2':[5,3,6,9],

})

print (df)
  Name  StudentID  Assignment1  Assignment2
0    a          7            1            5
1    b          8            3            3
2    c          9            5            6
3    d          4            7            9

#sample function
def computeFinalGrades(x):
    return x.sum()

#filter DataFrame by positions
df1 = df.iloc[:,2:]
#count computeFinalGrades
f = computeFinalGrades(df1.values)

#all Assignments convert to joined string
df['Assignment'] = df1.astype(str).apply(', '.join, axis=1)
df['FinalGrade'] = f
print (df)
  Name  StudentID  Assignment1  Assignment2 Assignment  FinalGrade
0    a          7            1            5       1, 5          39
1    b          8            3            3       3, 3          39
2    c          9            5            6       5, 6          39
3    d          4            7            9       7, 9          39

for name, assign, final in zip(df['Name'],df['Assignment'],df['FinalGrade']):
    print(f'{name} has obtained the grades {assign}. The final grade is {final}')

a has obtained the grades 1, 5. The final grade is 39
b has obtained the grades 3, 3. The final grade is 39
c has obtained the grades 5, 6. The final grade is 39
d has obtained the grades 7, 9. The final grade is 39
Sign up to request clarification or add additional context in comments.

3 Comments

How can I display the output by alphabetical order? I have tried to add df.sort('Name', ascending=False) but it returned KeyError: 'Name'
@bubble33 - You are close, try sort_values like df = df.sort_values('Name', ascending=False)
Worked perfect. Thanks a lot!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.