0% found this document useful (0 votes)
34 views15 pages

Programs of Python Pandas

programs of python pandas class 12 chapter 1 and chapter 2 with pyplot
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)
34 views15 pages

Programs of Python Pandas

programs of python pandas class 12 chapter 1 and chapter 2 with pyplot
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/ 15

INFORMATICS PRACTICES

CLASS-12(SCIENCE)
SN PYTHON CODES OF PANDAS PRACTICALLY DEMONSTRATED IN ONLINE
CLASSES:
1 #using NumPy arrays-Creation of array from Pandas Series
import pandas as pd
import numpy as np
series1 = [10,20,30,40] #series1 is a pandas series
n1 =np.array(series1) #n1 is a numpy array
print(series1)
print(n1)
2 #Binary Operations on Data frames
import pandas as pd
student ={'Unit Test-1':[5,6,8,3,10], 'Unit Test-2': [7,8,9,6,15]}
student1 ={'Unit Test-1':[3,3,6,6,8], 'Unit Test-2': [5,9,8,10,5]}
ds =pd.DataFrame(student)
ds1 =pd.DataFrame(student1)
print(ds)
print(ds1)
print("Subtraction")
print(ds.sub(ds1))
print("rsub")
print(ds.rsub(ds1))
print("Addition")
print(ds.add(ds1))
print("radd")
print(ds.radd(ds1))
print("Multiplication")
print(ds.mul(ds1))
print("Division")
print(ds.div(ds1))
3 #Broadcasting using 1-D array
import pandas as pd
a = [[2,5,6],[5,8,9]]
df =pd.DataFrame(a)
print(df)
4 #Broadcasting on two data frames of same sizes
import pandas as pd
a =[2,5,6,7,8]
b =[5,8,9,4,10]
df1 =pd.DataFrame(a)
df2 =pd.DataFrame(b)
print(df1)
print(df2)
5 #Implementing pipelining/chaining by performing addition and division
#operation one after the other using pipe()

import pandas as pd
import numpy as np
import math
#user defined functions

def adder(adder1,adder2):
return adder1+adder2

def divide(adder1,adder2):
return adder1/adder2

#Create a Dictionary of series


d={'science_marks':pd.Series([22,55]),
'english_marks':pd.Series([89,87])}
df=pd.DataFrame(d)
print(df)
df1=df.pipe(adder,5).pipe(divide,2) #Pipelining/Chaining
print(df1)
6 #Comparing two series
import pandas as pd
a =pd.Series([1,2,7,5,4])
b =pd.Series([4,8,9,5,10])
print("First Series is: ")
print(a)
print("Second Series is: ")
print(b)
print("Comparing the elements of both the series ")
print("Equals")
print(a == b)
print("Greater than: ")
print(a > b)
print("Less than: ")
print(a < b)
print("Adding two series :")
c=a+b
print(c)
print("subtracting two series: ")
c=a-b
print(c)
print("Product of two series: ")
c=a*b
print(c)
print("Divide series1 by series2 : ")
c = a/b
print(c)
7 #Concatenating two data frames
import pandas as pd
d1 = {'roll_no': [10,11,12,13,14,15],
'name':['Ankit','Pihu','Rinku','Yash','Vijay','Nikhil']}
df1 =pd.DataFrame(d1, columns =['roll_no','name'])
print(df1)
d2 ={'roll_no': [1,2,3,4,5,6],
'name':['Renu','Jatin','Deep','Guddu','Chhaya','Sahil']}
df2 =pd.DataFrame(d2,columns = ['roll_no','name'])
print(df2)
8 # Maximum , Minimum , Mean, Mode , Median and Count of sales Salesman
wise
import pandas as pd
print("\n")
print ( "Dataframe of Values\n")
monthlysale = {'Salesman':["Rinku", "Jay","Radhika","Arun","Jay",
"Karan","Akshit", "Jay","Karan","Akshit",
"Radhika","Karan"],
'Sales':[1000,300,800,1000,500,60,1000,900,300,1000,900,50],
'Quarter':[1,1,1,2,2,2,3,3,3,4,4,4],
'District':['Kangra','Hamirpur','Kangra','Mandi',
'Hamirpur','Kangra','Kangra','Hamirpur',
'Mandi','Hamirpur','Hamirpur','Kangra'] }
df = pd.DataFrame(monthlysale )
print(df)
# Use of mode() method of DataFrame
print("\n")
print ( "Use of mode() method of DataFrame")
print(df.mode())
print("\n")
print("Use of max,min,mean,median and count\n")
pd.pivot_table(df, index = ['Salesman'], values = ['Sales'],aggfunc=
['max','min','mean','median','count'])
9 #Creating a duplicate csv file with selective columns

