0% found this document useful (0 votes)
73 views21 pages

Kendriya Vidyalaya Ujjain: Submitted By:-Subodhit Chouhan

This document contains 17 Python programs related to data analysis and visualization using Pandas library. The programs create DataFrames and Series from various data, perform operations like selecting, adding, deleting rows and columns, plotting various graphs like bar plot, histogram, line plot etc. using real world datasets on population, survey results, online contests scores etc. Key operations include creating and modifying DataFrames, plotting various charts, reading and writing CSV files.

Uploaded by

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

Kendriya Vidyalaya Ujjain: Submitted By:-Subodhit Chouhan

This document contains 17 Python programs related to data analysis and visualization using Pandas library. The programs create DataFrames and Series from various data, perform operations like selecting, adding, deleting rows and columns, plotting various graphs like bar plot, histogram, line plot etc. using real world datasets on population, survey results, online contests scores etc. Key operations include creating and modifying DataFrames, plotting various charts, reading and writing CSV files.

Uploaded by

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

KENDRIYA VIDYALAYA UJJAIN

Academic year – 2021-22


Informatics Practices(065)
Practical file
Class-12TH

Submitted by:-
Subodhit Chouhan
PROGRAM-1

AIM: write a program to create a Series object using an


Nd-array that has 10 elements at equal distance in the
range 100 to 200?

PROGRAM:

#PROGRAM TO CREATE GIVEN SERIES


import pandas as pd
import numpy as np
n=np.arange(110.0,201.0,10)
s=pd.Series(n)
print(s)

PROGRAM-2
Aim; Write a program to create a Series object using a
dictionary that
stores the number of students in each section of class 12
in your school?

PROGRAM:

#PROGRAM TO CREATE GIVEN SERIES


import pandas as pd
df={'A':39,'B':41,'C':42,'D':44}
s=pd.Series(df)
print(s)

PROGRAM-3
Aim; Consider the series object S that stores the
contribution of each section, as
shown below:

Write code to modify the amount of section ‘A’ as 3400 and


for section ‘C’ and ‘D’ as 5000. Print the changed object.

PROGRAM:
#PROGRAM TO CREATE ORIGINAL SERIES
import pandas as pd
df={'A':4300,'B':6500,'C':3900,'D':6100}
s=pd.Series(df)
print(s)
#MODIFICATION IN SERIES
print(‘program after modification’)
s['A']=3400
s['C']=s['D']=5000
print(s)

PROGRAM-4
Aim; Write a program to create a Data Frame Quarterly Sales
where each row
contains the Quarterly Sales of TV, Freeze and AC. Show the
DataFrame
after deleting details of Freeze.

