RASHTRIYA INDIAN MILITARY COLLEGE, JANUARY TERM, 2025
PRACTICAL FILE
Informatics Practices
Submitted To: Submitted By:
Mr.Manoj Kumar Nama Name: Cdt. Aanik Verma
Roll No:3261/S
1
Index
List of Experiments
S No Name of Experiment Page No
1 Write a python program to draw line charts from the given 4-5
financial data of XYZ Co. for 5 days in the form a Data Frame
namely fdf
2 Shubham, a Data Analyst with a multinational brand has designed 5-7
the Data Frame df that contains the four quarters' sales data
of different stores
3 Write the UPDATE command to increase the commission (Column 7
name : COMM) by 500 of all the Salesmen who have achieved Sales
(Column name : SALES) more than 200000. The table's name
is COMPANY.
4 Write a program to print a Data Frame one row at a time and print 7-9
only first five rows.
5 Write a program to iterate over a data frame containing names and 9-10
marks which then calculates grades as per marks(as per guiding
below) and adds them to the grade column.
6 Consider the following graph 11-12
7 Experiment No. 5: Given a series nfib that contains reversed 12-13
Fibonacci numbers with Fibonacci numbers
8 Write a program to add titles 14
9 Consider the following tables 15-16
10 Consider the following tables CABHUB and CUSTOMER 17-18
11 Based on the SQL table CAR_SALES,write suitable queries 19-20
12 In a database Tamil Nadu_Sangam there are two tables with the 21-26
instances
13 Given an ndarray p as ([1, 2, 3, 4]). Write code to plot a bar chart. 27-28
14 PQR is a list having three lists inside it. It contains summarized 28-29
data of three different trials conducted by company A. create a bar
chart that plots these three sub lists of ABC in a single chart.
15 Write a code to create a scatter chart from the 1990 and 2010 30-31
16 Write a SQL query to order the (student ID, marks) table in 32
descending order of the marks.
17 Write a program to create three different Series objects from the 33-34
three rows of a Data Frame df.
18 Write a suitable python code to create (favourite hobby)bar chart 34-35
19 Create an array, A, in the range 1 to 20 with values 1.25 apart. 35-37
Another array, B, contains the log values of the elements in the
array A. Write a program to create a scatter plot of first vs. second
array (array A vs. B)
2
20 Create a panda’s series from a dictionary of values and a ndarray. 38-39
21 Create a data frame in python 39-40
22 Write Python code to create a Series object Temp2 storing 40-41
temperatures of seven days of week. Its indexes should
be ;Sunday ;Monday;Saturday.
23 Consider the following series object namely S 41-42
24 Write suitable SQL queries 43-44
25 Consider the following tables 44
26 Write a Pandas program to convert a dictionary to a Pandas series. 44
Sample dictionary: d1 = {a: 100, ;b: 200;300}
27 Consider the following table GAME 45
28 Consider the following tables 46-51
29 Find the total number of customers from each country in the table 52
(customer ID, customer Name, country) using group by.
30 Find the min, max, sum, and average of the marks in a student 52
marks table.
31 Consider the following table ‘DRINK’ 53
32(a) Titanic data analysis 54-55
32(b Covid 19 data analysis 56-57
)
32(c) # Importing and exporting data between pandas and CSV file. 57-61
# To create and open a data frame using ‘Student_result.csv’
file using Pandas.
# To display row labels, column labels data types of each
column and the dimensions
# To display the shape (number of rows and columns) of the
CSV file.
# To display Adm_No, Gender and Percentage from
‘student_result.csv’ file.
# To display the first 5 and last 5 records from
‘student_result.csv’ file.
# To display Student_result file with new column names.
# To modify the Percentage of student below 35 with NaN
value in dataframe.
3
Practical Assignment
Experiment No. 1:
Write a python program to draw line charts from the given financial data of
XYZ Co. for 5 days in the form a DataFrame namely fdf as shown below:
Day1 Day2 Day3 Day4 Day5
0 74.25 56.03 59.30 69.00 89.65
1 76.06 68.71 72.07 78.47 79.65
2 69.50 62.89 77.65 65.53 80.75
3 72.55 56.42 66.46 76.85 85.08
Output:
4
Experiment No. 2:
Let's break down these Pandas operations!
(i) Predicting the output of Python statements:
(a) print(df.size)
5
The .size attribute of a Pandas DataFrame returns the total number of elements in the DataFrame. This is
calculated by multiplying the number of rows by the number of columns. 1
In the DataFrame df, there are 3 rows and 5 columns ('Store', 'Qtr1', 'Qtr2', 'Qtr3', 'Qtr4').
Therefore, the output will be: 15
(b) print(df[1:3])
This Python statement uses slicing to select rows from the DataFrame. The syntax [start:end] selects
rows starting from the index specified by start up to (but not including) the index specified by end.
Here, 1:3 means it will select rows starting from index 1 up to (but not including) index 3. This includes the
rows with index 1 and index 2.
Therefore, the output will be:
Store Qtr1 Qtr2 Qtr3 Qtr4
1 Store2 350 340 403 210
2 Store3 250 180 145 160
(ii) Deleting the last row from the DataFrame:
To delete the last row of the DataFrame, you can use the .drop() method along with the index of the last
row. You can get the index of the last row using df.index[-1].
Python
df_dropped = df.drop(df.index[-1])
print(df_dropped)
This would result in:
Store Qtr1 Qtr2 Qtr3 Qtr4
0 Store1 300 240 450 230
1 Store2 350 340 403 210
(iii) Write Python statement to add a new column 'Total Sales' which is the addition of all the 4
quarter sales:
You can create a new column named 'Total Sales' by summing the values across the 'Qtr1', 'Qtr2', 'Qtr3', and
'Qtr4' columns row-wise.
Python
df['Total Sales'] = df['Qtr1'] + df['Qtr2'] + df['Qtr3'] + df['Qtr4']
print(df)
This will add the 'Total Sales' column to your DataFrame:
Store Qtr1 Qtr2 Qtr3 Qtr4 Total Sales
0 Store1 300 240 450 230 1220
1 Store2 350 340 403 210 1303
2 Store3 250 180 145 160 735
Experiment No. 3:
Write the UPDATE command to increase the commission (Column name : COMM) by 500 of
all the Salesmen who have achieved Sales (Column name : SALES) more than 200000. The
table's name is COMPANY.
Output :
6
Experiment No. 4:
Write a program to print a DataFrame one row at a time and print only first five rows.
import pandas as pd
def print_dataframe_rows(df):
"""
Prints the first five rows of a DataFrame, one row at a time.
Args:
df: The pandas DataFrame to print.
"""
print("Printing the first five rows of the DataFrame, one row at a time:")
for index, row in df.iterrows():
print(f"Row {index}:")
print(row)
if index >= 4:
break
if __name__ == "__main__":
data = {
'Name': ['Alice', 'Bob', 'Charlie', 'David', 'Emily', 'Frank', 'Grace'],
'Age': [25, 30, 22, 28, 35, 40, 27],
'City': ['New York', 'Los Angeles', 'Chicago', 'Houston', 'Phoenix',
'Philadelphia', 'San Antonio'],
'Salary': [50000, 60000, 45000, 55000, 70000, 80000, 52000]
df = pd.DataFrame(data)
7
print_dataframe_rows(df
Output:
Row 0:
Name Alice
Age 25
City New York
Salary 50000
Name: 0, dtype: object
Row 1:
Name Bob
Age 30
City Los Angeles
Salary 60000
Name: 1, dtype: object
Row 2:
Name Charlie
Age 22
City Chicago
Salary 45000
Name: 2, dtype: object
Row 3:
Name David
Age 28
City Houston
Salary 55000
Name: 3, dtype: object
Row 4:
Name Emily
Age 35
City Phoenix
Salary 70000
8
Name: 4, dtype: object
Experiment No. 5:
Write a program to iterate over a dataframe containing names and marks which
then calculates grades as per marks(as per guiding below) and adds them to the
grade column.
Marks>= 95 grade A+ ; Marks 50-60 grade C;
Marks 70-90 grade A ; Marks 40-50 grade D;
Marks 60-70 grade B ; Marks <40 grade F
import pandas as pd
def calculate_grade(marks):
"""
Calculates the grade based on the given marks.
Args:
marks: The marks obtained by a student.
Returns:
The grade corresponding to the marks.
"""
if marks >= 95:
return 'A+'
elif 70 <= marks < 90:
return 'A'
elif 60 <= marks < 70:
return 'B'
elif 50 <= marks < 60:
return 'C'
elif 40 <= marks < 50:
return 'D'
else:
return 'F'
def process_student_data(data):
"""
Processes student data, calculates grades, and adds them to a DataFrame.
Args:
data: A dictionary containing student names and marks.
Returns:
A pandas DataFrame with names, marks, and grades.
"""
df = pd.DataFrame(data)
df['Grade'] = df['Marks'].apply(calculate_grade)
return df
if __name__ == "__main__":
student_data =
'Name': ['Alice', 'Bob', 'Charlie', 'David', 'Emily', 'Frank', 'Grace'],
9
'Marks': [98, 85, 62, 55, 48, 30, 75]
student_grades_df = process_student_data(student_data)
print(student_grades_df)
Output :
Name Marks Grade
0 Alice 98 A+
1 Bob 85 A
2 Charlie 62 B
3 David 55 C
4 Emily 48 D
5 Frank 30 F
6 Grace 75 A
Experiment No. 6:
10
Output:
11
Experiment No. 7:
Given a series nfib that contains reversed Fibonacci numbers with Fibonacci numbers as
shown below:
[0, -1, -1, -2, -3, -5, -8, -13, -21, -34, 0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
Write a program to plot nfib with following specifications:
(a) The line color should be magenta.
(b) The marker edge color should be black with size 5.
(c) Grid should be displayed.
Output:
12
Experiment No. 8:
13
Output:
Experiment No. 9: Consider the following tables
14
Output :
15
Experiment No. 10:
16
Output:
17
Experiment No. 11:
18
Output:
19
Experiment No. 12:
20
21
Output:
22
23
24
Experiment No. 28:
25
Output:
Experiment No. 13:
26
Given an ndarray p as ([1, 2, 3, 4]). Write code to plot a bar chart having bars for
p and p**2 (with red colour) and another bar for p and p*2 (with blue colour).
Output:
import matplotlib.pyplot as plt
import numpy as np
p = np.array([1, 2, 3, 4])
x = np.arange(len(p)) # the label locations
width = 0.35 # the width of the bars
fig, ax = plt.subplots()
rects1 = ax.bar(x - width/2, p**2, width, label='$p^2$', color='red')
rects2 = ax.bar(x + width/2, p*2, width, label='$2p$', color='blue')
ax.set_ylabel('Values')
ax.set_title('Bar chart of $p^2$ and $2p$')
ax.set_xticks(x)
ax.set_xticklabels(p)
ax.legend()
ax.bar_label(rects1, padding=3)
ax.bar_label(rects2, padding=3)
fig.tight_layout()
plt.show()
27
Experiment No. 14:
PQR is a list having three lists inside it. It contains summarized data of three
different trials conducted by company A. create a bar chart that plots these
three sublists of ABC in a single chart. Keep the width of each bar as 0.25.
XYZ = [[5, 25, 45, 20], [4, 23, 49, 17], [6, 22, 47, 19]]
import matplotlib.pyplot as plt
import numpy as np
def plot_trial_data(data, width=0.25):
"""
Plots the trial data as a grouped bar chart.
Args:
data: A list of lists, where each sublist represents a trial.
width: The width of each bar. Defaults to 0.25.
"""
num_trials = len(data)
num_categories = len(data[0])
x = np.arange(num_categories)
colors = ['blue', 'green', 'red', 'cyan', 'magenta', 'yellow']
fig, ax = plt.subplots()
for i, trial_data in enumerate(data):
x_positions = x + (i - num_trials / 2 + 0.5) * width
ax.bar(x_positions, trial_data, width, label=f'Trial {i+1}', color=colors[i %
len(colors)])
ax.set_xlabel('Category')
ax.set_ylabel('Values')
ax.set_title('Bar Chart of Trial Data')
ax.set_xticks(x)
ax.set_xticklabels([f'Category {i+1}' for i in range(num_categories)])
ax.legend()
for i, trial_data in enumerate(data):
x_positions = x + (i - num_trials / 2 + 0.5) * width
for j, value in enumerate(trial_data):
ax.text(x_positions[j], value, str(value), ha='center', va='bottom')
plt.tight_layout()
plt.show()
if __name__ == "__main__":
xyz_data = [[5, 25, 45, 20], [4, 23, 49, 17], [6, 22, 47, 19]]
plot_trial_data(xyz_data
28
Experiment No. 15:
29
Output:
30
31
Experiment No. 16:
Write a SQL query to order the (student ID, marks) table in descending order of the marks.
Output:
32
Experiment No. 17:
Write a program to create three different Series objects from the three rows of a DataFrame
df.
Output:
Series from the first row:
Store Store1
Qtr1 300
Qtr2 240
Qtr3 450
Qtr4 230
Name: 0, dtype: object
Series from the second row:
33
Store Store2
Qtr1 350
Qtr2 340
Qtr3 403
Qtr4 210
Name: 1, dtype: object
Series from the third row:
Store Store3
Qtr1 250
Qtr2 180
Qtr3 145
Qtr4 160
Name: 2, dtype: object
Experiment No. 18:
34
Output:
Experiment No. 19:
Create an array, A, in the range 1 to 20 with values 1.25 apart. Another
array,
B, contains the log values of the elements in the array A.
Write a program to create a scatter plot of first vs. second array (array A
vs. B)
with red circle markers; specify the x-axis (containing first array’s
values) title
as ‘Random Values’ and y-axis title as ‘Logarithm Values’.
35
Output:
36
37
Experiment No. 20:
Create a panda’s series from a dictionary of values and a ndarray.
Output:
Pandas Series from Dictionary:
A 10
B 20
C 30
D 40
dtype: int64
Pandas Series from ndarray with Index:
38
one 100
two 200
three 300
four 400
five 500
dtype: int32
Pandas Series from ndarray with Default Index:
0 100
1 200
2 300
3 400
4 500
dtype: int32
Experiment No. 21:
39
Output:
Experiment No. 22:
Write Python code to create a Series object Temp2 storing temperatures of seven days of
week. Its indexes should be 'Sunday', 'Monday',... 'Saturday'.
Output:
import pandas as pd
# Temperature data for seven days
temperatures = [22, 23, 25, 24, 26, 27, 25]
# Days of the week as index
days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday',
'Friday', 'Saturday']
40
# Create the Series object Temp2
Temp2 = pd.Series(temperatures, index=days)
# Print the Series object to verify
print(Temp2)
Experiment No. 23:
Output:
Let's analyze the given Pandas Series S and predict the output of the Python statements.
Given Series S:
0 0.430271
1 0.617328
2 -0.265421
3 -0.836113
dtype: float64
(a) S * 100
This operation will multiply each element of the Series S by 100.
Output:
0 43.0271
1 61.7328
41
2 -26.5421
3 -83.6113
dtype: float64
(b) S > 0
This operation will perform a boolean comparison for each element of the Series S to check if it is greater
than 0. It will return a new Series of boolean values.
Output:
0 True
1 True
2 False
3 False
dtype: bool
(c) S1 = pd.Series(S)
This statement creates a new Series object named S1 by copying the data and index from the existing Series
S. Essentially, S1 will be an identical copy of S.
Value of Series object S1:
0 0.430271
1 0.617328
2 -0.265421
3 -0.836113
dtype: float64
(d) S2 = pd.Series(S1) + 3
This statement first creates a new Series object named S2 by copying the data and index from the Series S1
(which is a copy of S). Then, it adds the scalar value 3 to each element of the newly created Series S2.
Value of Series object S2:
0 3.430271
1 3.617328
2 2.734579
3 2.163887
dtype: float64
Experiment No. 24:
42
Output:
(i) To calculate the exponent for 3 raised to the power of 4.
SQL
SELECT POWER(3, 4);-- OR (in some systems like PostgreSQL)SELECT 3 ^ 4;--
OR (in some systems like SQL Server)SELECT EXP(4 * LOG(3));
(ii) To display the current date and time.
SQL
-- MySQLSELECT NOW();
-- PostgreSQLSELECT NOW();SELECT CURRENT_TIMESTAMP;
-- SQL ServerSELECT GETDATE();
-- OracleSELECT SYSDATE FROM dual;SELECT SYSTIMESTAMP FROM dual;
(iii) To round off the value -34.4567 to 2 decimal places.
SQL
SELECT ROUND(-34.4567, 2);
(iv) To remove all the probable leading and trailing spaces from the column userid of the table
named user.
SQL
UPDATE userSET userid = TRIM(userid);
(v) To display the length of the string 'FIFA World Cup'.
SQL
-- MySQL, PostgreSQL, SQL ServerSELECT LENGTH('FIFA World Cup');
-- OracleSELECT LENGTH('FIFA World Cup') FROM dual;
Experiment No. 25: Consider the following table:
43
Output:
Experiment No. 26:
Write a Pandas program to convert a dictionary to a Pandas series.
Sample dictionary: d1 = {'a': 100, 'b': 200, 'c':300}
Output:
import pandas as pd
# Sample dictionary
d1 = {'a': 100, 'b': 200, 'c': 300}
# Convert the dictionary to a Pandas Series
series1 = pd.Series(d1)
# Print the resulting Series
print(series1)
Experiment No. 27:
44
Output:
Experiment No. 28:
45
46
Output:
47
48
49
Experiment No. 28:
50
Output:
Experiment No. 29:
51
Experiment No. 30:
52
53
54
55
56
57
58
59
60
61