Python Programs
Python Programs
0 with an
increment of 2.5.
import pandas as pd
import numpy as np
n=np.arange(41,60,2.5)
s=pd.series(n)
print(s)
Output:
2 46.0
3 48.5
4 51.0
5 53.5
6 56.0
[2] Write a program to generate a series of 10 numbers with a scalar value of 44.
import pandas as pd
print(pd.Series(44,range(1,11)))
Output:
1 44
2 44
3 44
4 44
5 44
6 44
7 44
8 44
9 44
10 44
dtype: int64
[3] Create a panda’s series from a dictionary of values and a ndarray.
import pandas as pd
#creating series from a dictionary
d={'Jan':31,'Feb':28,'Mar':31,'Apr':30}
s=pd.series(d)
print('Series from dictionary')
print('~~~~~~~~~~~~~~~~~~~~~~~~~')
print(s)
#Creating series from an ndarray
ar=np.array([2,3,4,5,6])
print('\nSeries from ndarray')
print('~~~~~~~~~~~~~~~~~~~~~~~~')
s1=pd.Series(ar)
print(s1)
Output
import pandas as pd
std_marks=[]
for i in range(1,6):
m=int(input('Enter the Percentile:'))
std_marks.append(m)
s=pd.Series(index=range(1201,1206),data=std_marks)
print('Data fetched from the series are:')
print(s[s>=75])
Output:
1202 78
1204 88
1205 83
dtype: int64
[5] Create a data frame for examination results and display roe labels, column labels data
types of each column and the dimensions.
import pandas as pd
res={'Amit':[76,78,75,66,68],
'Shialesh':[78,56,77,49,55],
'Rani':[90,91,93,97,99],
'Madan':[55,48,59,60,66],
'Radhika':[78,79,85,88,86]}
df=pd.DataFrame(res)
print("Printing row labels in a list:")
print("~~~~~~~~~~~~~~~~~~~~~~~")
idx=df.index
l=list(idx)
print(l)
print("Printing row labels in a list:")
print("~~~~~~~~~~~~~~~~~~~~~~")
print("[",end=" ")
for col in df.columns:
print(col,end=" ")
print("]")
print("Printing Data Types of each column")
print("~~~~~~~~~~~~~~~~~~~~~~~~")
print(df.dtypes)
print("Printing dimensions of Data Frame")
print("~~~~~~~~~~~~~~~~~~~~~~~~~")
print(df.ndim)
Printing row labels in a list:
~~~~~~~~~~~~~~~~~~~~~~~
[0, 1, 2, 3, 4]
~~~~~~~~~~~~~~~~~~~~~~
[ Amit ]
~~~~~~~~~~~~~~~~~~~~~~~~
Amit int64
Shialesh int64
Rani int64
Madan int64
Radhika int64
dtype: object
~~~~~~~~~~~~~~~~~~~~~~~~~
[6] Create a dataframe and iterate them over rows.
import pandas as pd
data=[["virat",55,66,31],["rohit",88,66,43],["hardik",99,101,68]]
players=pd.DataFrame(data,columns=["Name","Match-1","Match-2","Match-3"])
print("Iterating by rows:")
print("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
for index, rows in players.iterrows():
print(index,rows.values)
print("Iterating by columns:")
print("~~~~~~~~~~~~~~~~~~~~~~~~~~~")
for index, rows in players.iterrows():
print(index,row["Name"],row["Match-1"],
row["Match-2"],row["Match-3"])
output:
Iterating by rows:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
0 [ ‘virat’ 55 66 31]
1 [‘rohit’ 88 66 43]
Iterating by columns:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
0 virat 55 66 31
1 rohit 88 66 43
2 hardik 99 101 68
[7] Create a dataframe and print it along with their index using iteritems().
import pandas as pd
df=pd.DataFrame(sc_4yrs)
print(df)
print("-----------------------------------------")
print("Year:",year)
print(runs)
OUTPUT:
Year: 2016
ViratKohli 2595
ShikharDhawan 2378
Year: 2017
ViratKohli 2818
ShikharDhawan 2295
Year: 2018
ViratKohli 2735
ShikharDhawan 2378
import pandas as pd
#Creating DataFrame
d={2018:[110,130,115,118],
2019:[205,165,175,190],
2020:[115,206,157,179],
2021:[118,198,183,169]}
sales=pd.DataFrame(d,index=['Kapil','Kamini','Shikhar','Mohini'])
print("Row Labels:\n",sales.index)
print("~~~~~~~~~~~~~~~~~~~~~~~~~~\n")
print("Column Labels:\n",sales.columns)
print("~~~~~~~~~~~~~~~~~~~~~~~~~~\n")
print(sales.dtypes)
print("~~~~~~~~~~~~~~~~~~~~~~~~~\n")
print("Dimensions:",sales.ndim)
print("Shape:",sales.shape)
print("Size:",sales.size)
print("Values:”,sales.values)
OUTPUT:
Row Labels:
~~~~~~~~~~~~~~~~~~~~~~~~~~
Column Labels:
~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~~
2018 int64
2019 int64
2020 int64
2021 int64
dtype: object
~~~~~~~~~~~~~~~~~~~~~~~~~
Dimensions: 2
Shape: (4, 4)
Size: 16
import pandas as pd
#creating dataframe
d={2018:[110,130,115,118],
2019:[205,165,175,190],
2020:[115,206,157,179],
2021:[118,198,183,169]}
sales = pd.DataFrame(d,index=["Kapil","kamini","shikhar","mohini"])
print("display last two rows of dataframe:")
print("-------------------------------------")
#method 1
print("using tail function:")
print(sales.tail(2))
#method 2
print("using iloc")
print(sales.iloc[-2:])
#with specific columns,I have printed two columns
print("specific columns")
print(sales.iloc[-2:,-2:])
print("display first two columns of dataframe:")
print("---------------------------------------------")
print(sales[sales.columns[0:2]])
Output:
Using iloc
2018 2019
Kapil 110 205
Kamini 130 165
Shikhar 115 175
Mohini 118 190
[10] Use above (Question 9) dataframe and do the following:
import pandas as pd
#creating Dataframe
d={2018:[110,130,115,118],
2019:[205,165,175,190],
2020:[115,206,157,179],
2021:[118,198,183,169]}
sales=pd.Dataframe(d,index=['kapil','kamini','shikhar','mohini'])
print("Transpose:")
print('----------------------------------------')
print(sales.T)
print("\nsales made by each salesman in 2018")
print(sales.loc[:,2018])
print("sales made by kapil and mohini:")
print(sales.loc[['kapil','mohini'],[2019,2020]])
print("add data:")
sales.loc["Nirali"]=[221,178,165,177]
print(sales)
print("Delete Data for 2018:")
sales=sales.drop(columns=2018)
print(sales)
sales.drop("kinshuk")
print(sales)
sales=sales.rename({"kamini":"rani","kapil":"anil"},axis="index")
print(sales)
sales.loc[sales.index=="Mohini",2018]=150
print(sales)
Output
Transpose:
Kapil kamini shikhar mohini
2018 110 130 115 118
2019 205 165 175 190
2020 115 206 157 179
2021 118 198 183 169
Sales made by each salesman in 2018
Kapil 110
Kamini 130
Shikhar 115
Mohini 118
Name: 2018, dtype: int64
import matplotlib.pyplot as pp
mon=['january','february','march','april','may']
sales=[510,350,475,580,600]
pp.plot(mon,sales,label='sales',color='b',linestyle='dashed',marker='D')
pp.xlabel("months")
pp.ylabel("sales")
pp.legend()
pp.show()
Output:
[12] Pratyush garments has recorded the following data into their register for their
income from cotton clothes and jeans . Plot them on the line chart.
import matplotlib.pyplot as pp
day=['mon','tues','wed','thurs','fri']
ct=[450,560,400,605,580]
js=[490,600,425,610,625]
pp.plot(day,ct,label='cotton',color='g',linestyle='dotted',marker='+')
pp.plot(day,js,label='food',color='m',linestyle='dashdot',marker='x')
pp.xlabel("days")
pp.ylabel("orders")
pp.legend()
pp.show()
OUTPUT:
[13].Observe the given data for monthly views of one of the youtube channels for six
months . Plot them on the line chart.
3. Display legends
6. Use dot marker with blue edge color and black fill color.
import matplotlib.pyplot as pp
mon=['jan','feb','march','april','may','june']
views=[2500,2100,1700,3500,3000,3800]
pp.plot(mon,views,label='views
state',color='r',linestyle='dashed',linewidth=4,marker='o',markerfacecolor='k',markeredgecolor='
b')
pp.title('youtube stats')
pp.xlabel('months')
pp.ylabel('views')
pp.legend()
pp.show()
OUTPUT:
14. Write a program to plot a range from 1to 30 with step value 4. Use the following
algebraic Exp y =5*x+2
import matplotlib.pyplot as pp
import numpy as np
x=np.arange(1,30,4)
y=5*x+2
pp.plot(x,y)
pp.show()
output
15. Display following bowling figure through bar chart :
Overs Runs
1 6
2 18
3 10
4 5
import matplotlib.pyplot as pp
overs=[1,2,3,4]
runs=[6,18,10,5]
pp.bar(overs,runs,color="m")
pp.xlabel("overs")
pp.ylabel("runs")
pp.show()
Output:
INDEX
PYTHON PROGRAMS
S.NO PRACTICAL SIGNATURE
1 Write a program to generate a series of float number from 41.0
to 60.0 with an increment of 2.5 each .
2 Write a program to generate a series of 10 numbers with a
scalar value of 44.
3 Create a pandas series from dictionary of values and a ndarray.
4 Given a series , print all the elements that are above the 75 th
percentile.
5 Create a dataframe for examination results and display row
labels , column labels datatypes of each columns and the
dimensions .
6 Create a dataframe and iterate them over rows.
7 Create a dataframe and print it along with their index using
iteritems().
8 Create the following dataframe sales containing year wise
sales figures for five sales persons in INR.Use the years as
column labels and sales person names.
Overs Runs
1 6
2 18
3 10
4 5
ACKNOWLEDGEMENT
I wish to express my deep sense of gratitude and indebtedness to our learned teacher Ms.
PREETI, PGT(INFORMATICS PRACTICES), INDIAN MODERN SENIOR
SECONDARY SCHOOL for her valuable help , advice and guidance in the preparation of
this practical file.
I am also greatly indebted to our principal Mr. RAJENDER GAHLYAN and school
authorities for providing me with the facilities and requisite laboratory conditions for making
this practicle file.
I also extend my thanks to a number of teachers, my classmates and friends who helped
me to complete this practical file successfully.
CERTIFICATE
This is to certify that ____________ student of Class XII, INDIAN MODERN SENIOR
SECONDARY SCHOOL has completed the PRACTICAL FILE during the academic year
2023-24 towards partial fulfillment of credit for the Informatics Practices practical evaluation
of CBSE and submitted satisfactory report, as compiled in the following pages , under my
supervision.
Signature Signature