12th IP PRACTICALS
12th IP PRACTICALS
12th IP PRACTICALS
01
Create a series of these numbers:
33,55,65,29,19,23
Find the sum of those values which ending with 3 or 5.
Input
import pandas as pd
list=[33,55,65,29,19,23]
ser=pd.Series(list)
print("Original Series:")
print(ser)
print("=========================")
sum5=sum(ser[ser%10==5])
sum3=sum(ser[ser%10==3])
print("Output after performing the operation:")
print(sum3+sum5)
Output
Program No. 02
Create a series of 10 numbers starting with 41 and with the increment of 3.
Add 7 to all odd values and subtract 3 in even values. Reprint the updated
series.
Input
import pandas as pd
ser=pd.Series(range(41,70,3))
print("Original Series is:")
print(ser)
for i in range(0,ser.size):
if ser[i]%2==0:
ser[i]=ser[i]-3
elif ser[i]%2!=0:
ser[i]=ser[i]+7
print("Updated Series is:")
print(ser)
Output
Program No. 03
Create a series of 10 numbers. Change the value of all the elements those
values are multiples of 4.
Input
import pandas as pd
numbers=[ ]
#Generating a Series
for i in range(1,11):
val=int(input('Enter a number: '))
numbers.append(val)
ser=pd.Series(numbers)
print('Original Series: ')
print(ser)
#Changing the values of multiple of 4 and assigning them a value 13
ser[ser%4==0]=13
print('Series after Changing the values of multiple of 4:')
print(ser)
Output
Program No. 04
Create a series and print top 6 elements and bottom 6 elements using the
head( ) and tail( ).
Input
import pandas as pd
ser_length=int(input(“Enter the the length of the Series: “))
numbers=[ ]
#Generating a Series
for i in range(ser_length):
val=int(input(“Enter a number: “))
numbers.append(val)
ser=pd.Series(numbers)
print(“Original Series: ”)
print(ser)
print(“Top 6 elements of Series are: ”)
print(ser.head(6))
print(“Bottom 6 elements of Series are: ”)
print(ser.tail(6))
Output
Program No. 05
Create a panda’s series from a dictionary of values and a ndarray.
Input
import pandas as pd
import numpy as np
#Creating series from a dictionary
d={'Jan':31,'Feb':28,'Mar':31,'Apr':30}
s=pd.Series(d)
print("Series from dictionary")
print("~~~~~~~~~~~~~~~~~~~~~~~")
print(s)
#Creating series from an ndarray
ar=np.array([2,3,4,5,6])
print("\nSeries from ndarray")
print("~~~~~~~~~~~~~~~~~~~~~~~")
s1=pd.Series(ar)
print(s1)
Output
Program No. 06
Write a program to create data series and then change the indexes of the
series object in any random order.
Input
import pandas as pd
Ser=pd.Series([1,2,3,4,5],[0,9,8,7,6])
print("Original Series is:")
print(Ser)
Ser=Ser.reindex([6,7,8,9,0])
print("Series after changing the order of index:")
print(Ser)
Output
Program No. 07
Consider the two series objects s11 and s12. Print the attributes of both
these objects in a report form as shown below:
Input
import pandas as pd
import numpy as np
section=["A","B","C","D"]
contri=[6700,5600,5000,5200]
section1=["A","B","C","D","E"]
contri1=np.array([6700,5600,5000,5200,np.NaN])
s11=pd.Series(data=contri,index=section)
s12=pd.Series(data=contri1*2,index=section1,dtype=np.float32)
print("Attribute name \t\t Object s11 \t\t Object s12")
print("-------------- \t\t ---------- \t\t ----------")
print("Data type(.type) :\t\t",s11.dtype,"\t\t\t", s12.dtype)
print("Shape(.shape) :\t\t",s11.shape,"\t\t\t", s12.shape)
print("No. of bytes(.nbytes) :\t\t",s11.nbytes,"\t\t\t", s12.nbytes)
print("No. of dimensions(.ndim) :\t\t",s11.ndim,"\t\t\t", s12.ndim)
print("Has NaNs?(.hasnans) :\t\t",s11.hasnans,"\t\t\t", s12.hasnans)
print("Empty?(.empty) :\t\t",s11.empty,"\t\t\t", s12.empty)
Output
Program No. 08
Write a Python code to create a dictionary which stores the sales values
under 4 stores of a company for two years. The dictionary is as shown
below:
Dict={'Year1':{'Store1':21300,'Store2':28400,'Store3':
42100,'Store4':36500},'Year2':{'Store1':28250,'Store2'
:26000,'Store3':36300,'Store4':32200}}
Use the above dictionary to create a DataFrame with Years as row labels
and Stores as column labels. Finally, display the DataFrame.
Input
import pandas as pd
Dict={'Year1':{'Store1':21300,'Store2':28400,'Store3':42100,'Store4':36500},
'Year2':{'Store1':28250,'Store2':26000,'Store3':36300,'Store4':32200}}
Df=pd.DataFrame(Dict)
print("The elements of DataFrame are:\n")
print(Df)
Output
Program No. 09
A fruit farming company wants to record the targeted sale and actual sale
of different fruits (Apples, Mangoes, Grapes and Pears). Write a Python
code to create a list of dictionaries each containing targeted sale and actual
sale of each fruit. Further, create a DataFrame (df1) using fruit names as
row labels and targeted sale and actual sale as column labels. Finally,
display the DataFrame.
Input
import pandas as pd
Dict1={'Targeted':65000,'Actual':62000}
Dict2={'Targeted':89000,'Actual':92000}
Dict3={'Targeted':72000,'Actual':75000}
Dict4={'Targeted':54000,'Actual':53000}
s1=[Dict1,Dict2,Dict3,Dict4]
df1=pd.DataFrame(s1,index=['Apples','Mangoes','Grapes','Pears'])
print("The elements of DataFrame are:\n")
print(df1)
Output
Program No. 10
Write a python code to create a (3x3) 2D ndarray containing natural
numbers from 1 to 9. With the help of the 2D array, create a DataFrame
using row and column labels of your choice. Finally, display the DataFrame.
Input
import pandas as pd
import numpy as np
npa=np.array([[1,2,3],[4,5,6],[7,8,9]])
index=['Row1','Row2','Row3']
columns=['Col1','Col2','Col3']
df2=pd.DataFrame(npa,index,columns)
print("The elements of DataFrame are:\n")
print(df2)
Output
Program No. 11
Write a python code to create a Dataframe by using the specification given
below:
In the above structure, First, Second, Third and English, Maths, Science
are the row and column indices respectively. Display the following
information from the above Dataframe:
(i) Number of rows in the Dataframe
(ii) Number of values in each column of the Dataframe
(iii) First two rows of the Dataframe
(iv) Last two rows of the Dataframe
Input
import pandas as pd
Lst1=[[87,92,76],[79,98,78],[84,65,73]]
index=['First','Second','Third']
columns=['English','Maths','Science']
df1=pd.DataFrame(Lst1,index,columns)
print("The elements of DataFrame are:")
print(df1)
print("=====================================")
print("Number of rows in the Dataframe:")
print(len(df1))
print("=====================================")
print("Number of values in each column of the Dataframe")
print(df1.count())
print("=====================================")
print("First two rows of the Dataframe")
print(df1.head(2))
print("=====================================")
print("Last two rows of the Dataframe")
print(df1.tail(2))
Output
Program No. 12
Write a python code to create a Dataframe to record the sales of a
company under different quarters in the years 2020 and 2021. Use the
quarters(Qr1,Qr2,Qr3 and Qr4) as row indices and year 2020 and 2021 as
column indices. Display the following:
the sale values of the all the quarters in the year 2021
(i) Entire Dataframe
(ii) The sale values of both the years in quarter Qr3
(iii) The sale values of all the quarters in the year 2021
Input
import pandas as pd
dict={2020:{'Qr1':56000,'Qr2':48000,'Qr3':41000,'Qr4':55000},2021:{'Qr1':5
8000,'Qr2':47000,'Qr3':39000,'Qr4':57000}}
df2=pd.DataFrame(dict)
print("Elements of the Dataframe:")
print(df2)
print("Sales under quater 3(Qr3):")
print(df2.loc['Qr3'])
print("Sales in year 2021:")
print(df2[2021])
Output
Program No. 13
Write a python code to create a (3x3) Dataframe containing integer
numbers. Display the Dataframe after removing the 3rd row and 3rd
column of the Dataframe.
Input
import pandas as pd
Lst=[[10,20,30],[40,50,60],[70,80,90]]
df1=pd.DataFrame(Lst)
print("Original Dataframe:")
print(df1)
del df1[2] # To delete 3rd column
df1=df1.drop([2]) # To delete 3rd row
print("Dataframe after removing removing the 3rd row and 3rd column of
the Dataframe ")
print(df1)
Output
Program No. 14
Write a python code to create a Dataframe using the columns as names of
first five months and number of days in each month. Use boolean type row
indices alternately True and False corresponding to each month. Display
the Dataframe. Also, display the rows whose Boolean indices are True.
Input
import pandas as pd
Lst=[['JAN',31],['FEB',28],['MAR',31],['APR',30],['MAY',31]]
index=[True,False,True,False,True]
columns=['Months','No. of Days']
df1=pd.DataFrame(Lst,index,columns)
print('Element of Dataframe are:')
print(df1)
print('The rows for which the indices are True:')
print(df1.loc[True])
Output
Program No. 15
Write a python code to create a Dataframe containing integer numbers.
Display the entire elements of the Dataframe. Also display the number of
rows and the number of columns in the Dataframe.
Input
import pandas as pd
Lst=[[12,34,65],[78,89,32]]
df1=pd.DataFrame(Lst)
print('Element of Dataframe are:')
print(df1)
print('The number of rows in the Dataframe are:')
print(len(df1.axes[0])) # Axis 0 represents rows
print('The number of columns in the Dataframe are:')
print(len(df1.axes[1])) # Axis 1 represents columns
Output
Program No. 16
Write a python code to create two Dataframes each with 3 rows and 3
columns containing integer numbers. Display the original Dataframe and
number of common elements in both the Dataframes.
Input
import pandas as pd
Lst1=[[12,34,65],[78,89,32],[55,67,23]]
Lst2=[[15,56,65],[78,84,98],[55,53,23]]
df1=pd.DataFrame(Lst1)
df2=pd.DataFrame(Lst2)
print('Elements of first Dataframe are:')
print(df1)
print('Elements of second Dataframe are:')
print(df2)
print('The number of common elements are:')
print(len(df1==df2))
Output
Program No. 17
Write a python code to create a Dataframes containing marks in Eng1,
Eng2, and Total marks secured by three students of the class. Display the
original Dataframe. Also, display the marks in Eng1, Eng2 and Total marks
of the students who have secured 80% and above.
Input
import pandas as pd
Lst1=[[82,80,162],[75,80,155],[92,83,175]]
index=[1,2,3]
columns=['Eng1','Eng2','Total Marks']
df1=pd.DataFrame(Lst1,index,columns)
print('Elements of first Dataframe are:')
print(df1)
print('The rows with Total marks 80% and above are:')
print(df1[df1['Total Marks']>=160])
Output