PROGRAM:
#CREATING DATAFRAME
import pandas as pd
df=pd.DataFrame({‘TV’:
[200000,230000,210000,24000],’FREEZE’:\
[3000000,200000,290000,210000],’AC’:
[240000,153000,245000,170000]},\
index=[‘QTR1’,’QTR2’,’QTR3',’QTR4’])
print(df)
#DATAFRAME AFTER DELETING ‘FREEZE’ COLUMN
print(“DataFrame after removing ‘FREEZE’ column “)
del df[‘FREEZE’]
print(df)

PROGRAM-5

Aim; Write a program to create a DataFrame to store Roll


Number, Name and
Marks of five students. Print the DataFrame and its
transpose.

PROGRAM:
#CREATING GIVEN DATAFRAME
import pandas as pd
df=pd.DataFrame({‘Roll number’:
[101,102,103,104,105],’name’:[‘karan’,’taran’,’piyush’,\
‘bhupinder’,’simran’],’marks’:[82,85,79,86,94]})
print(df)
#TRANSPOSING DATAFRAME
print(‘Dataframe after transpose’)
print(df.T)

PROGRAM-6

Aim; Write a program to create a DataFrame from a CSV


file. Display the shape
(Number of rows and columns) of the CSV file.
PROGRAM:
#CREATING FOLLOEING
DATAFRAME FROM CSV FILE
import pandas as pd
df=pd.read_csv("C:\\CSV.csv")
print(df)
print('Numberof rows:',len(df))
print('Number of
columns:',len(df.columns))

PROGRAM-7

Aim; Name, Height and Weight of 05 students. Create a


CSV file using this
DataFrame.
PROGRAM:
#CREATING CSV FILE USING FOLLOEING DATAFRAME
import pandas as pd
df=pd.DataFrame({‘Roll number’:
[101,102,103,104,105],’name’:[‘karan’,’taran’,’piyush’,\
‘bhupinder’,’simran’],’marks’:[82,85,79,86,94]})
print(df)
df.to_csv(“C:\\student.csv”)

PROGRAM-8

Aim; Given the school result data, analyses the


performance of the students in
Accountancy and Business studies using bar graph.
PROGRAM:
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np

Name=['Anil','Vikas','Arma','Bhupendra','Avnish',
'rajesh']
Acc=[88,82,41,25,50,52]
BST=[97,74,30,62,55,90]
x=np.arange(len(Name))
plt.bar(Name,Acc,label='Accountancy')
plt.bar(x+.2,BST,label='Business studies')
plt.legend(loc='upper right')
plt.show()
PROGRAM-9

Aim; Given datasets population of India and Pakistan in the given


years. Show
the population of India and Pakistan using line chart with title and
labels of
both axis.

PROGRAM:
#CREATING FOLLOWING GRAPH
import matplotlib.pyplot as plt
a=[44.91,58.09,78.07,107.7,138.5,170.6]
b=[449.48,559.51,696.783,870.133,1000.4,1309.1]
x=[a[i]+b[i] for i in range(len(a))]
y=[1960,1970,1980,1990,2000,2010]
plt.xlabel(‘x’)
plt.ylabel(‘y’)
plt.plot(y,x)
plt.title(‘Pakistan India Population till 2010’)
plt.show()
PROGRAM-10

Aim; Write program to plot a bar chart from medals won by four
countries. Make
Sure thet bars are separateky visible
PROGRAM:
#CREATING THE GRAPH
import matplotlib.pyplot as plt
import numpy as np
plt.figure(figsize=(10,7))
info=['Gold','Silver','Bronze','Total']
Australia=[80,59,59,198]
England=[45,45,46,136]
India=[26,20,20,66]
Canada=[15,40,27,82]
x=np.arange(len(info))
plt.bar(info,Australia,width=0.15)
plt.bar(x+.15,England,width=0.15)
plt.bar(x+.30,India,width=0.15)
plt.bar(x+.45,Canada,width=0.15)
plt.show()

PROGRAM-11

Aim; A survey gathers height and weight of 60 participants and


recorded the
Participants’ ages as:
Ages=[1,1,2,3,5,7,8,9,10,10,11,13,13,15,16,17,18,19,20,21,

21,23,24,24,24,25,25,25,25,26,26,26,27,27,27,27,29,30,30,30,30,
31,33,34,34,
34,35,36,36,37,37,37,38,38,39,40,40,41,41]
Write a program to plot a histogram from above data with 20

PROGRAM:
#PROGRAM TO CREATE FOLLOWING HISTOGRAM
import matplotlib.pyplot as plt
Ages =
[1,1,2,3,5,7,8,9,10,10,11,13,13,15,16,17,18,19,20,21,21,23,24,24,24,
25,
25,25,25,26,26,26,27,27,27,27,27,29,30,30,30,30,31,33,34,34,34,
35,36,36,37,37, 37,38,38,39,40,40,41,41]
plt.hist(Ages,bins=20)
plt.title(“participant’s Ages histogram”)
plt.show()

PROGRAM-12

Aim; Write a program to create data series and then


change the indexes of the
Series object in any random order.

PROGRAM:
#CREATING ORIGINAL SERIES
import pandas as pd

s=pd.Series(range(100,501,100),index=['I','J','K','L’
‘M'])
print(‘original series’)
print(s)
#RENAMING INDEXES
print(‘Data series after changing indexes’)
s.index=['K','L','M','J','I']
print(s)

PROGRAM-13

Aim; In an online contest, two 2-player teams’ point in 4


rounds are stored in two
DataFrames as shown below:

Write a program to calculate total points earned by


both the teams in each round.

PROGRAM:

#PROGRAM TO CALCULATE POINT EARNED BY BOTH


TEAM IN EACH ROUND
import pandas as pd
d1={'P1':[700,975,970,900],'P2':[490,460,570,570]}
df1=pd.DataFrame(d1)
print('Performance of team-1')
print(df1)
d2={'P1':[1100,1275,1270,1400],'P2':
[1400,1260,1500,1190]}
df2=pd.DataFrame(d2)
print('Performance of team-2')
print(df2)
print('Points earned by both teams')
print(df1+df2)

PROGRAM-14

Aim; Write a program to create a DataFrame with two


columns name and age.
Add a new column Updated Age that contains age
increased by 10 years.
PROGRAM:
#PROGRAM TO ADD A COLUMN IN DATAFRAME
import pandas as pd
d1={'Name':['Amit','Sumit','Rahul','Dev'],'Age':
[20,22,16,15]}
df1=pd.DataFrame(d1)
print('original dataframe')
print(df1)
print('Dataframe after adding column updated
age')
df1['Updated age']=df1['Age']+10
print(df1)

PROGRAM-15

Aim; Given a DataFrame ( df ) as under:


Write a program to print DataFrame one row at a time.
Also display first two
rows of City column and last two rows of Schools
column.

PROGRAM:

#PROGRAM TO DISPLAY ONE ROW AT


A TIME OF THE GIVEN
DATAFRAME
import pandas as pd
d1={‘city’:
[‘delhi’,’mumbai’,’kolkata’,’chennai’],’scho
ols’:\
[7654,8700,9800,8600]}
df1=pd.DataFrame(d1)
for (r,s) in df1.iterrows():
print(s)
print('-----------------------')
print('*******************')
print('first two rows of city column')
print(df1['city'].head(2))
print('*******************')
print('Last two rows of school
column')
print(df1['schools'].tail(2))

PROGRAM-16

Aim; Write a program to plot a horizontal bar chart from


heights of some students.
PROGRAM:
#CREATING GRAPH
import matplotlib.pyplot as plt
import pandas as pd
d={'Height':[5,5.5,6,5,6],'Weight':
[20,30,15,18,23]

df=pd.DataFrame(d,index(‘Ram’,’Shyam’,’Ra
ma’,’Kashish’,\
‘Mayank’))
df.plot.barh()
plt.ylabel('Names')
plt.xlabel('Height and Weight')
plt.show()
PROGRAM-17

Aim; (i) Create above dataframe name it df1


(ii) Add a new column ‘birthdate’ with all values 5
(iii) Add a new row with label BIKANER with all
value 5
(iv) Delete column population and grade
(v) Delete row UDR and MUMBAI
(vi) Update population of UDR from 3000 to 1111

PROGRAM:

#CREATING DATAFRAMNE

import pandas as pd
d={‘Capital’:[True,False,True,True,False],’Density’:
[7,3,4,8,6],’grade’:[‘A’,’C’,’B’,\
‘A’,’B’],’Income’:[70,30,50,90,60],’population’:
[8000,3000,6000,9000,7000]}

df1=pd.DataFrame(d,index=[‘DELHI’,’UDR’,’JPR’,’MUMBAI’,’I
NDORE’])
print(df1)

#ADDING NEW COLUMN ‘birthdate’ WITH ALL VAUES AS 5

df1['birthdate']=5
print(df1)

#ADDING NEW ROW ’BIKANER’ WITH ALL VALUES AS 5

df1.loc['BIKANER']=5
print(df1)
#DELETING COLUMNS ‘population’ AND ‘grade’

df1.drop(["population","grade"],axis=1)

#DELETING ROW ‘UDR’ AND ‘MUMBAI’

df1.drop(['UDR','MUMBAI'])

#UPDATING POPULATION OF UDR


df1.at['UDR','population']=1111
print(df1)

You might also like