0% found this document useful (0 votes)
55 views27 pages

Class 12 IP File 23 24

The document provides 10 programs related to data handling and analysis using Pandas library in Python. The programs cover topics like creating series and dataframes from various data structures, accessing elements, filtering data, basic visualization and SQL operations for data management. Overall, the programs demonstrate common data wrangling and exploration tasks using Pandas.

Uploaded by

toeshipahadiya
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)
55 views27 pages

Class 12 IP File 23 24

The document provides 10 programs related to data handling and analysis using Pandas library in Python. The programs cover topics like creating series and dataframes from various data structures, accessing elements, filtering data, basic visualization and SQL operations for data management. Overall, the programs demonstrate common data wrangling and exploration tasks using Pandas.

Uploaded by

toeshipahadiya
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/ 27

INFORMATICS PRACTICES (065)

PRACTICAL PROGRAMS (2023-24)


S. No. Program Date Page No. Remark
Name s
Data Handling
Create a panda’s series from a dictionary of values
1. and ndarray.
2.
Write a program to create a series and access its
elements using index and slicing.
Given a Series, print all the elements that are above
3. the 75th percentile.
4.
Write a program to create a series and display its first
5 and last 5 elements.
Write a python program to create a data frame with
5. the help of a dictionary, in which each row contains
the item category, item name, and expenditure. Print
the total expenditure of the quarter.
Create a data frame for examination result and
6. display row labels, column labels data types of each
column and the dimensions.
7.
Write a program to create a dataframe and display its
first 2 and last 2 elements.
8.
Write a Pandas program to create and display a
DataFrame from a dictionary data which has the index
labels.
9. Write a Pandas program to add the column Roll_No
in the DataFrame(Specified in Program 8). Also
delete the data for student “Anjali”.
Filter out rows based on different criteria such as
10. duplicate rows.
Importing and exporting data between pandas and
11. CSV file.

Visualization
Given the school result data, analyze the
12. performance of the students on different parameters,
e.g., subject wise or class wise.
Write a program to plot the graph for result analysis
13. based on different subjects using the Data frame with
title and legends.
Take data of weights of 24 people and analyze
14. the data using the histogram.

Data
Management
Create a student table with the student id, name,
15. and marks as attributes where the student id is the
primary key.
16. Insert the details of a new student in the above table.

Delete the details of students who are having ‘i’ as the


17.
last character in his/her name from the above table.
Use the select command to get the details of
18.
the students with marks more than 70.
Find the min, max, sum, and average of the marks in
19.
the student table.
Find the total number of customers from each
20. country in the table (customer ID, customer Name,
country) using group by.
Write a SQL query to order the (student ID, marks)
21. table in descending order of the marks.
22. Write the SQL query to find out day , day name
month, month name and year of the date
(29/02/2000).
23. Write a SQL query to find the Mod, round and power
of a number.
24. Write the SQL query to execute various string
functions.
Program 1: Create a panda’s series from a dictionary of values and a ndarray.

Source Code:

#import pandas as pd
import pandas as pd
# import numpy as np
import numpy as np
# numpy array
arr = np.array(['A','B','K','S','F', 'O','E','K','S'])
# forming series
Sr = pd.Series(arr)
# output
print("Series from Array", Sr)
# simple dict
dict = {'A':82,'B':40,'K':8,'J':92, 'L':19,'Z':15,'N':16}
# forming series
s = pd.Series(dict)
# output
print("Series from Dictionary",s)
Output:

Series from Array 0 A


1 B
2 K
3 S
4 F
5 O
6 E
7 K
8 S
dtype: object

Series from Dictionary A 82


B 40
K 8
J 92
L 19
Z 15
N 16
dtype: int64
Program 2: Write a program to create a series and access its elements using
index and slicing.

Source Code:

# import pandas and numpy


import pandas as pd
import numpy as np

# creating simple array


data = np.array(['g','e','e','k','s','f', 'o','r','g','e','e','k','s'])
ser = pd.Series(data,index=[10,11,12,13,14,15,16,17,18,19,20,21,22])

# accessing a element using index element


print("Accessing a element using index element ",ser[16])

#accessing the elements using slicing


