Informatics Practices
Write a program that reads students marks from a ‘Result.csv’ file and displays percentage of each student.
Answer
import pandas as pd
df = pd.read_csv('Result.csv')
df['Total_marks'] = df[['Maths', 'Science', 'English',]].sum(axis=1)
df['Percentage'] = (df['Total_marks'] / 300) * 100
print("Percentage of each student: ")
print(df[['Name', 'Percentage']])Output
Percentage of each student:
Name Percentage
0 Rahul 85.000000
1 Rohan 81.666667
2 Riya 88.333333
3 Raj 86.666667
Related Questions
Why does the following code cause error?
s1 = pd.Series(range 1, 15, 5), index = list('ababa') print(s1['ab'])Consider the following Class12.csv file containing the data as given below:
RollNo Name Accounts Maths BSt IP Eco 10 Ritu Jain 88 67 87 97 56 11 Mridul Mehta 67 78 77 87 90 12 Divij 87 89 78 82 92 13 Yashvi Verma 67 82.3 76.5 98.2 78.6 14 Deepak Virmani 56.7 76.5 88 78 67 15 Jatin Malik 76 66 77 87.5 67.5 (a) Read the csv file into a dataframe df which is stored with tab ('\t') separator.
(b) Write the code to find the total marks (Total_ marks) for each student and add it to the newly-created dataframe.
(c) Also calculate the percentage obtained by each student under a new column “Average” in the dataframe.
Write the name of function to store data from a dataframe into a CSV file.
How can we import specific columns from a CSV file?