import pandas as pd
df =pd.read_csv("E:\\Data\\Employee.csv")
df1 = df.to_csv("E:\\Data\\Emp.csv",columns =["Empid",'Name'])
10 #To illustrate pivot_table() function on the dataframe
#generated using .csv file-Employee.csv

import pandas as pd
df =pd.read_csv("E:\\Data\\Employee.csv",skiprows =1,
names =['E_id','Ename','E_age','Ecity','Esalary'])
print(df)
r=df.pivot_table(df,index=['Ename'],aggfunc="sum")
print(r)
11 #Implementing iteritems()-displaying dataframe contents columnwise
import pandas as pd
total_sales ={2015:{'Qtr1':34500,'Qtr2':45000,'Qtr3':50000,'Qtr4':39000},
2016:{'Qtr1':44500,'Qtr2':65000,'Qtr3':70000,'Qtr4':49000},
2017:{'Qtr1':44500,'Qtr2':65000,'Qtr3':70000,'Qtr4':49000}}
df= pd.DataFrame(total_sales) #Converting data series into Data frame
for (col,colSeries) in df.iteritems(): #displaying column-wise data
print("Column Index :",col)
print("Containing :")
print(colSeries)
12 #To perform all the aggregate and statistical functions in Pandas
import pandas as pd
import numpy as np
#Create a Dictionary of series
d = {'Name':pd.Series(['Sachin','Dhoni','Virat','Rohit','Shikhar']),
'Age':pd.Series([26,25,25,24,31]),
'Score':pd.Series([87,67,89,55,47])}
#Create a DataFrame
df= pd.DataFrame(d)
print("Dataframecontents")
print (df)
print(df.count())
print("count age",df[['Age']].count())
print("sum of score",df[['Score']].sum())
print("minimum age",df[['Age']].min())
print("maximum score",df[['Score']].max())
print("mean age",df[['Age']].mean())
print("mode of age",df[['Age']].mode())
print("median of score",df[['Score']].median())
13 #Creating data frame by passing a list of dictionaries

import pandas as pd
newstudent =[{'Rinku':67,'Ritu':78,'Ajay':75,'Pankaj':88,'Aditya':92},
{'Rinku':77,'Ritu':58,'Ajay':87,'Pankaj':65},
{'Rinku':88,'Ajay':67,'Pankaj':74,'Aditya':70}]

newdf =pd.DataFrame(newstudent)
print(newdf)
14 # Use of Histogram and hist() method
import pandas as pd
import matplotlib.pyplot as plt
print("\n")
print ( "Dataframe of Values\n")
monthlysale ={'Salesman':["Ankur","Pawan","Jiten","Rahul","Pawan",
"Ankur","Karan","Akshit","Mudit","Jiten",
"Karan","Rinku"],
'Sales' : [1000,300,800,900,1000,500,60,1000,900,50,600,450],
'Quarter' :[1,1,1,2,2,2,3,3,3,4,4,4],
'District': ['Wazirabad','Sirmore','Dehradun','Solan',
'Mandi','Kangra','Chail','Dehradun',
'Kangra','Solan','Mandi','Sirmore'] }
df = pd.DataFrame(monthlysale )
df.hist() #hist() by default, creates histogram for all numeric columns
plt.show()
15 #Implementing iterrows()
import pandas as pd
total_sales ={2015:{'Qtr1':34500,'Qtr2':45000,'Qtr3':50000,'Qtr4':39000},
2016:{'Qtr1':44500,'Qtr2':65000,'Qtr3':70000,'Qtr4':49000},
2017:{'Qtr1':44500,'Qtr2':65000,'Qtr3':70000,'Qtr4':49000}}
df= pd.DataFrame(total_sales) #Converting data series into Data frame
for (row,rowSeries) in df.iterrows():
print("RowIndex :",row)
print("Containing :")
print(rowSeries)
16 #Creating data frame from student list
import pandas as pd1
data1 = [['Shreya',20],['Rakshit',22],['Srijan',18]]
df1 = pd1.DataFrame(data1,columns=['Name','Age']) #Defining column
names
#to be displayed as headings
print(df1)
17 #Creating data frame from student list
import pandas as pd1
data1 = [['Shreya',20],['Rakshit',22],['Srijan',18]]
df1 = pd1.DataFrame(data1,columns=['Name','Age']) #Defining column
names
#to be displayed as headings
print(df1)
table =pd1.DataFrame(df1)
print(table)
print("Index Value")
print(table.index)
print("Column Name")
print(table.columns)
18 #Sorting the data in a data frame

