Ip Practical File
Ip Practical File
PROGRAMS:
Ex. No. 1
CREATE A PANDA’S SERIES FROM A DICTIONARY OF VALUES AND
NDARRAY.
AIM:
To create a panda’s series from a dictionary of values and a ndarray.
SOURCE CODE:
import pandas as pd
import numpy as np
s=pd.Series(np.array([1,3,4,7,8,8,9]))
print(s)
OUTPUT:
RESULT:
Thus, the above given program is executed successfully and the output is verified.
Ex. No. 2
ARITHMETIC OPERATIONS ON TWO PANDAS SERIES
AIM:
To write a Pandas program to perform arithmetic operations on two Pandas Series.
SOURCE CODE:
OUTPUT:
RESULT:
Thus, the above given program is executed successfully and the output is verified.
EX. NO. 3
ADDING DATA TO AN EXISTING SERIES
AIM:
To Write a Pandas program to add some data to an existing Series.
SOURCE CODE:
import pandas as pd
s1 = pd.Series(['S101', 'Amjad', 'C.Sc.', 'XII – A1', '450'])
print("Original Data Series:")
print(s1)
print("\nData Series after adding some data:")
s2=pd.Series(['90.0', 'PASS'])
new_s=pd.concat([s1,s2])
print(new_s)
OUTPUT:
RESULT:
Thus, the above given program is executed successfully and the output is verified.
Ex. No. 4:
MATHEMATICAL EXPRESSION, EXPONENTIATION AND NUMPY.
AIM:
To create a Pandas Series for mathematical expression, exponentiation and NumPy.
Program:
import pandas as pd
import numpy as np
ss=np.arange(10,15)
print(ss)
s=pd.Series(index=ss,data=ss*4)
s1=pd.Series(index=ss,data=ss+4)
s2=pd.Series(index=ss,data=ss**2)
s.index.name='INDEX'
print(s)
print(s1)
print(s2)
OUTPUT:
RESULT:
Thus, the above given program is executed successfully and the output is verified.
Ex. No. 5
PRINT ALL THE ELEMENTS THAT ARE ABOVE THE 75TH PERCENTILE.
AIM:
To write a program to print all the elements that are above the 75th percentile in a
given Series.
SOURCE CODE:
OUTPUT:
Result:
The above given program is executed successfully and the output is shown.
Ex. No. 6: (Don’t Write the heading)
CREATE A DATA FRAME, QUARTERLY SALES WHERE EACH ROW
CONTAINS THE ITEM CATEGORY, ITEM NAME, AND EXPENDITURE. GROUP
THE ROWS BY THE CATEGORY AND PRINT THE TOTAL EXPENDITURE PER
CATEGORY.
AIM:
To Create a Data Frame quarterly sales where each row contains the item category,
item name and expenditure. Group the rows by the category and print the total expenditure
per category.
SOURCE CODE:
import pandas as pd
dic={'itemcat': ['car', 'Ac', 'Aircoller', 'Washing Machine'], 'itemname':['Ford',
"Hitachi", 'Symphony', 'LG'], 'expenditure':[7000000, 50000, 12000, 14000]}
quartsales=pd.DataFrame(dic)
print(quartsales)
qs=quartsales.groupby('itemcat')
print('Result after Filtering Dataframe')
print(qs[['itemcat', 'expenditure']].sum())
OUTPUT:
RESULT:
Thus, the above given program is executed successfully and the output is verified.
EX. NO 7:
FIND THE TOTAL MARK AND PERCENTAGE OF EACH STUDENT USING
SERIES.
AIM:
To Write a program to accept the name and mark obtained by 3 students for 2 terms (using
series) . Find the total mark and percentage of each student
Program:
import pandas as pd
mark1=[]
mark2=[]
names=[]
for i in range(3):
n=input("Enter the students name --")
names.append(n)
m1=eval(input("Enter the First term mark out of 100 --"))
mark1.append(m1)
m2=eval(input("Enter the Second term mark out of 100 --"))
mark2.append(m2)
series1=pd.Series(mark1,index=names)
series2=pd.Series(mark2,index=names)
c='y'
while(c=='y' or c=='Y'):
print("\n1.Total Mark")
print("\n2. Percentage")
ch=eval(input("\nEnter your choice (1/2) --"))
if ch==1:
print(series1+series2)
elif ch==2:
print((series1+series2)*100/200)
c=input("Do you want to continue?(y/n)")
OUTPUT:
Enter the students name --Yogesh
Enter the First term mark out of 100 --89
Enter the Second term mark out of 100 --97
Enter the students name --Monish
Enter the First term mark out of 100 --95
Enter the Second term mark out of 100 --68
Enter the students name --Pavithran
Enter the First term mark out of 100 --85
Enter the Second term mark out of 100 --68
1.Total Mark
2. Percentage
1.Total Mark
2. Percentage
RESULT:
Thus, the above given program is executed successfully and the output is verified.
EX. NO. 8
FIND THE SUM OF EACH COLUMN AND THE LOWEST MEAN.
AIM:
To create a data frame and find:
i) the sum of each column
ii) the column with the lowest mean.
Program:
import pandas as pd
profit={'TCS':{'qtr1':2500,'qtr2':2000,'qtr3':3000,'qtr4':2000},
'WIPRO':{'qtr1':2800,'qtr2':2400,'qtr3':3600,'qtr4':2400},
'L&T':{'qtr1':2100,'qtr2':5700,'qtr3':35000,'qtr4':2100}}
df=pd.DataFrame(profit)
print(df)
print()
print('Column wise sum in dataframe is.. ')
print(df.sum(axis=0))
print()
print('Column wise mean value..')
print(df.mean(axis=0))
print()
print('Column with minimum mean value is..')
print(df.mean(axis=0).idxmin())
OUTPUT:
RESULT:
Thus, the above given program is executed successfully and the output is verified.
EX. NO. 9
DISPLAY ROW LABELS, COLUMN LABELS DATA TYPES OF EACH COLUMN
AND THE DIMENSIONS.
AIM:
To write a python program to create a Data Frame for examination results and display row
labels, column labels data types of each column and the dimensions.
Program:
import pandas as pd
import numpy as np
TNS=np.array([200,180,175,188,196,180,181,191,178,180,200,200])
TNSP=np.array([200,180,174,186,196,180,180,191,178,180,199,200])
PP=TNSP/TNS
d={'Class':['I','II','III','IV','V','VI','VII','VIII','IX','X','XI','XII'],
'Total_No.of_Stud_Appear':[200,180,175,188,196,180,181,191,178,180,200,200],
'Total_No.of_Stud_Pass':[200,180,174,186,196,180,180,191,178,180,199,200],
'Pass_%':PP*100}
Result=pd.DataFrame(d)
print(Result)
print(Result.dtypes)
print('Shapes of the DataFrame is : ')
print(Result.shape)
OUTPUT:
RESULT:
Thus, the above given program is executed successfully and the output is verified.
EX. NO. 10
FILTER OUT THE DUPLICATE ROWS.
AIM:
To create a program to filter out the Rows Based on different Criteria such as
Duplicate Rows.
Program:
import pandas as pd
d={'Name':['Rohit','Dhoni','Rohit','Ganguly','Yuvaraj','Rohit','Dhoni','Kohli','Sachin','Kohli'],
'High_Score_ODI':[264,183,209,183,150,209,224,183,200,183],
'ODI_ Score':[9115,10773,9115,11363,8701,9115,10773,11867,18426,11867]}
Result=pd.DataFrame(d)
DR=Result[Result.duplicated(keep=False)]
print(Result)
print('\n')
print(DR)
OUTPUT:
RESULT:
Thus, the above given program is executed successfully and the output is verified.
EX. NO. 11
CALCULATION AND DISPLAY DATA USING VARIOUS METHODS IN SERIES
AIM:
To create a series object for the given data and write code to perform
a. Modify the amount of section ‘XI A’ as 7600 and for sections ‘XI C’ and ‘XI D’ as 7000
and print the Series.
b. Display the first 3 elements (use slicing)
c. Display the last 3 elements.(use slicing)
d. Display Third and Fourth elements(using iloc)
e. Display Second, Third and Fourth elements(using loc).
Program:
import pandas as pd
s=pd.Series([6700,5600,5000,5200,6200,5800],
index=['XI A','XI B','XI C','XI D','XI E','XI F'])
s[0]=7600
s[2:4]=7000
print('Series after modifying amounts')
print(s)
print('\nFirst 3 elements')
print(s[:3])
print('\nLast 3 elements')
print(s[-3:])
print('\nThird and fourth elements (using iloc)')
print(s.iloc[2:4])
print('\nSecond, Third and fourth elements (using loc)')
print(s.loc['XI B':'XI D'])
OUTPUT:
Series after modifying amounts
XI A 7600
XI B 5600
XI C 7000
XI D 7000
XI E 6200
XI F 5800
dtype: int64
First 3 elements
XI A 7600
XI B 5600
XI C 7000
dtype: int64
Last 3 elements
XI D 7000
XI E 6200
XI F 5800
dtype: int64
RESULT:
Thus, the above given program is executed successfully and the output is verified.
EX. NO. 12
CALCULATION AND DISPLAY DATA USING VARIOUS FUNCTIONS IN
DATAFRAME
AIM:
To create a dataframe for the given data and perform
i) Display the dataframe
ii) Sales of 2017
iii) Sales in quarter2
iv) Sales in 2015, 2016 and 2017 for quarter 1 &2
v) Indices of dataframe and columns of dataframe
vi) Row and column labels
vii) Number of rows and columns
viii) Sales in qtr 4 during the year 2018
ix) Change sales in qtr3 during 2017 to 754 and print
x) Add a new column 2019 with values[524,639,785,458]for all quarters
.
Program:
import pandas as pd
sales=pd.DataFrame({'2015':[256,452,635,965],
'2016':[745,785,478,547],'2017':[452,474,725,854],
'2018':[1021,958,528,425]},
index=['qtr1','qtr2','qtr3','qtr4'])
print("Data Frame")
print(sales)
print("Sales of 2017")
print(sales['2017'])
print("Sales in Quarter 2")
print(sales.loc['qtr2',:])
print("Sales in 2015, 2016 & 2017 for Quarter 1 & 2")
print(sales.loc['qtr1':'qtr2','2015':'2017'])
print("Indices of DataFrame")
print(sales.index)
print("Columns of DataFrame")
print(sales.columns)
print("Row and Column Labels")
print(sales.axes)
print("Number of Rows and Columns")
print(sales.shape)
print("Sales in Quarter 4 during the year 2018")
print(sales.loc['qtr4','2018'])
print("Change sales in Quarter 3 during 2017 to 754 and Print")
sales.loc['qtr3','2017']=754
print(sales)
print("Add a new column 2019 with values [524,639,785,458] for all quarters")
sales[2019]=[524,639,785,458]
print(sales)
OUTPUT:
Data Frame
2015 2016 2017 2018
qtr1 256 745 452 1021
qtr2 452 785 474 958
qtr3 635 478 725 528
qtr4 965 547 854 425
Sales of 2017
qtr1 452
qtr2 474
qtr3 725
qtr4 854
Name: 2017, dtype: int64
Sales in Quarter 2
2015 452
2016 785
2017 474
2018 958
Name: qtr2, dtype: int64
Sales in 2015, 2016 & 2017 for Quarter 1 & 2
2015 2016 2017
qtr1 256 745 452
qtr2 452 785 474
Indices of DataFrame
Index(['qtr1', 'qtr2', 'qtr3', 'qtr4'], dtype='object')
Columns of DataFrame
Index(['2015', '2016', '2017', '2018'], dtype='object')
Row and Column Labels
[Index(['qtr1', 'qtr2', 'qtr3', 'qtr4'], dtype='object'), Index(['2015', '2016', '2017', '2018'],
dtype='object')]
Number of Rows and Columns
(4, 4)
Sales in Quarter 4 during the year 2018
425
Change sales in Quarter 3 during 2017 to 754 and Print
2015 2016 2017 2018
qtr1 256 745 452 1021
qtr2 452 785 474 958
qtr3 635 478 754 528
qtr4 965 547 854 425
Add a new column 2019 with values [524,639,785,458] for all quarters
2015 2016 2017 2018 2019
qtr1 256 745 452 1021 524
qtr2 452 785 474 958 639
qtr3 635 478 754 528 785
qtr4 965 547 854 425 458
RESULT:
Thus, the above given program is executed successfully and the output is verified.
EX. NO. 13
LOAD CSV FILE AND DISPLAY THE DATA
AIM:
To create a dataFrame using student name , roll number and grade as the key, transfer
this dataframe to CSV file using tab as separator and load the above csv file and display the
data.
Program:
import pandas as pd
st={'Student':['Sunil','Cody','Sanjay','Drew'], 'Rollnumber':[1,5,10,15], 'Grade':['A','C','F','B']}
df=pd.DataFrame(st)
print("Original Dataframe")
print(df)
print()
df.to_csv('Students.csv', sep = '\t')
new_df=pd.read_csv('Students.csv')
print('Data from Students.csv:')
print(new_df)
OUTPUT:
RESULT:
Thus, the above given program is executed successfully and the output is verified.
EX. NO 14:
IMPORTING AND EXPORTING DATA BETWEEN PANDAS AND CSV FILE.
AIM:
To write a program to import and export data between pandas and CSV file.
Program:
import pandas as pd df=pd.read_csv("D:\cars.csv")
print(df)
print()
Name=['ELAN','NIVAN','SUHAIL','ARJUN']
Prof=['Animator','GraphicDesigner','SoundEngineer','Game Developer']
dict={'Name':Name,'Prof':Prof}
df=pd.DataFrame(dict)
print("The File'team.CSV'is Created(Imported)")
df.to_csv('team.csv')
print()
OUTPUT:
RESULT:
Thus, the above given program is executed successfully and the output is verified.
EX. NO. 15:
READ CSV USING DATAFRAME AND DISPLAY THE DETAILS
AIM:
To create a csv file with id,fname marks in English, accountancy Business studiesand
IP. Read csv using dataframe and do the following
i) Display the details of topper
ii) Display English toppers details
Program:
import pandas as pd
nm=['Ajay','Aparna','Pankaj','Geet']
eng=[90,40,80,98]
ac=[56,78,45,89]
bs=[78,77,56,66]
ip=[89,87,67,98]
dict={'name':nm,'eng':eng,'acct':ac,'bstudy':bs,'IP':ip}
df=pd.DataFrame(dict)
df.to_csv('file1.csv')
df2=pd.read_csv('file1.csv')
print(df)
df['Total']=df['eng']+df['acct']+df['bstudy']+df['IP']
print("\nPrint the details of topper")
print(df[df.Total==df.Total.max()])
print("\nPrinting the english topper details")
print(df[df.eng==df.eng.max()])
OUTPUT:
RESULT:
Thus, the above given program is executed successfully and the output is verified.