Informatics Practices Practical File
Section 1: Python Programs using Pandas and Matplotlib
1. Read and Display a CSV File
import pandas as pd
data = pd.read_csv('data.csv')
print(data)
Output: Displays the contents of data.csv.
2. Basic Statistics with Pandas
import pandas as pd
data = pd.read_csv('data.csv')
print("Mean:\n", data.mean())
print("Median:\n", data.median())
print("Standard Deviation:\n", data.std())
Output: Displays the mean, median, and standard deviation of the numeric columns.
3. Filtering Data from a DataFrame
import pandas as pd
data = pd.read_csv('data.csv')
filtered_data = data[data['Marks'] > 50]
print(filtered_data)
Output: Displays rows where "Marks" is greater than 50.
4. Sorting a DataFrame
import pandas as pd
data = pd.read_csv('data.csv')
sorted_data = data.sort_values(by='Marks', ascending=False)
print(sorted_data)
Output: Displays the DataFrame sorted by "Marks" in descending order.
5. Handling Missing Values
import pandas as pd
data = pd.read_csv('data.csv')
data.fillna(0, inplace=True)
print(data)
Output: Replaces all missing values with 0 and displays the updated DataFrame.
6. Grouping and Aggregation
import pandas as pd
data = pd.read_csv('data.csv')
grouped_data = data.groupby('Class')['Marks'].mean()
print(grouped_data)
Output: Displays the average marks for each class.
7. Creating a Bar Chart
import pandas as pd
import matplotlib.pyplot as plt
data = pd.read_csv('data.csv')
plt.bar(data['Name'], data['Marks'])
plt.xlabel('Name')
plt.ylabel('Marks')
plt.title('Marks of Students')
plt.show()
Output: A bar chart of students' marks.
8. Line Plot
import pandas as pd
import matplotlib.pyplot as plt
data = pd.read_csv('data.csv')
plt.plot(data['Date'], data['Temperature'], marker='o')
plt.xlabel('Date')
plt.ylabel('Temperature')
plt.title('Temperature Over Time')
plt.show()
Output: A line plot showing temperature changes over time.
9. Pie Chart
import pandas as pd
import matplotlib.pyplot as plt
data = pd.read_csv('data.csv')
plt.pie(data['Marks'], labels=data['Name'], autopct='%1.1f%%')
plt.title('Marks Distribution')
plt.show()
Output: A pie chart of marks distribution.
10. Histogram
import pandas as pd
import matplotlib.pyplot as plt
data = pd.read_csv('data.csv')
plt.hist(data['Marks'], bins=5, color='skyblue', edgecolor='black')
plt.xlabel('Marks Range')
plt.ylabel('Frequency')
plt.title('Histogram of Marks')
plt.show()
Output: A histogram showing the frequency of marks.
11. Scatter Plot
import pandas as pd
import matplotlib.pyplot as plt
data = pd.read_csv('data.csv')
plt.scatter(data['Age'], data['Marks'], color='green')
plt.xlabel('Age')
plt.ylabel('Marks')
plt.title('Age vs Marks')
plt.show()
Output: A scatter plot of age vs marks.
12. Add a New Column
import pandas as pd
data = pd.read_csv('data.csv')
data['Total Marks'] = data['Maths'] + data['Science'] + data['English']
print(data)
Output: Displays the DataFrame with a new column "Total Marks".
13. Save DataFrame to a New CSV
import pandas as pd
data = pd.read_csv('data.csv')
data.to_csv('new_data.csv', index=False)
print("File saved successfully.")
Output: Saves the DataFrame to a new CSV file.
14. Display Top 5 Rows
import pandas as pd
data = pd.read_csv('data.csv')
print(data.head())
Output: Displays the first 5 rows of the DataFrame.
15. Calculate Correlation
import pandas as pd
data = pd.read_csv('data.csv')
print(data.corr())
Output: Displays the correlation matrix of numeric columns.
16. Calculate Frequency of a Column
import pandas as pd
data = pd.read_csv('data.csv')
frequency = data['Category'].value_counts()
print(frequency)
Output: Displays the frequency of each category in the "Category" column.
17. Box Plot
import pandas as pd
import matplotlib.pyplot as plt
data = pd.read_csv('data.csv')
plt.boxplot(data['Marks'])
plt.title('Box Plot of Marks')
plt.show()
Output: A box plot of marks.
18. Highlight Maximum and Minimum Values
import pandas as pd
data = pd.read_csv('data.csv')
print("Max Marks:\n", data['Marks'].max())
print("Min Marks:\n", data['Marks'].min())
Output: Displays the maximum and minimum marks.
19. Merge Two DataFrames
import pandas as pd
data1 = pd.read_csv('data1.csv')
data2 = pd.read_csv('data2.csv')
merged_data = pd.merge(data1, data2, on='ID')
print(merged_data)
Output: Displays the merged DataFrame based on "ID".
20. Drop a Column
import pandas as pd
data = pd.read_csv('data.csv')
data = data.drop(columns=['Unwanted_Column'])
print(data)
Output: Displays the DataFrame after dropping the specified column.
Section 2: SQL Queries with Functions
1. Display all data from a table
SELECT * FROM students;
Output: Displays all records from the "students" table.
2. Count the number of students
SELECT COUNT(*) FROM students;
Output: Displays the total count of students.
3. Find the average marks
SELECT AVG(marks) FROM students;
Output: Displays the average marks of all students.
4. Find the maximum marks
SELECT MAX(marks) FROM students;
Output: Displays the highest marks.
5. Find the minimum marks
SELECT MIN(marks) FROM students;
Output: Displays the lowest marks.
6. Display students scoring more than 80
SELECT * FROM students WHERE marks > 80;
Output: Displays details of students scoring more than 80.
7. Group students by class and find average marks
SELECT class, AVG(marks) FROM students GROUP BY class;
Output: Displays the average marks for each class.
8. Find total marks of all students
SELECT SUM(marks) FROM students;
Output: Displays the total marks scored by all students.
9. Display distinct classes
SELECT DISTINCT class FROM students;
Output: Displays all unique classes.
10. Display students sorted by marks in descending order
SELECT * FROM students ORDER BY marks DESC;
Output: Displays students sorted by marks in descending order.
11. Display students scoring between 60 and 80
SELECT * FROM students WHERE marks BETWEEN 60 AND 80;
Output: Displays students with marks in the specified range.
12. Find the number of students in each class
SELECT class, COUNT(*) FROM students GROUP BY class;
Output: Displays the count of students in each class.
13. Display students whose names start with 'A'
SELECT * FROM students WHERE name LIKE 'A%';
Output: Displays students whose names start with 'A'.
14. Find the length of student names
SELECT name, LENGTH(name) AS Name_Length FROM students;
Output: Displays the length of each student's name.
15. Convert names to uppercase
SELECT UPPER(name) FROM students;
Output: Displays all student names in uppercase.
16. Find the total marks in each class
SELECT class, SUM(marks) FROM students GROUP BY class;
Output: Displays total marks for each class.
17. Display current date and time
SELECT NOW();
Output: Displays the current date and time.
18. Extract year from the date of birth
SELECT name, YEAR(dob) AS Birth_Year FROM students;
Output: Displays names and birth years of students.
19. Find students with duplicate marks
SELECT marks, COUNT(*) FROM students GROUP BY marks HAVING COUNT(*) > 1;
Output: Displays marks that are duplicated.
20. Delete students with marks less than 40
DELETE FROM students WHERE marks < 40;
Output: Deletes records of students with marks below 40.