Practical File Class - Xii Informatics Practices (New) : 1. How To Create A Series From A List, Numpy Array and Dict?

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 17

PRACTICAL FILE

CLASS – XII
Informatics Practices (New)

1. How to create a series from a list, numpy array and dict?


# Inputs
import numpy as np
mylist = list('abcedfghijklmnopqrstuvwxyz')
myarr = np.arange(26)
mydict = dict(zip(mylist, myarr))
Solution:
ser1 = pd.Series(mylist)
ser2 = pd.Series(myarr)
ser3 = pd.Series(mydict)
print(ser3.head())
Output

a 0

b 1

c 2

d 3

e 4
2. How to combine many series to form a dataframe?
# Input
import numpy as np
ser1 = pd.Series(list('abcedfghijklmnopqrstuvwxyz'))
ser2 = pd.Series(np.arange(26))

# Solution-1
df = pd.concat([ser1, ser2], axis=1)

# Solution-2
df = pd.DataFrame({'col1': ser1, 'col2': ser2})
print(df.head())

col1 col2
0 a 0
1 b 1
2 c 2
3 e 3
4 d 4

3. How to get the items of series A not present in series B?


# Input
ser1 = pd.Series([1, 2, 3, 4, 5])
ser2 = pd.Series([4, 5, 6, 7, 8])
Solution:
ser1[~ser1.isin(ser2)]
Output :
0 1
1 2
2 3
dtype: int64

4. How to get the minimum, 25th percentile, median, 75th, and max of a numeric series?
# Input
state = np.random.RandomState(100)
ser = pd.Series(state.normal(10, 5, 25))
Solution:
np.percentile(ser, q=[0, 25, 50, 75, 100])
Output
array([ 1.39267584, 6.49135133, 10.2578186 , 13.06985067, 25.80920994])
 5. How to convert a numpy array to a dataframe of given shape? (L1)

# Input

ser = pd.Series(np.random.randint(1, 10, 35))

Solution:

df = pd.DataFrame(ser.values.reshape(7,5))

print(df)

Output :

0 1 2 3 4

0 1 2 1 2 5

1 1 2 4 5 2

2 1 3 3 2 8

3 8 6 4 9 6
4 2 1 1 8 5

5 3 2 8 5 6

6 1 5 5 4 6

