0% found this document useful (0 votes)
7 views3 pages

Task2 - Colaboratory Dip

Uploaded by

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

Task2 - Colaboratory Dip

Uploaded by

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

Dipanshu Nanhe Class: R&A Div 02 Task2 - Colaboratory

Roll No: PB32

from google.colab import drive


drive.mount('/content/drive')

Drive already mounted at /content/drive; to attempt to forcibly remount, call drive.mount("/content/drive", force_remount=True).

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

# Create the student data dictionary


student_data = {
'StudentID': range(1, 101),
'Name': ['Student {}'.format(i) for i in range(1, 101)],
'Age': np.random.randint(18, 30, 100),
'GPA': np.random.uniform(2.0, 4.0, 100),
'Major': np.random.choice(['Engineering', 'Science', 'Business'], 100)
}

# Create a Pandas DataFrame from the student data


df = pd.DataFrame(student_data)

# Save the DataFrame to a CSV file


df.to_csv('student_data.csv', index=False)

df = pd.read_csv('student_data.csv')
df

StudentID Name Age GPA Major

0 1 Student 1 22 2.552273 Business

1 2 Student 2 29 3.469472 Business

2 3 Student 3 29 2.640666 Engineering

3 4 Student 4 29 3.815554 Business

4 5 Student 5 29 2.989830 Business

... ... ... ... ... ...

95 96 Student 96 20 3.795674 Business

96 97 Student 97 20 2.849596 Business

97 98 Student 98 29 2.296410 Science

98 99 Student 99 19 2.577920 Business

99 100 Student 100 27 2.138926 Science

100 rows × 5 columns

# Display the first 5 rows of the DataFrame


print("Task 1: Display the first 5 rows of the DataFrame")
print(df.head())

Task 1: Display the first 5 rows of the DataFrame


StudentID Name Age GPA Major
0 1 Student 1 22 2.552273 Business
1 2 Student 2 29 3.469472 Business
2 3 Student 3 29 2.640666 Engineering
3 4 Student 4 29 3.815554 Business
4 5 Student 5 29 2.989830 Business

# Show summary statistics for 'Age' and 'GPA' columns


print("\nTask 2: Summary statistics for 'Age' and 'GPA' columns")
summary_stats = df[['Age', 'GPA']].describe()
print(summary_stats)

Task 2: Summary statistics for 'Age' and 'GPA' columns


Age GPA
count 100.000000 100.000000
mean 23.490000 2.932050
std 3.630663 0.593364
min 18.000000 2.001235
25% 20.000000 2.437913
50% 23.000000 2.830511
75% 26.250000 3.466067
max 29.000000 3.970887

https://colab.research.google.com/drive/1wzL6YRktB5jGWEQR6OVWwjDfjlzygVG3#scrollTo=VrKhRQAXPSAX&printMode=true 1/3
9/30/23, 11:40 PM Task2 - Colaboratory

# Count the number of students by 'Gender' and create a bar plot


print("\nTask 3: Count the number of students by 'Major' and create a bar plot")
gender_counts = df['Major'].value_counts()
gender_counts.plot(kind='bar')
plt.title('Distribution of Students by Major')
plt.xlabel('Major')
plt.ylabel('Count')
plt.show()

Task 3: Count the number of students by 'Major' and create a bar plot

# Calculate the average 'GPA' for each 'Major' and display as a DataFrame
print("\nTask 4: Calculate the average 'GPA' for each 'Major'")
major_gpa_avg = df.groupby('Major')['GPA'].mean().reset_index()
print(major_gpa_avg)

Task 4: Calculate the average 'GPA' for each 'Major'


Major GPA
0 Business 2.925297
1 Engineering 2.901532
2 Science 2.971649

# Create a new DataFrame with students having 'GPA' > 3.5 and 'Age' < 25
print("\nTask 5: Create a new DataFrame with 'GPA' > 3.5 and 'Age' < 25")
filtered_df = df[(df['GPA'] > 3.5) & (df['Age'] < 25)]
print(filtered_df)

output Task 5: Create a new DataFrame with 'GPA' > 3.5 and 'Age' < 25
StudentID Name Age GPA Major
24 25 Student 25 20 3.525940 Science
37 38 Student 38 23 3.865368 Science
40 41 Student 41 20 3.887455 Science
45 46 Student 46 23 3.829072 Business
47 48 Student 48 23 3.823113 Engineering
54 55 Student 55 23 3.573453 Science
57 58 Student 58 23 3.957774 Science
62 63 Student 63 23 3.634259 Business
70 71 Student 71 20 3.691028 Science
77 78 Student 78 24 3.780056 Business
85 86 Student 86 20 3.714173 Business
87 88 Student 88 18 3.944086 Engineering
88 89 Student 89 22 3.857096 Engineering
91 92 Student 92 20 3.809201 Business
94 95 Student 95 19 3.569902 Science
95 96 Student 96 20 3.795674 Business

https://colab.research.google.com/drive/1wzL6YRktB5jGWEQR6OVWwjDfjlzygVG3#scrollTo=VrKhRQAXPSAX&printMode=true 2/3
9/30/23, 11:40 PM Task2 - Colaboratory

https://colab.research.google.com/drive/1wzL6YRktB5jGWEQR6OVWwjDfjlzygVG3#scrollTo=VrKhRQAXPSAX&printMode=true 3/3

You might also like