print("Accessing the elements using slicing\n",ser[2:9:3])

Output:

Accessing a element using index element o


Accessing the elements using slicing
12 e
15 f
18 g
dtype: object
Program 3: Given a Series, print all the elements that are above the 75th
percentile.

Source Code:

import pandas as pd
import numpy as np
a=np.array([1,3,4,7,8,8,9])
Sr=pd.Series(a)
print("Series is\n",Sr)
s=df.quantile([.75])
print("Result is \n",s)

Output:

Series is
0 1
1 3
2 4
3 7
4 8
5 8
6 9
dtype: int32
Result is
0.75 8.0
dtype: float64
Program 4: Write a program to create a series and display its first 5 and last 5
elements.

Source Code:

import pandas as pd
import numpy as np
Sr=pd.Series([1,3,4,7,8,8,9])
print("First five elements of series:\n",Sr.head(5))
print("Last five elements of series:\n",Sr.tail(5))

Output:

First five elements of series:


0 1
1 3
2 4
3 7
4 8
dtype: int64
Last five elements of series:
2 4
3 7
4 8
5 8
6 9
dtype: int64
Program 5: Write a python program to create a data frame with the help of a
dictionary, in which each row contains the item category, item name, and
expenditure. Print the total expenditure of the quarter.

Source Code:

