0% found this document useful (0 votes)
291 views

Practical File

Uploaded by

DAKSHESH KALE
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)
291 views

Practical File

Uploaded by

DAKSHESH KALE
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/ 13

1.

To write a Python program to create a Series to store 5 students Percentage Using


dictionary and print all the elements that are above 75 percentage.

import pandas as pd
dict1={'Ramesh':89,'Suresh':65,'Karman':45,'Simar':95,'Raghav':45}
sr1=pd.Series(dict1)
for i, value in sr1.items():
if value > 75:
print (i, value)

2. To write a Python program to create a Series object that stores the Initial budget
allocated (50000/- each) for the four quarters of the year: Qtr1, Qtr2, Qtr3 and Qtr4.

import pandas as pd
sr1=pd.Series([50000, 50000, 50000, 50000],index=['Qtr1', 'Qtr2', 'Qtr3', 'Qtr4'])
print(sr1)

3. To write a Python program to create a Series object that stores the Section names
('A','B','C') as the index and Contribution made by them respectively (6700,5500,nil) as
values.

import pandas as pd
sr1=pd.Series([6700,5500,0],index=['A', 'B', 'C'])
print(sr1)

4. To Create a program in python to perform following mathematical Operations on Two


Series objects: (i) Addition (ii) Subtraction (iii) Multiplication (iv) Division.
import pandas as pd
seriesA = pd.Series([10,20,30,40,50], index = ['a', 'b', 'c', 'd', 'e'])
seriesB = pd.Series([5,10,15,20,25], index = ['a', 'b', 'c', 'd', 'e'])
A = seriesA + seriesB
S = seriesA - seriesB
M = seriesA * seriesB
D = seriesA / seriesB
print ("Series A")
print (seriesA)
print ("Series B")
print (seriesB)
print("By Adding we get")
print (A)
print("By Subtracting we get")
print (S)
print("By Multiplying we get")
print (M)
print("By Dividing we get")
print (D)

5. To write a Python program to create a Series and perform the following operations
i. to sort the values in descending order
ii. To display records from 2nd record to 5th record
iii. To display 3rd and 5th record only.
iv. To display record on 2nd index position

import pandas as pd
Sr1=pd.Series([56, 78, 34, 23, 81, 15],index=['a', 'b', 'c', 'd', 'e', 'f'])
print ("Sort Values descending order")
print (Sr1.sort_values(ascending=False))
print ("Display records from 2nd record to 5th record")
print (Sr1[1:5])
print ("Display 3rd and 5th record only")
print (Sr1[2:5:2])
print ("Display record on 2nd index position")
print (Sr1[2])

6. To write a Python program to create a Series using list of Marks of 10 students and display
first 5 Students’ marks and Last 2 Students’ marks from Series object.

import pandas as pd
List1=[67, 78, 56, 43, 69, 76, 89, 90, 78, 98, 94]
Marklist=pd.Series(List1)
print ("Marks of first 5 students")
print (Marklist.head(5))
print ("Marks of last 2 students")
print (Marklist.tail(2))
7. To write a Python program to create a panda’s Data Frame for the following table
Using Nested list:

import pandas as pd
List1=[['Arun', 21],['Bala', 23],['Charan', 22]]
Dict1=pd.DataFrame(List1,columns=['Name', 'Age'], index=['Stu1','Stu2','Stu3'])
print (Dict1)

8. To write a Python program to create a panda’s DataFrame called DF for the following
table Using Dictionary of List and perform the following operations:

(i) To Display only column ‘Toys’ from DataFrame DF.


(ii) To Display the row details of ‘AP’ and ‘OD’ from DataFrame DF.
(iii) To Display the column ‘Books’ and ‘Uniform’ for ‘M.P’ and ‘U.P’ from DataFrame
DF.
(iv) To Display consecutive 3 rows and 3 columns from DataFrame DF.

import pandas as pd
dictlist={'Toys':[7916,8508,7226,7617],'Books':[61896,8208,6149,6157],
'Uniform':[610,508,611,457],'Shoes':[8810,6798,9611,6457]}
DF=pd.DataFrame(dictlist, index=['AP','OD','MP','UP'])
print (DF)
print (DF['Toys']) #(i)
print (DF.loc['AP':'OD']) #(ii)
print (DF.loc['MP':'UP','Books':'Uniform']) #(iii)
print (DF.iloc[1:4,1:4]) #(iv)
9. To write a Python program to create a panda’s DataFrame called DF for the following
table Using Dictionary of List and perform the following operations:

(i) Insert a new column “Bags” with values as [5891, 8628, 9785, 4475].
(ii) Delete the row details of M.P from DataFrame DF.