import pandas as pd
student_marks =pd.Series({'Vijaya':80,'Rahul':92,'Meghna':67,
'Radhika':95,'Shaurya':97})
student_age =pd.Series({'Vijaya':32,'Rahul':28,'Meghna':30,
'Radhika':25,'Shaurya':20})
student_df =pd.DataFrame({'Marks':student_marks,'Age':student_age})
print(student_df)
#Sorting the data on the basis of marks in ascending order

print(student_df.sort_values(by=['Marks'])) #by keyword defines


#the field on the basis of which the data is to be sorted

print(student_df.sort_values(by=['Marks'],ascending=False)) #Sorted
#in descending order of Marks
19 #Altering dataframe labels

from collections import OrderedDict


from pandas import DataFrame
import pandas as pd
import numpy as np
table = OrderedDict((
("name", ['vishal', 'anil', 'mayur', 'viraj','mahesh']),
('age',[15, 16, 15, 17,16]),
('weight', [51, 48, 49, 51,48]),
('height', [5.1, 5.2, 5.1, 5.3,5.1]),
('runsscored', [55,25, 71, 53,51])
))
d = DataFrame(table)
print("DATA OF DATAFRAME")
print(d)
print("DATA OF DATAFRAME AFTER REINDEX")
df=d.rename(index={0:'a',1:'b'})
print(df)
20 #Data frame as dicitonary

import pandas as pd
df ={'Name':['Rinku','Ritu','Ajay','Pankaj','Aditya'],
'IP':[78,88,98,90,87]}
print(df)
table =pd.Series(df)
table1 =pd.DataFrame(table)
print(table)
print("Index Value")
print(table1.index)
print("Column Name")
print(table1.columns)
21 #Pivoting using pivot() method
#Creating a DataFrame
import pandas as pd
ClassXII_result= {'Name':['Radhika','Sonia','Shaurya','Radhika'],\
'Subject':['IP','CS','Maths','Phy'],\
'Marks':[98,87,99,87],\
'Grade':['A1','A2','A1','B']}
df=
pd.DataFrame(ClassXII_result,columns=['Name','Subject','Marks','Grade'])
print(df)
22 #Illustrating the use of pivot_table() method
import pandas as pd
name_dict = { 'INVIGILATOR' : ["Rajesh",
"Naveen","Anil","Naveen","Rajesh"],\
'AMOUNT' : [550,550,550,550,550], }
df = pd.DataFrame(name_dict )
print(df)
pd.pivot_table(df, index = ['INVIGILATOR'],aggfunc="sum")
23 #Illustrate pivot_table for all the columns with Filtering
import pandas as pd
sale_dict = {'ITEM_NAME':["NOTEBOOK",
"PEN","INKPEN","NOTEBOOK","PEN"],\
'AMOUNT':[100,50,30,100,50],\
'QUANTITY':[2,5,3,3,5] }
df = pd.DataFrame(sale_dict )
print(df)
pd.pivot_table(df, index = ['ITEM_NAME','AMOUNT','QUANTITY'],
aggfunc='sum')
24 #Program on Quantile
import pandas as pd
import numpy as np
df= pd.DataFrame(np.array([[1, 1], [2, 10], [3, 100], [4, 1000]]),columns=['a',
'b'])
print(df)
print(df.quantile(0.5)) #50% quantile or median
25 #To compute variance for the given data set

import numpy as np

dataset= [2,6,8,12,18,24,28,32]

variance= np.var(dataset) #var() method of NumPy is used