import pandas as pd
data={'Category':['Medical','Transport','Grocery','Clothes'],'Item
Name':['Sanitizer','Cab','Snacks','Summer Wears'],'Expenditure':[1000,4250,1500,5200]}

quarterly_expenditure=pd.DataFrame(data)

print(quarterly_expenditure)

print('Total Expenditure in Quarter')

print(quarterly_expenditure['Expenditure'].sum())

Output:

Category Item Name Expenditure


0 Medical Sanitizer 1000
1 Transport Cab 4250
2 Grocery Snacks 1500
3 Clothes Summer Wears 5200
Total Expenditure in Quarter
11950
Program 6: Create a data frame for examination result and display row
labels, column labels, data types of each column and the dimensions.
Source Code:

import pandas as pd

# Create a dictionary of examination results


results = {'Name': ['John', 'Emily', 'Michael', 'Jessica'],'Math': [87, 92, 94, 90],'Science': [89, 93, 95, 87],
'English': [78, 85, 89, 80]}

# Create a DataFrame from the dictionary


df = pd.DataFrame(results)

# Display row labels


print("Index:\n",df.index)

# Display column labels


print("Columns:\n",df.columns)

# Display data types of each column


print("Data type:\n",df.dtypes)

# Display the dimensions of the DataFrame


print("Shape:\n",df.shape)

Output:

Index:
RangeIndex(start=0, stop=4, step=1)
Columns:
Index(['Name', 'Math', 'Science', 'English'], dtype='object')
Data type:
Name object
Math int64
Science int64
English int64
dtype: object
Shape:
(4, 4)
Program 7: Write a program to create a dataframe and display its first 2 and last
2 elements.

Source Code:

import pandas as pd

# Create a dictionary of examination results

results = {'Name': ['Ilma', 'Astha', 'Mohini', 'Anju'],'Math': [87, 92, 94, 90],'Science': [89, 93, 95,
87],'English': [78, 85, 89, 80]}

# Create a DataFrame from the dictionary


df = pd.DataFrame(results)

print("First two elements of series:\n",df.head(2))


print("Last two elements of series:\n",df.tail(2))

Output:

First two elements of series:


Name Math Science English
0 Ilma 87 89 78
1 Astha 92 93 85
Last two elements of series:
Name Math Science English
2 Mohini 94 95 89
3 Anju 90 87 80
Program 8: Write a Pandas program to create and display a DataFrame from a
dictionary data which has the index labels.

Source Code:

import pandas as pd
import numpy as np

exam_data = {'name': ['Muskan', 'Nidhi', 'Kanishka', 'Jiya', 'Anjali', 'Manu', 'Mehak', 'Leena', 'Kavya',
'Joya'],
'score': [12.5, 9, 16.5, np.nan, 9, 20, 14.5, np.nan, 8, 19], 'attempts': [1, 3, 2, 3, 2, 3, 1, 1, 2, 1],
'qualify': ['yes', 'no', 'yes', 'no', 'no', 'yes', 'yes', 'no', 'no', 'yes']}
labels = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']
df = pd.DataFrame(exam_data , index=labels)
print(df)

Output:

name score attempts qualify


a Muskan 12.5 1 yes
b Nidhi 9.0 3 no
c Kanishka 16.5 2 yes
d Jiya NaN 3 no
e Anjali 9.0 2 no
f Manu 20.0 3 yes
g Mehak 14.5 1 yes
h Leena NaN 1 no
i Kavya 8.0 2 no
j Joya 19.0 1 yes
Program 9: Write a Pandas program to add the column Roll_No in a DataFrame
(Specified in Program 7). Also delete the data for Student “Anjali”.

Source Code:

import pandas as pd
import numpy as np
exam_data = {'name': ['Muskan', 'Nidhi', 'Kanishka', 'Jiya', 'Anjali', 'Manu', 'Mehak', 'Leena', 'Kavya',
'Joya'],
'score': [12.5, 9, 16.5, np.nan, 9, 20, 14.5, np.nan, 8, 19],'attempts': [1, 3, 2, 3, 2, 3, 1, 1, 2, 1],'qualify':
['yes', 'no', 'yes', 'no', 'no', 'yes', 'yes', 'no', 'no', 'yes']}
labels = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']
df = pd.DataFrame(exam_data , index=labels)
#adding the column Roll_No
df["Roll_No"]=[1,2,3,4,5,6,7,8,9,10]
print("Data Frame after adding column Roll_No\n", df)
#Deleting the row
df=df.drop('e',axis=0)
print("\nData Frame after adding column Roll_No\n",df)

Output:
Data Frame after adding column Roll_No
name score attempts qualify Roll_No
a Muskan 12.5 1 yes 1
b Nidhi 9.0 3 no 2
c Kanishka 16.5 2 yes 3
d Jiya NaN 3 no 4
e Anjali 9.0 2 no 5
f Manu 20.0 3 yes 6
g Mehak 14.5 1 yes 7
h Leena NaN 1 no 8
i Kavya 8.0 2 no 9
j Joya 19.0 1 yes 10

Data Frame after adding column Roll_No


name score attempts qualify Roll_No
a Muskan 12.5 1 yes 1
b Nidhi 9.0 3 no 2
c Kanishka 16.5 2 yes 3
d Jiya NaN 3 no 4
f Manu 20.0 3 yes 6
g Mehak 14.5 1 yes 7
h Leena NaN 1 no 8
i Kavya 8.0 2 no 9
j Joya 19.0 1 yes 10
Program 10: Filter out rows based on different criteria such as duplicate rows in
the dataframe of Program.

Source Code:

import pandas as pd
import numpy as np
exam_data = {'name': ['Muskan', 'Nidhi', 'Kanishka', 'Jiya', 'Anjali', 'Muskan', 'Mehak', 'Leena', 'Kavya',
'Joya'],
'score': [12.5, 9, 16.5, np.nan, 9, 12.5, 14.5, np.nan, 8, 19],'attempts': [1, 3, 2, 3, 2, 1, 1, 1, 2, 1],'qualify':
['yes', 'no', 'yes', 'no', 'no', 'yes', 'yes', 'no', 'no', 'yes']}
labels = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']
df = pd.DataFrame(exam_data , index=labels)
print("Data Frame\n", df)
#Filtering the duplicate rows
df=df[df.duplicated(keep=False)]
print("\n Duplicate rows\n",df)

Output:

Data Frame
name score attempts qualify
a Muskan 12.5 1 yes
b Nidhi 9.0 3 no
c Kanishka 16.5 2 yes
d Jiya NaN 3 no
e Anjali 9.0 2 no
f Muskan 12.5 1 yes
g Mehak 14.5 1 yes
h Leena NaN 1 no
i Kavya 8.0 2 no
j Joya 19.0 1 yes

Duplicate rows
name score attempts qualify
a Muskan 12.5 1 yes
f Muskan 12.5 1 yes
Program 11: Importing and exporting data between pandas and CSV file.

Source Code:

import pandas as pd
import numpy as np

exam_data = {'name': ['Muskan', 'Nidhi', 'Kanishka', 'Jiya', 'Anjali', 'Manu', 'Mehak', 'Leena', 'Kavya',
'Joya'],
'score': [12.5, 9, 16.5, np.nan, 9, 20, 14.5, np.nan, 8, 19],'attempts': [1, 3, 2, 3, 2, 3, 1, 1, 2, 1],
'qualify': ['yes', 'no', 'yes', 'no', 'no', 'yes', 'yes', 'no', 'no', 'yes']}

labels = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']
df = pd.DataFrame(exam_data , index=labels)

# Exporting to CSV file


df.to_csv("DF_to_CSV.csv")

# Importing from CSV file


df1 = pd.read_csv('DF_to_CSV.csv',index_col=0)

print(df1)

Output:
name score attempts qualify

a Muskan 12.5 1 yes

b Nidhi 9.0 3 no

c Kanishka 16.5 2 yes

d Jiya NaN 3 no

e Anjali 9.0 2 no

f Manu 20.0 3 yes

g Mehak 14.5 1 yes

h Leena NaN 1 no

i Kavya 8.0 2 no

j Joya 19.0 1 yes

[ ]:

Program 12: Given the school result data for the last five years, analyze
the results on different parameters, e.g., subject wise or class wise.
Source Code:

import pandas as pd
import matplotlib.pyplot as plt
years=[2020,2021,2022,2023,2024]
class_X=[89,92,75,86,98]
class_XII=[74,87,75,90,92]
classes=[class_X,class_XII]
plt.plot(years, class_X,label='class_X')
plt.plot(years,class_XII,label='class_XII')
plt.xlabel('Years')
plt.ylabel('Marks')
plt.legend(loc='upper left')
plt.title("Result Analysis")
plt.show()

Output:
Program 13: Write a program to plot the graph for result analysis based on
different subjects using the Data frame with title and legends.
Source Code:

import pandas as pd
import matplotlib.pyplot as plt
student={"name":['Ilma', 'Astha', 'Mohini', 'Anju'],'Math':[87, 92, 94, 90],'Science': [89, 93, 95,
87],'English': [78, 85, 89, 80]}
df=pd.DataFrame(student)
df.plot(kind="bar",x="name")
plt.xlabel('Students')
plt.ylabel('Marks')
plt.title("Result Analysis")
plt.show()

Output:
Program 14: Take data of weights of 24 people and analyze the data
using the histogram.
Source Code:

import pandas as pd
import matplotlib.pyplot as plt
weight=[20,21,22,23,35,36,41,45,49,47,52,56,55,58,59,65,62,68,71,75,81,95,98,100]
plt.hist(weight,bins=8)
plt.ylabel('Weight')
plt.title("Weight Analysis")
plt.show()

Output:
Program 15: Create a student table with the student id, name, and marks
as attributes where the student id is the primary key.
SQL Command:

Output:
Program 16: Insert the details of a new student in the above table.
SQL Command:

Output:
Program 17: Delete the details of students who are having ‘i’ as the last
character in his/her name from the above table.
SQL Command:

Output:
Program 18: Use the select command to get the details of the
students with marks more than 70.
SQL Command:

Output:
Program 19: Find the min, max, sum, and average of the marks in the student
table.
SQL Command:

Output:
Program 20: Find the total number of customers from each country in
the table (customer ID, customer Name, country) using group by.

Customer table:

SQL Command:

Output:
Program 21: Write a SQL query to order the (student ID, marks) table in
descending order of the marks.
SQL Command:

Output:
Program 22: Write the SQL query to find out day , day name, month name
and year of the date (29/02/2000).

SQL Command:

Output:

DAY("2020-02-29")
29
MONTH("2020-02-29")
2
YEAR("2020-02-29")
2020
DAYNAME("2020-02-29")
Saturday
MONTHNAME("2020-02-29")
February
Program 23: Write the SQL query to find Mod, round and power of a
number.

SQL Command:

Output:
Program-24: Write the SQL query to execute various string functions.

SQL Command:

Output:

You might also like