import pandas as pd
dictlist={'Toys':[7916,8508,7226,7617],'Books':[61896,8208,6149,6157],
'Uniform':[610,508,611,457],'Shoes':[8810,6798,9611,6457]}
DF=pd.DataFrame(dictlist, index=['AP','OD','MP','UP'])
print (DF)
DF['Bags']=[5891, 8628, 9785, 4475]#(i)
print (DF)
DF=DF.drop(['MP'])#(ii)
print(DF)

10. To write a Python program to create a panda’s DataFrame called DF for the following
table using Dictionary of List and display the details of students whose Percentage is more
than 85.
import pandas as pd
dictlist={'Stu_Name':['Anu', 'Arun', 'Bala', 'Charan', 'Mano'],
'Degree':['MBA','MCA','M.E','M.Sc','MCA'],
'Percentage':[90,85,91,76,84]}
DF=pd.DataFrame(dictlist, columns=['Stu_Name','Degree','Percentage'],
index=['S1','S2','S3','S4','S5'])

print (DF)
print ()
print ("Student scoring more than 85")
for ind in DF.index:
if DF['Percentage'][ind]>85:
print(DF['Stu_Name'][ind], DF['Degree'][ind], DF['Percentage'][ind])

11. To write a Python program to create a panda’s DataFrame called Students


for the following table and demonstrate iterrows and iteritems.
iterrows
import pandas as pd
dict = {'name':["aparna", "pankaj", "sudhir", "Geeku"],
'degree': ["MBA", "BCA", "M.Tech", "MBA"],
'score':[90, 40, 80, 98]}
df = pd.DataFrame(dict)
print (df)
print ("---------------------------------------------")
# iterating over rows using iterrows() function
for i, j in df.iterrows():
print(i, j)
print()

iteritems
import numpy as np
import pandas as pd
df = pd.DataFrame({'species': ['mammal', 'mammal', 'fish'],
'population': [3948, 4000, 6000]},
index=['tiger', 'fox', 'shark'])
for label, content in df.items():
print('label:', label)
print('content:', content, sep='\n')
12. To write a Python program to plot a Line chart to depict the changing
weekly Onion and Brinjal prices for four weeks. Also, give appropriate axes
labels, title and keep marker style as Diamond and marker edge color as ‘red’
for Onion.
Week1 Week2 Week3 Week4
Onion 25 40 45 50
Brinjal 35 40 30 60
import pandas as pd
import matplotlib.pyplot as plt

Price={
'Onion': pd.Series([25,40,45,50],
index=['Week1', 'Week2', 'Week3', 'Week4']),
'Brinjal': pd.Series([35,40,30,60],
index=['Week1', 'Week2', 'Week3', 'Week4'])}
Dfprice=pd.DataFrame(Price)
print (Dfprice)
Dfprice.plot(kind='line', color=['red','blue'],marker='D')
plt.xlabel("Weeks")
plt.ylabel("Price")
plt.title("Weekly Prices of Onion and Brinjal")
plt.show()
13. To write a Python program to plot a Bar chart for the following table and
keep the width of each bar as 0.25 and specify each bar with different colors.
Also, give proper title and axes labels for the bar chart.

import pandas as pd
import matplotlib.pyplot as plt
Games={'State': pd.Series(['DL','TN','AP','KA']),
'Players': pd.Series([5,11,10,7])}
DF=pd.DataFrame(Games)
print(DF)
DF.plot(kind='bar',x='State',title='Country Players',color=['r','y','g','b'],
linewidth=0.25jnmh)
plt.xlabel('State')
plt.ylabel('Number of Players')
plt.show()

14. To write a Python program to plot a Multiple Bar chart for the following
table. Also, give Appropriate axes labels, title and legend.
Countr Gold Silver Bronze
y
AUS 80 59 59
IND 45 45 46
ENG 26 20 20
import pandas as pd
CAN 15 40 27
import matplotlib.pyplot as plt
Medals={'Gold': pd.Series([80,45,26,15],index=['AUS','IND','ENG','CAN']),
'Silver': pd.Series([59,45,20,40],index=['AUS','IND','ENG','CAN']),
'Bronze': pd.Series([59,46,20,27],index=['AUS','IND','ENG','CAN'])}
df1=pd.DataFrame(Medals)
print(df1)
df1.plot(kind='bar',title='Medals won by different countries',color=['red',
'yellow','purple'],edgecolor='Green',linewidth=2)
plt.ylabel('Number of Medals')
plt.xlabel('Countries')
plt.show()
15. To write a Python program to plot a Histogram for the following class
interval or range. Also, give appropriate axes name, title and edegecolor as
‘red’.

import pandas as pd
import matplotlib.pyplot as plt
data = {'Name':['Arnav', 'Sheela', 'Azhar', 'Bincy', 'Yash','Nazar'],
'Height' : [60,61,63,65,61,60],
'Weight' : [47,89,52,58,50,47]}
df=pd.DataFrame(data)
print(df)
df.plot(kind='hist',edgecolor='red')
plt.show()

You might also like