Informatics Practices
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.
Python Data Handling
7 Likes
Answer
(a)
df = pd.read_csv('Class12.csv', sep='\t')
Output
RollNo Name Accounts Maths BSt IP Eco
0 10 Ritu Jain 88.0 67.0 87.0 97.0 56.0
1 11 Mridul Mehta 67.0 78.0 77.0 87.0 90.0
2 12 Divij 87.0 89.0 78.0 82.0 92.0
3 13 Yashvi Verma 67.0 82.3 76.5 98.2 78.6
4 14 Deepak Virmani 56.7 76.5 88.0 78.0 67.0
5 15 Jatin Malik 76.0 66.0 77.0 87.5 67.5
(b)
df['Total_marks'] = df[['Accounts', 'Maths', 'BSt', 'IP', 'Eco']].sum(axis=1)
Output
RollNo Name Accounts Maths BSt IP Eco Total_marks
0 10 Ritu Jain 88.0 67.0 87.0 97.0 56.0 395.0
1 11 Mridul Mehta 67.0 78.0 77.0 87.0 90.0 399.0
2 12 Divij 87.0 89.0 78.0 82.0 92.0 428.0
3 13 Yashvi Verma 67.0 82.3 76.5 98.2 78.6 402.6
4 14 Deepak Virmani 56.7 76.5 88.0 78.0 67.0 366.2
5 15 Jatin Malik 76.0 66.0 77.0 87.5 67.5 374.0
(c)
df['Average'] = (df['Total_marks'] / 500) * 100
Output
RollNo Name Accounts Maths BSt IP Eco Total_marks Average
0 10 Ritu Jain 88.0 67.0 87.0 97.0 56.0 395.0 79.00
1 11 Mridul Mehta 67.0 78.0 77.0 87.0 90.0 399.0 79.80
2 12 Divij 87.0 89.0 78.0 82.0 92.0 428.0 85.60
3 13 Yashvi Verma 67.0 82.3 76.5 98.2 78.6 402.6 80.52
4 14 Deepak Virmani 56.7 76.5 88.0 78.0 67.0 366.2 73.24
5 15 Jatin Malik 76.0 66.0 77.0 87.5 67.5 374.0 74.80
Answered By
3 Likes
Related Questions
Find the Error :
data = np.array(['a', 'b', 'c', 'd', 'e', 'f']) s = pd.Series(data, index = [100, 101, 102, 103, 104, 105]) print(s[102, 103, 104] )
Can you correct the error ?
Why does the following code cause error?
s1 = pd.Series(range 1, 15, 5), index = list('ababa') print(s1['ab'])
Write a program that reads students marks from a ‘Result.csv’ file and displays percentage of each student.
Write the name of function to store data from a dataframe into a CSV file.