PYTHON PROGRAM - PRACTICAL FILE PROGRAMS - XII IP - Learnpython4cbse
PYTHON PROGRAM - PRACTICAL FILE PROGRAMS - XII IP - Learnpython4cbse
https://www.learnpython4cbse.com/practical-file-programs-xii-ip 1/16
1/19/24, 6:01 PM PYTHON PROGRAM | PRACTICAL FILE PROGRAMS - XII IP | learnpython4cbse
24.Create a data frame based on e-commerce data and generate descriptive statistics (mean,
median, mode, quartile, and variance)
II. Visualization
25. Given the school result data, analyses the performance of the students on different parameters, e.g
subject wise or class wise.
26. Write a program to plot a bar chart in python to display the result of a school for five
consecutive years.
27. For the Data frames created above, analyze, and plot appropriate charts with title and legend.
Number of Students against Scores in all the 7 subjects
Show the Highest score of each subject
28. For the Data frames created above, analyze, and plot appropriate charts with title and legend.
Show the Average score of each subject
29. For the Data frames created above, analyze, and plot appropriate charts with title and legend.
Number of Females and Males
Average Percentage of Females and Males
30. Take data of your interest from an open-source (e.g. data.gov.in), aggregate and summarize
it. Then plot it using different plotting functions of the Matplotlib library.
CODING:
# create a dictionary
dictionary = {'X' : 10, 'Y' : 20, 'Z' : 30} # create a series
series = pd.Series(dictionary)
print(series)
2. Write a Pandas program to perform arithmetic operations on two Pandas Series. Download
# Write a Pandas program to perform arithmetic operations on two Pandas Series.
import pandas as pd
ds1 = pd.Series([3, 6, 9, 12, 15])
ds2 = pd.Series([2, 4, 6, 8, 10])
ds = ds1 + ds2
print("Add two Series:")
https://www.learnpython4cbse.com/practical-file-programs-xii-ip 2/16
1/19/24, 6:01 PM PYTHON PROGRAM | PRACTICAL FILE PROGRAMS - XII IP | learnpython4cbse
print(ds)
print("Subtract two Series:")
ds = ds1 - ds2
print(ds)
print("Multiply two Series:")
ds = ds1 * ds2
print(ds)
print("Divide Series1 by Series2:")
ds = ds1 / ds2
print(ds)
4. Write a Pandas program to select the rows where the percentage greater than 70. Download
# Write a Pandas program to select the rows where the percentage greater than 70.
import pandas as pd
import numpy as np
exam_data = {'name': ['Aman', 'Kamal', 'Amjad', 'Rohan', 'Amit', 'Sumit',
'Matthew', 'Kartik', 'Kavita', 'Pooja'],
'perc': [79.5, 29, 90.5, np.nan, 32, 65, 56, np.nan, 29, 89],
'qualify': ['yes', 'no', 'yes', 'no', 'no', 'yes', 'yes', 'no', 'no',
'yes']}
labels = ['A', 'B', 'C', 'B', 'E', 'F', 'G', 'H', 'I', 'J']
df = pd.DataFrame(exam_data , index=labels)
print("Number of student whoes percentage more than 70:")
print(df[df['perc'] > 70])
5. Write a Pandas program to select the rows the percentage is between 70 and 90 (inclusive)
Download
# Write a Pandas program to select the rows the percentage is between 70 and 90
(inclusive)
import pandas as pd
import numpy as np
https://www.learnpython4cbse.com/practical-file-programs-xii-ip 3/16
1/19/24, 6:01 PM PYTHON PROGRAM | PRACTICAL FILE PROGRAMS - XII IP | learnpython4cbse
7. Write a Pandas program to join the two given dataframes along rows and assign all data.
Download
# Write a Pandas program to join the two given dataframes along rows and assign
all data.
import pandas as pd
import numpy as np
exam_dic1 = {'name': ['Aman', 'Kamal', 'Amjad', 'Rohan', 'Amit', 'Sumit',
'Matthew', 'Kartik', 'Kavita', 'Pooja'],
'perc': [79.5, 29, 90.5, np.nan, 32, 65, 56, np.nan, 29, 89],
'qualify': ['yes', 'no', 'yes', 'no', 'no', 'yes', 'yes', 'no', 'no',
'yes']}
exam_data1 = pd.DataFrame(exam_dic1)
exam_dic2 = {'name': ['Parveen', 'Ahil', 'Ashaz', 'Shifin', 'Hanash'],
'perc': [89.5, 92, 90.5, 91.5, 90],
'qualify': ['yes', 'yes', 'yes', 'yes', 'yes']}
exam_data2 = pd.DataFrame(exam_dic2)
https://www.learnpython4cbse.com/practical-file-programs-xii-ip 4/16
1/19/24, 6:01 PM PYTHON PROGRAM | PRACTICAL FILE PROGRAMS - XII IP | learnpython4cbse
print("Original DataFrames:")
print(exam_data1)
print("-------------------------------------")
print(exam_data2)
print("\nJoin the said two dataframes along rows:")
result_data = pd.concat([exam_data1, exam_data2])
print(result_data)
8. Write a Pandas program to join the two given dataframes along columns and assign all data.
Download
# Write a Pandas program to join the two given dataframes along columns and assign
all data.
import pandas as pd
import numpy as np
exam_dic1 = {'name': ['Aman', 'Kamal', 'Amjad', 'Rohan', 'Amit', 'Sumit',
'Matthew', 'Kartik', 'Kavita', 'Pooja'],
'perc': [79.5, 29, 90.5, np.nan, 32, 65, 56, np.nan, 29, 89],
'qualify': ['yes', 'no', 'yes', 'no', 'no', 'yes', 'yes', 'no', 'no',
'yes']}
exam_data1 = pd.DataFrame(exam_dic1)
exam_dic2 = {'name': ['Parveen', 'Ahil', 'Ashaz', 'Shifin', 'Hanash'],
'perc': [89.5, 92, 90.5, 91.5, 90],
'qualify': ['yes', 'yes', 'yes', 'yes', 'yes']}
exam_data2 = pd.DataFrame(exam_dic2)
print("Original DataFrames:")
print(exam_data1)
print("-------------------------------------")
print(exam_data2)
print("\nJoin the said two dataframes along rows:")
result_data = pd.concat([exam_data1, exam_data2],axis=1)
print(result_data)
https://www.learnpython4cbse.com/practical-file-programs-xii-ip 5/16
1/19/24, 6:01 PM PYTHON PROGRAM | PRACTICAL FILE PROGRAMS - XII IP | learnpython4cbse
exam_data1 = pd.DataFrame(exam_dic1)
s = pd.Series(['Sukhvir', 54,'yes'], index=['name', 'perc','qualify'])
10. Program to select or filter rows from a DataFrame based on values in columns in pandas.(
Use of Relational and Logical Operators) Download
11. Filter out rows based on different criteria such as duplicate rows Download
https://www.learnpython4cbse.com/practical-file-programs-xii-ip 6/16
1/19/24, 6:01 PM PYTHON PROGRAM | PRACTICAL FILE PROGRAMS - XII IP | learnpython4cbse
'Sales':[8500,4500,9200,8500,9200,9600,8400]}
sales=pd.DataFrame(data)
# Find duplicate rows
duplicated = sales[sales.duplicated(keep=False)]
print("duplicate Row:\n",duplicated)
12. Importing and exporting data between pandas and CSV file.
# 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. Download
import pandas as pd
import csv
#Reading the Data
df = pd.read_csv("student_result.csv")
# Display Name of Columns
print(df.columns)
# Display no of rows and column
print(df.shape)
# Display Column Names and their types
print(df.info())
13. Read the ‘Student_result.csv’ to create a data frame and do the following operation:
# 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. Download
import pandas as pd
import csv
#To display Adm_No, Gender and Percentage from ‘student_result.csv’ file.
df = pd.read_csv("student_result.csv",usecols = ['ADM_NO','GENDER', 'PERCENTAGE'])
print("To display Adm_No, Gender and Percentage from ‘student_result.csv’ file.")
print(df)
#To display first 5 and last 5 records from ‘student_result.csv’ file.
df1 = pd.read_csv("student_result.csv")
https://www.learnpython4cbse.com/practical-file-programs-xii-ip 7/16
1/19/24, 6:01 PM PYTHON PROGRAM | PRACTICAL FILE PROGRAMS - XII IP | learnpython4cbse
print(df1.head())
print(df1.tail())
14. Read the ‘Student_result.csv’ to create a data frame and do the following operation:
# To display Student_result file with new column names.
# To modify the Percentage of student below 40 with NaN value in dataframe. Download
15. Read the ‘Student_result.csv’ to create a data frame and do the following operation:
Download
# To create a duplicate file for ‘student_result.csv’ containing Adm_No, Name and Percentage.
#
# Write
Read the
thestatement in Pandas to find
‘Student_result.csv’ tothe highest
create a percentage
data frameand
andalso
do print the student’s
the following
name and percentage.
operation:
# To create a duplicate file for ‘student_result.csv’ containing Adm_No, Name and
Percentage.
# Write the statement in Pandas to find the highest percentage and also print the
student’s name and percentage.
import pandas as pd
import numpy as np
import csv
# To create a duplicate file for ‘student_result.csv’ containing Adm_No, Name and
Percentage.
df = pd.read_csv("student_result.csv")
https://www.learnpython4cbse.com/practical-file-programs-xii-ip 8/16
1/19/24, 6:01 PM PYTHON PROGRAM | PRACTICAL FILE PROGRAMS - XII IP | learnpython4cbse
df.to_csv('copyStudent_result.csv',columns=
['ADM_NO',"STUDENT'S_NAME","PERCENTAGE"])
# Display Copied Dataframe
df2=pd.read_csv("copyStudent_result.csv")
print(df2)
# find the highest percentage and also print the student’s name and percentage.
df1 = pd.read_csv("student_result.csv")
df1 = df1[["STUDENT'S_NAME",'PERCENTAGE']]
[df1.PERCENTAGE== df1['PERCENTAGE'].max()]
print(df1)
16. Importing and exporting data between pandas and MySQL database Download
https://www.learnpython4cbse.com/practical-file-programs-xii-ip 9/16
1/19/24, 6:01 PM PYTHON PROGRAM | PRACTICAL FILE PROGRAMS - XII IP | learnpython4cbse
17. Find the sum of each column, or find the column with the lowest mean Download
# Find the sum of each column, or find the column with the lowest mean
import pandas as pd
Pass_Perc ={'Phy': {'2017':95.4,'2018':96.4,'2019':99.2,'2020':97.4},
'Che': {'2017':96.5,'2018':97.4,'2019':100,'2020':99.2},
'Maths': {'2017':90.2,'2018':92.6,'2019':97.4,'2020':98.0},
'Eng': {'2017':99.2,'2018':100,'2019':100,'2020':100},
'IP': {'2017':95.6,'2018':100,'2019':100,'2020':100}}
df=pd.DataFrame(Pass_Perc)
print(df)
print()
print('Column wise sum in datframe is :')
print(df.sum(axis=0))
# Print mean vaLue of each coLumn
print()
print('Column wise mean value are:')
print(df.mean(axis=0).round(1))
# Returns CoLumn with minimum mean vaLue
print()
print('Column with minimum mean value is:')
print(df.mean(axis=0).idxmin())
https://www.learnpython4cbse.com/practical-file-programs-xii-ip 10/16
1/19/24, 6:01 PM PYTHON PROGRAM | PRACTICAL FILE PROGRAMS - XII IP | learnpython4cbse
19. Subtract the mean of a row from each element of the row in a Data Frame Download
# Subtract the mean of a row from each element of the row in a Data Frame
import pandas as pd
Pass_Perc ={'Phy': {'2017':95.4,'2018':96.4,'2019':99.2,'2020':97.4},
'Che': {'2017':96.5,'2018':97.4,'2019':100,'2020':99.2},
'Maths': {'2017':90.2,'2018':92.6,'2019':97.4,'2020':98.0},
'Eng': {'2017':99.2,'2018':100,'2019':100,'2020':100},
'IP': {'2017':95.6,'2018':100,'2019':100,'2020':100}}
df=pd.DataFrame(Pass_Perc)
print(df)
print()
print('Mean of each row is:')
print(df.mean(axis=1))
print()
print('Datafranie after Subtracting mean value of\
each row from each element of that Row is:')
print(df.sub(df.mean(axis=1), axis=0))
21. Replace all missing values in a data frame with a 999 Download
https://www.learnpython4cbse.com/practical-file-programs-xii-ip 11/16
1/19/24, 6:01 PM PYTHON PROGRAM | PRACTICAL FILE PROGRAMS - XII IP | learnpython4cbse
df=pd.DataFrame(Srec)
print("\n- Dataframe Before Replacing NaN with 999-\n")
print(df)
#Replace missing value with zeros
print("\n-After Replacing missing value with 999-\n")
df=df.fillna(999)
print(df)
22. Given a Series, print all the elements that are above the 75th percentile. Download
# Given a Series, print all the elements that are above the 75th percentile.
import pandas as pd
import numpy as np
s=pd.Series(np.array([2,4,5,10,18,20,25]))
print(s)
res=s.quantile(q=0.75)
print()
print('75th Percentile of the series is::')
print(res)
print()
print('The elements that above the 75th percentile:')
print(s[s>res])
23. Create a Data Frame quarterly sales where each row contains the item category, item
name, and expenditure. Group the rows by the category and print the total expenditure per
category. Download
# Create a Data Frame quarterly sales where each row contains the item category,
#item name, and expenditure. Group the rows by the category and print the total
#expenditure per category.
import pandas as pd
# initialize list of lists
data = [['CAR','Maruti',1000000],['AC','Hitachi',55000],
['AIRCOLLER','Bajaj',12000],
['WASHING MACHINE','LG',15000],['CAR','Ford',7000000],['AC','SAMSUNG',45000],
['AIRCOLLER','Symphony',20000],['WASHING MACHINE','Wirlpool',25000]]
Col=['itemcat','itemname','expenditure']
# Create the pandas DataFrame
qrtsales = pd.DataFrame(data,columns=Col)
# print dataframe.
print (qrtsales)
qs=qrtsales.groupby('itemcat')
print('Result after Filtering Dataframe')
print(qs['itemcat','expenditure'].sum())
https://www.learnpython4cbse.com/practical-file-programs-xii-ip 12/16
1/19/24, 6:01 PM PYTHON PROGRAM | PRACTICAL FILE PROGRAMS - XII IP | learnpython4cbse
24. Create a data frame based on e-commerce data and generate descriptive statistics (mean,
median, mode, quartile, and variance) Download
# Create a data frame based on ecommerce data and generate descriptive statistics
# (mean, median,mode, quartile, and variance)
import pandas as pd
sales = {'InvoiceNo': [1001,1002,1903,1004,1085,1006,1007],
'ProductName': ['LCD','AC','Deodrant','leans','Books','Shoes','Jacket'],
'Quantity': [2,1,2,1,2,1,1],
'Price':[65000,55000,500,3000,958,3000,2200]}
df=pd.DataFrame(sales)
print(df)
print("Mean price of Item:", df['Price']. mean ().round (2))
print("Median price of Item:", df['Price']. median ().round (2))
print("Mode of price:\n", df[['Price']]. mode ())
print("Quartile of price:\n",df[['Price']].quantile([.1,.25,.5,.75],axis=0))
print("Variance of Price:\n",df[['Price']].var())
25. Given the school result data, analyses the performance of the students on different
parameters, e.g subject wise or class wise. Download
# Given the school result data, analyses the performance of the students on
#different parameters, e.g subject wise or class wise.
# x-axis is shows the subject and y -axis
# shows the markers in each subject
# import pandas and matplotlib
import pandas as pd
import matplotlib.pyplot as plt
# Simple Line Chart with setting of Label of X and Y axis,
# title for chart line and color of line
subject = ['Physic','Chemistry','Mathematics', 'Biology','Computer']
marks =[80,75,70,78,82]
# To draw line in red colour
plt.plot(subject,marks,'r',marker ='*')
# To Write Title of the Line Chart
plt.title('Marks Scored')
# To Put Label At Y Axis
plt.xlabel('SUBJECT')
# To Put Label At X Axis
plt.ylabel('MARKS')
plt.show()
26. Write a program to plot a bar chart in python to display the result of a school for five
consecutive years. Download
https://www.learnpython4cbse.com/practical-file-programs-xii-ip 13/16
1/19/24, 6:01 PM PYTHON PROGRAM | PRACTICAL FILE PROGRAMS - XII IP | learnpython4cbse
#Write a program to plot a bar chart in python to display the result of a school
for five consecutive years.
import matplotlib.pyplot as pl
year=['2015','2016','2017','2018','2019'] # list of years
p=[98.50,70.25,55.20,90.5,61.50] #list of pass percentage
j=['b','g','r','m','c'] # color code of bar charts
pl.bar(year, p, width=0.2, color=j) # bar( ) function to create the bar chart
pl.xlabel("year") # label for x-axis
pl.ylabel("Pass%") # label for y-axis
pl.show( ) # function to display bar chart
27. For the Data frames created above, analyze, and plot appropriate charts with title and legend.
• Number of Students against Scores in all the 7 subjects
• Show the Highest score of each subject Download
# For the Data frames created above, analyze, and plot appropriate charts with #
#title and legend.
#• Number of Students against Scores in all the 7 subjects
#• Show the Highest score of each subject
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
import csv
df = pd.read_csv("student_result.csv")
#Number of Students against Scores in all the 7 subjects
plt.hist([df['ENG'],df['HINDI'],df['MATHS'],df['SCIENCE'],df['SSC'],df['SANSK'],df
['CA']],color=['red', 'yellow', 'blue','green','orange','black','pink'])
plt.title('Number of Students against Scores')
plt.xlabel('Score')
plt.ylabel('Number of Students')
plt.legend(['English', 'Hindi', 'Maths','Science','S.Sc.','Sanskrit','CA'])
plt.show()
# Show the Highest score of each subject.
y = ['ENGG','HINNDI','MATHS','SCIENCE','SSC','SANSK','CA']
width =
[df['ENG'].max(),df['HINDI'].max(),df['MATHS'].max(),df['SCIENCE'].max(),df['SSC']
.max(),df['SANSK'].max(),df['CA'].max()]
plt.figure(figsize = (12,2))
plt.barh(y = y, width = width)
plt.title('Average Scores')
plt.xlabel('Average Score')
plt.ylabel('Subjects')
for i,v in enumerate(width):
plt.text(v, i, " "+str(round(v,2)), color='blue', va='center',
fontweight='bold')
https://www.learnpython4cbse.com/practical-file-programs-xii-ip 14/16
1/19/24, 6:01 PM PYTHON PROGRAM | PRACTICAL FILE PROGRAMS - XII IP | learnpython4cbse
plt.show()
28. For the Data frames created above, analyze, and plot appropriate charts with title and
legend.
• Show the Average score of each subject Download
# For the Data frames created above, analyze, and plot appropriate charts with
title and legend.
# • Show the Average score of each subject
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
import csv
df = pd.read_csv("student_result.csv")
# Show the Average score of each subject
y = ['ENGG','HINNDI','MATHS','SCIENCE','SSC','SANSK','CA']
width =
[df['ENG'].mean(),df['HINDI'].mean(),df['MATHS'].mean(),df['SCIENCE'].mean(),
df['SSC'].mean(),df['SANSK'].mean(),df['CA'].mean()]
plt.figure(figsize = (12,2))
plt.barh(y = y, width = width)
plt.title('Average Scores')
plt.xlabel('Average Score')
plt.ylabel('Subjects')
for i,v in enumerate(width):
plt.text(v, i, " "+str(round(v,2)), color='blue', va='center',
fontweight='bold')
plt.show()
29. For the Data frames created above, analyze, and plot appropriate charts with title and
legend.
• Number of Females and Males
• Average Percentage of Females and Males Download
# For the Data frames created above, analyze, and plot appropriate charts
# with title and legend.
# • Number of Females and Males
# • Average Percentage of Females and Males
https://www.learnpython4cbse.com/practical-file-programs-xii-ip 15/16
1/19/24, 6:01 PM PYTHON PROGRAM | PRACTICAL FILE PROGRAMS - XII IP | learnpython4cbse
df_gender = df.groupby('GENDER')
#Number of Females and Males
y = df_gender['GENDER'].count().keys()
width = df_gender['GENDER'].count()
plt.figure(figsize = (12,2))
plt.barh(y = y, width = width)
plt.title('No. of Females and Males')
plt.xlabel('Count')
plt.ylabel('Gender')
for i,v in enumerate(width):
plt.text(v, i, " "+str(v), color='blue', va='center', fontweight='bold')
plt.show()
#Average Percentage of Females and Males
y = df_gender['PERCENTAGE'].mean().keys()
width = df_gender['PERCENTAGE'].mean()
plt.figure(figsize = (12,2))
plt.barh(y = y,
width = width)
plt.title('Av Percentage of Female and Males')
plt.xlabel('Av. total Percentage ')
plt.ylabel('Gender')
for i,v in enumerate(width):
plt.text(v, i, " "+str(round(v,2)), color='blue', va='center',
fontweight='bold')
This site is visited
plt.show() About us Contact us - [email protected]
WhatsApp - +918076665624
feedback
Phone No - +918076665624
https://www.learnpython4cbse.com/practical-file-programs-xii-ip 16/16