6. How to get the day of month, week number, day of year and day of week from a series of
date strings?
# Input
ser = pd.Series(['01 Jan 2010', '02-02-2011', '20120303', '2013/04/04', '2014-05-05', '2015-
06-06T12:20'])
Solution:
from dateutil.parser import parse
ser_ts = ser.map(lambda x: parse(x))

# day of month
print("Date: ", ser_ts.dt.day.tolist())

# week number
print("Week number: ", ser_ts.dt.weekofyear.tolist())

# day of year
print("Day number of year: ", ser_ts.dt.dayofyear.tolist())

# day of week
print("Day of week: ", ser_ts.dt.weekday_name.tolist())

Date: [1, 2, 3, 4, 5, 6 ]
We number: [53, 5, 9, 14, 19, 23]
Day num of year: [1, 33, 63, 94, 125, 157]
Day of week: ['Friday', 'Wednesday', 'Saturday', 'Thursday', 'Monday', 'Saturday']

7. How to extract all numbers between a given range from a numpy array?
( Get all items between 5 and 10 from  a)
Input:
a = np.array([2, 6, 1, 9, 10, 3, 27])
Desired Output:
(array([6, 9, 10]),)
Solution:
a = np.arange(15)

# Method 1
index = np.where((a >= 5) & (a <= 10))
a[index]

# Method 2:
index = np.where(np.logical_and(a>=5, a<=10))
a[index]
#> (array([6, 9, 10]),)

# Method 3: (thanks loganzk!)


a[(a >= 5) & (a <= 10)]

8. How to swap two columns in a 2d numpy array?


Solution:
# Input
arr = np.arange(9).reshape(3,3)
arr
arr[:, [1,0,2]]
#> array([[1, 0, 2],
#> [4, 3, 5],
#> [7, 6, 8]])

9.How to swap two rows in a 2d numpy array?


Solution:
# Input
arr = np.arange(9).reshape(3,3)

arr[[1,0,2], :]
#> array([[3, 4, 5],
#> [0, 1, 2],
#> [6, 7, 8]])

10. How to print only 3 decimal places in python numpy array?


Solution:
# Input
rand_arr = np.random.random((5,3))

# Create the random array


rand_arr = np.random.random([5,3])

# Limit to 3 decimal places


np.set_printoptions(precision=3)
rand_arr[:4]
#> array([[ 0.443, 0.109, 0.97 ],
#> [ 0.388, 0.447, 0.191],
#> [ 0.891, 0.474, 0.212],
#> [ 0.609, 0.518, 0.403]])

11. Write a Program in Pandas to create series using pre-defined array/ create series using user-defined
array/list/ create series using pre-defined list/create Series using Predefined Dictionary/create series using
User-defined Dictionary/ change index in series/print head and tail elements/print according to index
position and condition in python.”””

import pandas as pd

'''#creating series using pre-defined array

data=['a','b','c']

s=pd.Series(data)

print(s)

#creating series using user-defined array/list

#creating an array

ar1=list()

n=int(input("Enter the values for an array"))

print("Enter numbers")

for i in range(0,n):

num=int(input("num:"))

ar1.append(num)

s=pd.Series(ar1)

print(s)

#creating series using pre-defined list

list=['a','b','c']

s=pd.Series(list)

print(s)

list=[[0,1,2,3],['a','b','c'],["vedant","purnendu","rupali"]]
s=pd.Series(list)

print(s)'''

#creating Series using Predefined Dictionary

dic=({'rupali':[9826386977,'[email protected]'], 'purnendu':[9826911972,'[email protected]'],
'vedant':[788990,'[email protected]']})

s=pd.Series(dic)

print (s)

#creating series using User-defined Dictionary

key=input("Enter the Key")

value=int(input("enter the value"))

dict[key]=value

s=pd.Series(dict)

print (s)

#change index in series

s=pd.Series(data,index=[1,2,3])

print (s)

#printing head and tail elements

print(s.head(2)) #displays first 2 elements

print(s.tail(1)) #displays last 1 elements'''

#printing according to index position and condition

print(s[1])

print("According to Condition")

print(s[s==9826386977])

12. Write a Program to enter data and show data in python using dataFrames and pandas.
import pandas as pd

data = [['Rajiv',10],['Sameer',12],['Kapil',13]]

df = pd.DataFrame(data,columns=['Name','Age'])

print (df)

data1 = {'Name':['Rajiv', 'Sameer', 'Kapil', 'Nischay'],'Age':[28,34,29,42], 'Designation':


['Accountant','Cashier','Clerk','Manager']}

df1 = pd.DataFrame(data1)

print (df1)

13. Write a Program to enter multiple values based data in multiple columns/rows and show that data
in python using dataFrames and pandas.’’’

import pandas as pd

weather_data={

'day':['01/01/2018','01/02/2018','01/03/2018','01/04/2018','01/05/2018','01/01/2018'],

'temperature':[42,41,43,42,41,40],

'windspeed':[6,7,2,4,7,2],

'event':['Sunny','Rain','Sunny','Sunny','Rain','Sunny']

df=pd.DataFrame(weather_data)

print(df)

print("Number of Rows and Columns")

print(df.shape)

print(df.head())

print("Tail")

print(df.tail(2))

print("Specified Number of Rows")

print(df[2:5])

print("Print Everything")

print(df[:])
print("Print Column Names")

print(df.columns)

print("Data from Individual Column")

print(df['day']) #or df.day

print(df['temperature'])

print("Maximum Temperature : ", df['temperature'].max())

print("Printing According to Condition")

print(df[df.temperature>41])

print("Printing the row with maximum temperature")

print(df[df.temperature==df.temperature.max()])

print("Printing specific columns with maximum temperature")

print(df[['day','temperature']][df.temperature==df.temperature.max()])

print("According to index")

print(df.loc[3])

print("Changing of Index")

df.set_index('day',inplace=True)

print(df)

print("Searching according to new index")

print(df.loc['01/03/2018'])

print("Resetting the Index")

df.reset_index(inplace=True)

print(df)

print("Sorting")

print(df.sort_values(by=['temperature'],ascending=False))

print("Sorting on Multiple Columns")

print(df.sort_values(by=['temperature','windspeed'],ascending=True))

print("Sorting on Multiple Columns one in ascending, another in descending")

print(df.sort_values(by=['temperature','windspeed'],ascending=[True,False]))

print("Sum Operations on Data Frame")


print(df['temperature'].sum())

print("Group By Operations")

print(df.groupby('windspeed')['temperature'].sum())

14. Write a Program to read CSV file and show its data in python using dataFrames and pandas.’’’

import pandas as pd

df=pd.read_csv("student.csv", nrows=3)

print("To display selected number of rows from beginning")

print(df)

df=pd.read_csv("student.csv")

print(df)

print("Number of Rows and Columns")

print(df.shape)

print(df.head())

print("Tail")

print(df.tail(2))

print("Specified Number of Rows")

print(df[2:5])

print("Print Everything")

print(df[:])

print("Print Column Names")

print(df.columns)

print("Data from Individual Column")

print(df['Name']) #or df.Name

print(df['Marks'])

print("Maximum Marks : ", df['Marks'].max())

print("Printing According to Condition")

print(df[df.Marks>70])

print("Printing the row with maximum temperature")


print(df[df.Marks==df.Marks.max()])

print("Printing specific columns with maximum Marks")

print(df[['Name','Marks']][df.Marks==df.Marks.max()])

print("According to index")

print(df.loc[3])

print("Changing of Index")

df.set_index('Scno',inplace=True)

print(df)

print("Searching according to new index")

print(df.loc[4862])

print("Resetting the Index")

df.reset_index(inplace=True)

print(df)

print("Sorting")

print(df.sort_values(by=['Marks'],ascending=False))

print("Sorting on Multiple Columns")

print(df.sort_values(by=['Class','Section'],ascending=True))

print("Sorting on Multiple Columns one in ascending, another in descending")

print(df.sort_values(by=['Marks','Name'],ascending=[False,True]))

print("Sum Operations on Data Frame")

print(df['Marks'].sum())

print("Group By Operations")

print(df.groupby('Class')['Marks'].sum())

15. How to compute the mean, median, standard deviation of a numpy array?

# Solution
mu, med, sd = np.mean(sepallength), np.median(sepallength), np.std(sepallength)
print(mu, med, sd)
#> 5.84333333333 5.8 0.825301291785
16. Write a program to create dataframe for 3 students including name and roll numbers and add
new columns for 5 subjects and 1 column to calculate percentage. It should include random
numbers in marks of all subjects

import pandas as pd, numpy as np, random


D={‘Roll’:[1,2,3],’Name’:[‘Sangeeta’,’Shanti’,’Swati’]}
P=[]
C=[]
M=[]
E=[]
H=[]
SD=pd.DataFrame(D)
for i in range(3):
P.append(random.randint(1,101))
C.append(random.randint(1,101))
M.append(random.randint(1,101))
E.append(random.randint(1,101))
H.append(random.randint(1,101))
SD[‘Phy’]=P
SD[‘Chem’]=C
SD[‘Maths’]=M
SD[‘Eng’]=E
SD[‘Hin’]=H

SD[‘Total’]=SD.Phy+SD.Chem+SD.Maths+SD.Eng+SD.Hin
SD[‘Per’]=SD.Total/5
print(SD)

17. Write a complete Program to show database connectivity of python Data Frames with mysql
#database using Student table with all operations involved in the Result Management System .

Solution:

def fetchdata():

import mysql.connector

try:

db = mysql.connector.connect(user='root', password=' ', host=’localhost’,database='test')

cursor = db.cursor()

sql = "SELECT * FROM student"

cursor.execute(sql)

results = cursor.fetchall()

for cols in results:


nm = cols[0]

st = cols[1]

stream =cols[2]

av=cols[3]

gd=cols[4]

cl=cols[5]

print ("Name =%s, Stipend=%f, Stream=%s, Average Marks=%f, Grade=%s, Class=%d" %


(nm,st,stream,av,gd,cl ))

except:

print ("Error: unable to fetch data")

db.close()

def adddata():

import mysql.connector

nm=input("Enter Name : ")

stipend=int(input('Enter Stipend : '))

stream=input("Stream: ")

avgmark=float(input("Enter Average Marks : "))

grade=input("Enter Grade : ")

cls=int(input('Enter Class : '))

db = mysql.connector.connect(user='root', password='', host=’localhost’, database='test')

cursor = db.cursor()

sql="INSERT INTO student VALUES ( '%s' ,'%d','%s','%f','%s','%d')" %(nm, stipend, stream, avgmark, grade,
cls)

try:

cursor.execute(sql)

db.commit()

except:

db.rollback()
db.close()

def updatedata():

import mysql.connector

try:

db = mysql.connector.connect(user='root', password=' ', host=’localhost’,database='test')

cursor = db.cursor()

sql = "Update student set stipend=%d where name='%s'" % (500,'Arun')

cursor.execute(sql)

db.commit()

except Exception as e:

print (e)

db.close()

def udata():

import mysql.connector

try:

db = mysql.connector.connect(user='root', password=' ', host=’localhost’, database='test')

cursor = db.cursor()

sql = "SELECT * FROM student"

cursor.execute(sql)

results = cursor.fetchall()

for cols in results:

nm = cols[0]

st = cols[1]

stream =cols[2]

av=cols[3]

gd=cols[4]

cl=cols[5]
print ("Name =%s, Stipend=%f, Stream=%s, Average Marks=%f, Grade=%s, Class=%d" %
(nm,st,stream,av,gd,cl ))

except:

print ("Error: unable to fetch data")

temp=input("Enter Student Name to Updated : ")

tempst=int(input("Enter New Stipend Amount : "))

try:

#db = mysql.connector.connect(user='root', password=' ', host=’localhost’,database='test')

#cursor = db.cursor()

sql = "Update student set stipend=%d where name='%s'" % (tempst,temp)

cursor.execute(sql)

db.commit()

except Exception as e:

print (e)

db.close()

def deldata():

import mysql.connector

try:

db = mysql.connector.connect(user='root', password=' ', host=’localhost’,database='test')

cursor = db.cursor()

sql = "SELECT * FROM student"

cursor.execute(sql)

results = cursor.fetchall()

for cols in results:

nm = cols[0]
st = cols[1]

stream =cols[2]

av=cols[3]

gd=cols[4]

cl=cols[5]

print ("Name =%s, Stipend=%f, Stream=%s, Average Marks=%f, Grade=%s, Class=%d" %


(nm,st,stream,av,gd,cl ))

except:

print ("Error: unable to fetch data")

temp=input("Enter Student Name to deleted : ")

try:

#db = mysql.connector.connect(user='root', password=' ', host=’localhost’,database='test')

#cursor = db.cursor()

sql = "delete from student where name='%s'" % (temp)

ans=input("Are you sure you want to delete the record : ")

if ans=='yes' or ans=='YES':

cursor.execute(sql)

db.commit()

except Exception as e:

print (e)

try:

db = mysql.connector.connect(user='root', password=' ', host=’localhost’,database='test')

cursor = db.cursor()

sql = "SELECT * FROM student"

cursor.execute(sql)
results = cursor.fetchall()

for row in results:

nm = row[0]

st = row[1]

stream =row[2]

av=row[3]

gd=row[4]

cl=row[5]

print ("Name =%s, Stipend=%f, Stream=%s, Average Marks=%f, Grade=%s, Class=%d" %


(nm,st,stream,av,gd,cl ))

except:

print ("Error: unable to fetch data")

You might also like