print(variance)

26 #Use of aggregate function mean


import pandas as pd
Class12 = {'Name':['SHIVAM', 'ARPIT', 'MERUL', 'SANSKAR', 'HARSHIT',
'ARPIT','VISHAL','DEV', 'RICHA', 'MUDIT', 'ARYAN', 'REYANSH',
'DHRUV'],'Age':[16,18, 15, 16, 17, 15, 16, 14, 16, 15, 17, 16, 16],'Score':[87,
64,89, 58, 74, 34, 77, 87, 64, 45, 68, 92, 93],'Grade' :['A2', 'B2','B1', 'C1', 'B1',
'D', 'B1', 'A2', 'B2', 'C2', 'B2', 'A1', 'A1']}
df = pd.DataFrame(Class12, columns=['Name', 'Age', 'Score', 'Grade'] )
dfpv=df.pivot_table(index='Name',values='Age',aggfunc='mean')
print(dfpv)
27 #use of count function
import pandas as pd
Class12 = {'Name':['SHIVAM', 'ARPIT', 'MERUL', 'SANSKAR', 'HARSHIT',
'ARPIT','VISHAL','DEV', 'RICHA', 'MUDIT', 'ARYAN', 'REYANSH',
'DHRUV'],'Age':[16,18, 15, 16, 17, 15, 16, 14, 16, 15, 17, 16, 16],'Score':[87,
64,89, 58, 74, 34, 77, 87, 64, 45, 68, 92, 93],'Grade' :['A2', 'B2','B1', 'C1', 'B1',
'D', 'B1', 'A2', 'B2', 'C2', 'B2', 'A1', 'A1']}
df = pd.DataFrame(Class12, columns=['Name', 'Age', 'Score', 'Grade'] )
print(df.count())
28 #use of median function
import pandas as pd
Class12 = {'Name':['SHIVAM', 'ARPIT', 'MERUL', 'SANSKAR', 'HARSHIT',
'ARPIT','VISHAL','DEV', 'RICHA', 'MUDIT', 'ARYAN', 'REYANSH',
'DHRUV'],'Age':[16,18, 15, 16, 17, 15, 16, 14, 16, 15, 17, 16, 16],'Score':[87,
64,89, 58, 74, 34, 77, 87, 64, 45, 68, 92, 93],'Grade' :['A2', 'B2','B1', 'C1', 'B1',
'D', 'B1', 'A2', 'B2', 'C2', 'B2', 'A1', 'A1']}
df = pd.DataFrame(Class12, columns=['Name', 'Age', 'Score', 'Grade'] )
print(df.median())
29 #use of mode function
import pandas as pd
Class12 = {'Name':['SHIVAM', 'ARPIT', 'MERUL', 'SANSKAR', 'HARSHIT',
'ARPIT','VISHAL','DEV', 'RICHA', 'MUDIT', 'ARYAN', 'REYANSH',
'DHRUV'],'Age':[16,18, 15, 16, 17, 15, 16, 14, 16, 15, 17, 16, 16],'Score':[87,
64,89, 58, 74, 34, 77, 87, 64, 45, 68, 92, 93],'Grade' :['A2', 'B2','B1', 'C1', 'B1',
'D', 'B1', 'A2', 'B2', 'C2', 'B2', 'A1', 'A1']}
df = pd.DataFrame(Class12, columns=['Name', 'Age', 'Score', 'Grade'] )
print(df.mode())
30 #use of sum function
import pandas as pd
Class12 = {'Name':['SHIVAM', 'ARPIT', 'MERUL', 'SANSKAR', 'HARSHIT',
'ARPIT','VISHAL','DEV', 'RICHA', 'MUDIT', 'ARYAN', 'REYANSH',
'DHRUV'],'Age':[16,18, 15, 16, 17, 15, 16, 14, 16, 15, 17, 16, 16],'Score':[87,
64,89, 58, 74, 34, 77, 87, 64, 45, 68, 92, 93],'Grade' :['A2', 'B2','B1', 'C1', 'B1',
'D', 'B1', 'A2', 'B2', 'C2', 'B2', 'A1', 'A1']}
df = pd.DataFrame(Class12, columns=['Name', 'Age', 'Score', 'Grade'] )
print(df.sum())
31 #use of var function
import pandas as pd
Class12 = {'Name':['SHIVAM', 'ARPIT', 'MERUL', 'SANSKAR', 'HARSHIT',
'ARPIT','VISHAL','DEV', 'RICHA', 'MUDIT', 'ARYAN', 'REYANSH',
'DHRUV'],'Age':[16,18, 15, 16, 17, 15, 16, 14, 16, 15, 17, 16, 16],'Score':[87,
64,89, 58, 74, 34, 77, 87, 64, 45, 68, 92, 93],'Grade' :['A2', 'B2','B1', 'C1', 'B1',
'D', 'B1', 'A2', 'B2', 'C2', 'B2', 'A1', 'A1']}
df = pd.DataFrame(Class12, columns=['Name', 'Age', 'Score', 'Grade'] )
print(df.var())
32 #use of std function
import pandas as pd
Class12 = {'Name':['SHIVAM', 'ARPIT', 'MERUL', 'SANSKAR', 'HARSHIT',
'ARPIT','VISHAL','DEV', 'RICHA', 'MUDIT', 'ARYAN', 'REYANSH',
'DHRUV'],'Age':[16,18, 15, 16, 17, 15, 16, 14, 16, 15, 17, 16, 16],'Score':[87,
64,89, 58, 74, 34, 77, 87, 64, 45, 68, 92, 93],'Grade' :['A2', 'B2','B1', 'C1', 'B1',
'D', 'B1', 'A2', 'B2', 'C2', 'B2', 'A1', 'A1']}
df = pd.DataFrame(Class12, columns=['Name', 'Age', 'Score', 'Grade'] )
print(df.std())
33 #use of pivot function
import pandas as pd
Class12 = {'Name':['SHIVAM', 'ARPIT', 'MERUL', 'SANSKAR', 'HARSHIT',
'VISHAL','DEV', 'RICHA', 'MUDIT', 'ARYAN', 'REYANSH', 'DHRUV'],'Age':[16,
15, 16, 17, 15, 16, 14, 16, 15, 17, 16, 16],'Score':[87, 64, 58, 74, 34, 77, 87,
64, 45, 68, 92, 93],'Grade' :['A2', 'B2', 'C1', 'B1', 'D', 'B1', 'A2', 'B2', 'C2', 'B2',
'A1', 'A1']}
df = pd.DataFrame(Class12, columns=['Name', 'Age', 'Score', 'Grade'] )
print(df)
pv = df.pivot(index='Name', columns='Age', values='Score') # pv is the
pivot table
print(pv)
34 #use of sort function
import pandas as pd
Class12 = {'Name':['SHIVAM', 'ARPIT', 'MERUL', 'SANSKAR', 'HARSHIT',
'VISHAL','DEV', 'RICHA', 'MUDIT', 'ARYAN', 'REYANSH', 'DHRUV'],'Age':[16,
15, 16, 17, 15, 16, 14, 16, 15, 17, 16, 16],'Score':[87, 64, 58, 74, 34, 77, 87,
64, 45, 68, 92, 93],'Grade' :['A2', 'B2', 'C1', 'B1', 'D', 'B1', 'A2', 'B2', 'C2', 'B2',
'A1', 'A1']}
df = pd.DataFrame(Class12, columns=['Name', 'Age', 'Score', 'Grade'] )
print(df)
dfs = df.sort_values(by='Name') # DataFrame sorted in ascending
order by ‘Name’
print(dfs)
35 #sorting in descending order
import pandas as pd
Class12 = {'Name':['SHIVAM', 'ARPIT', 'MERUL', 'SANSKAR', 'HARSHIT',
'VISHAL','DEV', 'RICHA', 'MUDIT', 'ARYAN', 'REYANSH', 'DHRUV'],'Age':[16,
15, 16, 17, 15, 16, 14, 16, 15, 17, 16, 16],'Score':[87, 64, 58, 74, 34, 77, 87,
64, 45, 68, 92, 93],'Grade' :['A2', 'B2', 'C1', 'B1', 'D', 'B1', 'A2', 'B2', 'C2', 'B2',
'A1', 'A1']}
df = pd.DataFrame(Class12, columns=['Name', 'Age', 'Score', 'Grade'] )
print(df)
dfs = df.sort_values(by='Name',ascending=False) # DataFrame sorted in
ascending order by ‘Name’
print(dfs)
36 #sorting by index
import pandas as pd
Class12 = {'Name':['SHIVAM', 'ARPIT', 'MERUL', 'SANSKAR', 'HARSHIT',
'VISHAL','DEV', 'RICHA', 'MUDIT', 'ARYAN', 'REYANSH', 'DHRUV'],'Age':[16,
15, 16, 17, 15, 16, 14, 16, 15, 17, 16, 16],'Score':[87, 64, 58, 74, 34, 77, 87,
64, 45, 68, 92, 93],'Grade' :['A2', 'B2', 'C1', 'B1', 'D', 'B1', 'A2', 'B2', 'C2', 'B2',
'A1', 'A1']}
df = pd.DataFrame(Class12, columns=['Name', 'Age', 'Score', 'Grade'] )
print(df)
dfs=df.sort_index() # DataFrame sorted by index
print(dfs)
37 #writing dataframe to csv file
import pandas as pd
Class12 = {'Name':['SHIVAM', 'ARPIT', 'MERUL', 'SANSKAR', 'HARSHIT',
'VISHAL','DEV', 'RICHA', 'MUDIT', 'ARYAN', 'REYANSH', 'DHRUV'],'Age':[16,
15, 16, 17, 15, 16, 14, 16, 15, 17, 16, 16],'Score':[87, 64, 58, 74, 34, 77, 87,
64, 45, 68, 92, 93],'Grade' :['A2', 'B2', 'C1', 'B1', 'D', 'B1', 'A2', 'B2', 'C2', 'B2',
'A1', 'A1']}
df = pd.DataFrame(Class12, columns=['Name', 'Age', 'Score', 'Grade'] )
print(df)
dfs=df.sort_index(ascending=False)
dfs.to_csv('d:/IP CLASS 12 OL/pandas/grades.csv')
print(dfs)
38 #reading a csv file
import pandas as pd
Class12 = {'Name':['SHIVAM', 'ARPIT', 'MERUL', 'SANSKAR', 'HARSHIT',
'VISHAL','DEV', 'RICHA', 'MUDIT', 'ARYAN', 'REYANSH', 'DHRUV'],'Age':[16,
15, 16, 17, 15, 16, 14, 16, 15, 17, 16, 16],'Score':[87, 64, 58, 74, 34, 77, 87,
64, 45, 68, 92, 93],'Grade' :['A2', 'B2', 'C1', 'B1', 'D', 'B1', 'A2', 'B2', 'C2', 'B2',
'A1', 'A1']}
df = pd.DataFrame(Class12, columns=['Name', 'Age', 'Score', 'Grade'] )
print(df)
dfs=df.sort_index(ascending=False)
dfr=pd.read_csv('d:/IP CLASS 12 OL/pandas/grades.csv')
print(dfr)
39 #use of max function
import pandas as pd
Class12 = {'Name':['SHIVAM', 'ARPIT', 'MERUL', 'SANSKAR', 'HARSHIT',
'ARPIT','VISHAL','DEV', 'RICHA', 'MUDIT', 'ARYAN', 'REYANSH',
'DHRUV'],'Age':[16,18, 15, 16, 17, 15, 16, 14, 16, 15, 17, 16, 16],'Score':[87,
64,89, 58, 74, 34, 77, 87, 64, 45, 68, 92, 93],'Grade' :['A2', 'B2','B1', 'C1', 'B1',
'D', 'B1', 'A2', 'B2', 'C2', 'B2', 'A1', 'A1']}
df = pd.DataFrame(Class12, columns=['Name', 'Age', 'Score', 'Grade'] )
print(df.max())
40 #use of min function
import pandas as pd
Class12 = {'Name':['SHIVAM', 'ARPIT', 'MERUL', 'SANSKAR', 'HARSHIT',
'ARPIT','VISHAL','DEV', 'RICHA', 'MUDIT', 'ARYAN', 'REYANSH',
'DHRUV'],'Age':[16,18, 15, 16, 17, 15, 16, 14, 16, 15, 17, 16, 16],'Score':[87,
64,89, 58, 74, 34, 77, 87, 64, 45, 68, 92, 93],'Grade' :['A2', 'B2','B1', 'C1', 'B1',
'D', 'B1', 'A2', 'B2', 'C2', 'B2', 'A1', 'A1']}
df = pd.DataFrame(Class12, columns=['Name', 'Age', 'Score', 'Grade'] )
print(df.min())
41 #use of describe function
import pandas as pd
S = pd . Series ( [ 5 , 10 , 15 , 20 , 25 ] )
print(S.describe())
42 #making histogram
import pandas as pd
import matplotlib.pyplot as plt
Class12 = {'Name':['SHIVAM', 'ARPIT', 'MERUL', 'SANSKAR', 'HARSHIT',
'ARPIT','VISHAL','DEV', 'RICHA', 'MUDIT', 'ARYAN', 'REYANSH',
'DHRUV'],'Age':[16,18, 15, 16, 17, 15, 16, 14, 16, 15, 17, 16, 16],'Score':[87,
64,89, 58, 74, 34, 77, 87, 64, 45, 68, 92, 93],'Grade' :['A2', 'B2','B1', 'C1', 'B1',
'D', 'B1', 'A2', 'B2', 'C2', 'B2', 'A1', 'A1']}
df = pd.DataFrame(Class12, columns=['Name', 'Age', 'Score', 'Grade'] )
df.hist()
plt.show()
43 #To construct a labelled histogram
import numpy as np
import matplotlib.pyplot as plt
data=[1,11,21,31,41]
plt.hist([5,15,25,35,15,55],bins=[0,10,20,30,40,50,60],weights=[20,10,45,33,
6,8],edgecolor ="red",facecolor="blue")
plt.xlabel('Class Interval', fontsize=10,color='green')
plt.ylabel('Frequency', fontsize=10,color='green')
plt.xticks(fontsize=10, rotation=0,color='blue')
plt.yticks(fontsize=10, rotation=0,color='blue')
plt.title('Histogram',fontsize=24,color='orange')
plt.show()
44 #plotting bar diagram
import pandas as pd
import matplotlib.pyplot as plt
Class12 = {'Name':['SHIVAM', 'ARPIT', 'MERUL', 'SANSKAR', 'HARSHIT',
'ARPIT','VISHAL','DEV', 'RICHA', 'MUDIT', 'ARYAN', 'REYANSH',
'DHRUV'],'Age':[16,18, 15, 16, 17, 15, 16, 14, 16, 15, 17, 16, 16],'Score':[87,
64,89, 58, 74, 34, 77, 87, 64, 45, 68, 92, 93],'Grade' :['A2', 'B2','B1', 'C1', 'B1',
'D', 'B1', 'A2', 'B2', 'C2', 'B2', 'A1', 'A1']}
df = pd.DataFrame(Class12, columns=['Name', 'Age', 'Score', 'Grade'] )
df.plot(kind='bar')
plt.show()
45 #To calculate grades
import pandas as pd
names = pd.Series(['Rohan','Misha','Mike','Simran'])
marks1 = pd.Series([87.0,56.0,91.0,61.0])
marks2 = pd.Series([96.0,76.0,81.0,57.0])
marks3 = pd.Series([92.0,66.0,83.0,60.0])
Stud = {'Name': names,'Subject1':marks1,'Subject2': marks2, 'Subject3':
marks3}
df1 = pd.DataFrame(Stud,columns =
['Name','Subject1','Subject2','Subject3','Total','Grade'])
print("Initial Values in dataframe")
print(df1)
lstTotal =[]
lstMrks = []
for (row, rowSeries) in df1.iterrows():
total = rowSeries['Subject1']+rowSeries['Subject2'] + rowSeries['Subject3']
lstTotal.append(total)
amrks = total/3
if amrks >= 90:
lstMrks.append('A+')
elif amrks >= 70:
lstMrks.append('A')
elif amrks >= 60:
lstMrks.append('B')
elif amrks >=50:
lstMrks.append('C')
elif amrks >= 40:
lstMrks.append('D')
else :
lstMrks.append('F')
df1['Total'] = lstTotal
df1['Grade'] = lstMrks
print("\n Dataframe after calculating grades")
print(df1)

SUBJECT TEACHER- K G UPADHYAY

DATE:11/12/20

You might also like