Class 12 Practical File
Class 12 Practical File
output:
0 a
1 b
2 c
3 d
dtype: object
output:
100 a
101 b
102 c
103 d
dtype: object
import pandas as pd
import numpy as np
data={'a':0.,'b':1.,'c':2.}
s =pd.Series(data)
print (s)
output:
a 0.0
b 1.0
c 2.0
dtype: float64
import pandas as pd
import numpy as np
data={'a':0.,'b':1.,'c':2.}
s =pd.Series(data,index=['b','c','d','a'])
print (s)
output:
b 1.0
c 2.0
d NaN
a 0.0
dtype: float64
output:
0 5
1 5
2 5
3 5
dtype: int64
output:
1
import pandas as pd
s =pd.Series([1,2,3,4,5],index =['a','b','c','d','e'])
#retrieve the first three element
print (s[:3])
output:
a 1
b 2
c 3
dtype: int64
import pandas as pd
s =pd.Series([1,2,3,4,5],index =['a','b','c','d','e'])
#retrieve the last three element
print (s[-3:])
output:
c 3
d 4
e 5
dtype: int64
output:
1
import pandas as pd
s =pd.Series([1,2,3,4,5],index =['a','b','c','d','e'])
#retrieve multiple elements
print (s[['a','c','e']])
output:
a 1
c 3
e 5
dtype: int64
output:
Empty DataFrame
Columns: []
Index: []
output:
0
0 1
1 2
2 3
3 4
4 5
import pandas as pd
data=[['Alex',10],['Bob',12],['Clarke',13]]
df=pd.DataFrame(data,columns=['Name','Age'])
print (df)
output:
Name Age
0 Alex 10
1 Bob 12
2 Clarke 13
output:
Name Age
0 Tom 28
1 Jack 34
2 Steve 29
3 Ricky 42
import pandas as pd
data={'Name':['Tom','Jack','Steve','Ricky'],'Age':[28,34,29,42]}
df=pd.DataFrame(data, index=['rank1','rank2','rank3','rank4'])
print (df)
output:
Name Age
rank1 Tom 28
rank2 Jack 34
rank3 Steve 29
rank4 Ricky 42
df=pd.DataFrame(d)
print (df)
output:
one two
a 1.0 1
b 2.0 2
c 3.0 3
d NaN 4
df=pd.DataFrame(d)
print (df['one'])
output:
a 1.0
b 2.0
c 3.0
d NaN
Name: one, dtype: float64
d ={'one':pd.Series([1,2,3], index=['a','b','c']),
'two':pd.Series([1,2,3,4], index=['a','b','c','d'])}
df=pd.DataFrame(d)
# Adding a new column to an existing DataFrame object with column label by passing new
series
print (df)
output:
Adding a new column by passing as Series:
one two three
a 1.0 1 10.0
b 2.0 2 20.0
c 3.0 3 30.0
d NaN 4 NaN
Adding a new column using the existing columns in DataFrame:
one two three four
a 1.0 1 10.0 11.0
b 2.0 2 20.0 22.0
c 3.0 3 30.0 33.0
d NaN 4 NaN NaN
d ={'one':pd.Series([1,2,3], index=['a','b','c']),
'two':pd.Series([1,2,3,4], index=['a','b','c','d']),
'three':pd.Series([10,20,30], index=['a','b','c'])}
df=pd.DataFrame(d)
print("Our dataframe is:")
print(df)
2020 97
Name: English, dtype: int64
-----------------------
2018 99
2019 97
2020 98
Name: Physics, dtype: int64
-----------------------
2018 100
2019 99
2020 98
Name: Chemistry, dtype: int64
-----------------------
2018 100
2019 100
2020 98
Name: Biology, dtype: int64
-----------------------
2018 100
2019 100
2020 99
Name: Maths, dtype: int64
-----------------------
2018 100
2019 99
2020 96
Name: CS, dtype: int64
0 Sally
1 Anna
2 Ryan
Name: Student_Name, dtype: object
Age
0 15
1 16
2 17
Name: Age, dtype: int64
Class
0 Maths
1 English
2 French
Name: Class, dtype: object
_______________________________________________________________________________
output:
______________________________________________________________________
#20. Show date and temperature in chart using matplotlib.pyplot
#Plotting Temperature against Height
import matplotlib.pyplot as plt
#list storing date in string format
date=["25/12","26/12","27/12"]
#list storing temperature values
temp=[8.5,10.5,6.8]
#create a figure plotting temp versus date
plt.plot(date, temp)
#show the figure
plt.show()
"""Plotting a line chart of date versus temperature by adding Label on X and Y axis, and adding
a Title and Grids to the chart."""
import matplotlib.pyplot as plt
date=["25/12","26/12","27/12"]
temp=[8.5,10.5,6.8]
plt.plot(date, temp)
plt.xlabel("Date") #add the Label on x-axis
plt.ylabel("Temperature") #add the Label on y-axis
plt.title("Date wise Temperature") #add the title to the chart
plt.grid(True) #add gridlines to the background
plt.yticks(temp)
plt.show()
output:
output:
Plotting Histogram Histograms are column-charts, where each column represents a range of values, and
the height of a column corresponds to how many values are in that range.
df=pd.DataFrame(data)
df.plot(kind='hist')
plt.show()
output:
import pandas as pd
marksUT= {'Name':['Raman','Raman','Raman','Zuhaire','Zuhaire','Zuhaire',
'Ashravy','Ashravy','Ashravy','Mishti','Mishti','Mishti'],
'UT':[1,2,3,1,2,3,1,2,3,1,2,3],
'Maths':[22,21,14,20,23,22,23,24,12,15,18,17],
'Science':[21,20,19,17,15,18,19,22,25,22,21,18],
'S.St':[18,17,15,22,21,19,20,24,19,25,25,20],
'Hindi':[20,22,24,24,25,23,24,17,25,25,24,25],
'Eng':[21,24,23,19,15,13,22,21,23,22,23,20]
}
df=pd.DataFrame(marksUT)
print("Max")
print(df.max(numeric_only=True)) #Find out maximum value numeric only
print("Min")
print(df.min(numeric_only=True)) #Find out miminum value
print("Sum")
print(df.sum(numeric_only=True)) #Calculate sum of values
print("Mean")
print(df.mean(numeric_only=True))#Calculate Average Value
print("Median")
print(df.median(numeric_only=True))#Find out middle value
print("Mode")
print(df.mode(numeric_only=True)) #Find out mode value
print("Standand deviation")
print(df.std(numeric_only=True))# Find out standard deviation
print("Describe ")
print(df.describe())
output:
Max
UT 3
Maths 24
Science 25
S.St 25
Hindi 25
Eng 24
dtype: int64
Min
UT 1
Maths 12
Science 15
S.St 15
Hindi 17
Eng 13
dtype: int64
Sum
UT 24
Maths 231
Science 237
S.St 245
Hindi 278
Eng 246
dtype: int64
Mean
UT 2.000000
Maths 19.250000
Science 19.750000
S.St 20.416667
Hindi 23.166667
Eng 20.500000
dtype: float64
Median
UT 2.0
Maths 20.5
Science 19.5
S.St 20.0
Hindi 24.0
Eng 21.5
dtype: float64
Mode
UT Maths Science S.St Hindi Eng
0 1.0 22.0 18 19.0 24.0 23.0
1 2.0 23.0 19 20.0 25.0 NaN
2 3.0 NaN 21 25.0 NaN NaN
3 NaN NaN 22 NaN NaN NaN
Standand deviation
UT 0.852803
Maths 3.980064
Science 2.667140
S.St 3.146667
Hindi 2.443296
Eng 3.370999
dtype: float64
Describe
UT Maths Science S.St Hindi Eng
14
This operator is used to filter records that rely on more than one condition.
15
This operator displays all those records which satisfy any of the conditions separated by OR and give the
output TRUE.
16
The NOT operator is used, when you want to display the records which do not satisfy a
condition.
17
The BETWEEN operator is used, when you want to select values within a given range.
19
The LIKE operator is used in a WHERE clause to search for a specified pattern in a
column of a table. There are mainly two wildcards that are used in conjunction with the
LIKE operator:
% – It matches 0 or more character.
_ – It matches exactly one character.
20
This operator is used for multiple OR conditions. This allows you to specify multiple
values in a WHERE clause.
21
The MIN function returns the smallest value of the selected column in a table.
22
The MAX function returns the largest value of the selected column in a table.
23
The COUNT function returns the number of rows which match the specified criteria.
24
The SUM function returns the total sum of a numeric column that you choose.
25
The AVG function returns the average value of a numeric column that you choose.
27
Aliases are used to give a column/table a temporary name and only exists for a duration of
the query.
28
29
30
31. Write a python program to create database hps and table emp
import mysql.connector as sql
import pandas as pd
con=sql.connect(user='root',password='root',host='localhost')
cur=con.cursor()
cur.execute('create database if not exists raath')
cur.execute('use raath')
cur.execute('create table if not exists emp(code char(4), name varchar(40), salary int)')
cur.execute('show tables')
for i in cur:
print(i)
Output:
con=sql.connect(user='root',password='root',host='localhost')
cur=con.cursor()
cur.execute('create database if not exists hps')
cur.execute('use hps')
cur.execute('create table if not exists emp(code char(4), name varchar(40), salary int)')
output:
33 Write a program to display all records from emp.
import mysql.connector as sql
import pandas as pd
con=sql.connect(user='root',password='root',host='localhost')
cur=con.cursor()
cur.execute('create database if not exists hps')
cur.execute('use hps')