Practical File Class - Xii Informatics Practices (New) : 1. How To Create A Series From A List, Numpy Array and Dict?
Practical File Class - Xii Informatics Practices (New) : 1. How To Create A Series From A List, Numpy Array and Dict?
Practical File Class - Xii Informatics Practices (New) : 1. How To Create A Series From A List, Numpy Array and Dict?
CLASS – XII
Informatics Practices (New)
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
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
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]),)
arr[[1,0,2], :]
#> array([[3, 4, 5],
#> [0, 1, 2],
#> [6, 7, 8]])
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
data=['a','b','c']
s=pd.Series(data)
print(s)
#creating an array
ar1=list()
print("Enter numbers")
for i in range(0,n):
num=int(input("num:"))
ar1.append(num)
s=pd.Series(ar1)
print(s)
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)'''
dic=({'rupali':[9826386977,'[email protected]'], 'purnendu':[9826911972,'[email protected]'],
'vedant':[788990,'[email protected]']})
s=pd.Series(dic)
print (s)
dict[key]=value
s=pd.Series(dict)
print (s)
s=pd.Series(data,index=[1,2,3])
print (s)
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)
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(df.shape)
print(df.head())
print("Tail")
print(df.tail(2))
print(df[2:5])
print("Print Everything")
print(df[:])
print("Print Column Names")
print(df.columns)
print(df['temperature'])
print(df[df.temperature>41])
print(df[df.temperature==df.temperature.max()])
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(df.loc['01/03/2018'])
df.reset_index(inplace=True)
print(df)
print("Sorting")
print(df.sort_values(by=['temperature'],ascending=False))
print(df.sort_values(by=['temperature','windspeed'],ascending=True))
print(df.sort_values(by=['temperature','windspeed'],ascending=[True,False]))
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(df)
df=pd.read_csv("student.csv")
print(df)
print(df.shape)
print(df.head())
print("Tail")
print(df.tail(2))
print(df[2:5])
print("Print Everything")
print(df[:])
print(df.columns)
print(df['Marks'])
print(df[df.Marks>70])
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(df.loc[4862])
df.reset_index(inplace=True)
print(df)
print("Sorting")
print(df.sort_values(by=['Marks'],ascending=False))
print(df.sort_values(by=['Class','Section'],ascending=True))
print(df.sort_values(by=['Marks','Name'],ascending=[False,True]))
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
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:
cursor = db.cursor()
cursor.execute(sql)
results = cursor.fetchall()
st = cols[1]
stream =cols[2]
av=cols[3]
gd=cols[4]
cl=cols[5]
except:
db.close()
def adddata():
import mysql.connector
stream=input("Stream: ")
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:
cursor = db.cursor()
cursor.execute(sql)
db.commit()
except Exception as e:
print (e)
db.close()
def udata():
import mysql.connector
try:
cursor = db.cursor()
cursor.execute(sql)
results = cursor.fetchall()
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:
try:
#cursor = db.cursor()
cursor.execute(sql)
db.commit()
except Exception as e:
print (e)
db.close()
def deldata():
import mysql.connector
try:
cursor = db.cursor()
cursor.execute(sql)
results = cursor.fetchall()
nm = cols[0]
st = cols[1]
stream =cols[2]
av=cols[3]
gd=cols[4]
cl=cols[5]
except:
try:
#cursor = db.cursor()
if ans=='yes' or ans=='YES':
cursor.execute(sql)
db.commit()
except Exception as e:
print (e)
try:
cursor = db.cursor()
cursor.execute(sql)
results = cursor.fetchall()
nm = row[0]
st = row[1]
stream =row[2]
av=row[3]
gd=row[4]
cl=row[5]
except: