[go: up one dir, main page]

0% found this document useful (0 votes)
11 views8 pages

0 Introduction To Data Science - Exercises

Uploaded by

milo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views8 pages

0 Introduction To Data Science - Exercises

Uploaded by

milo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Data Science

Exercises
Chapter 7
Exercises
DataFrame
1. Create a DataFrame containing student scores for three subjects: Math, Science, and History.
Include at least 5 students and their respective scores.

2. Add a new column to the DataFrame called "Total" which contains the total score of each student.

3. Calculate the average score for each subject and add it to the DataFrame.

4. Find the student(s) with the highest total score.

5. Filter out students who scored less than 80 in any subject.


DataFrame
1. Create a DataFrame containing student scores for three subjects: Math,
Science, and History. Include at least 5 students and their respective
scores. import pandas as pd
data = {
'StudentID': [1, 2, 3, 4, 5],
'Math': [85, 90, 75, 88, 92],
'Science': [78, 85, 80, 92, 87],
'History': [70, 65, 80, 75, 85]
}

#create a DataFrame
df = pd.DataFrame(data)
print ('Display the DataFrame')
print(df)
print()
DataFrame
2. Add a new column to the DataFrame called "Total" which contains the
total score of each student.

#Calculate Total Scores


df['Total'] = df['Math'] + df['Science'] + df['History']
print ('Calculate Total Scores')
print(df)
print()
DataFrame
3. Calculate the average score for each subject and add it to the DataFrame.

#Calculate Average Scores


df.loc['Average'] = df.mean()
print ('Calculate Average Scores')
print(df)
print()
DataFrame
4. Find the student(s) with the highest total score.

#Find the Top Performers


top_performer = df[df['Total'] == df['Total'].max()]
print ('Find the Top Performers')
print(top_performer)
print()
DataFrame
5. Filter out students who scored less than 80 in any subject.

#Filter Students
filtered_df = df[(df['Math'] >= 80) & (df['Science'] >= 80) & (df['History'] >= 80)]
print ('Filter Students')
print(filtered_df)

You might also like