0% found this document useful (0 votes)
42 views

Lab Programs

The document contains code and output from several pandas programs: 1. The first program creates a pandas series from a NumPy array and dictionary, and performs arithmetic operations on two pandas series. 2. The second program selects rows from a dataframe where the percentage is greater than 70, and also between 70-90. 3. A third program allows the user to input a row index and change the percentage in that row. 4. The last two programs join two dataframes along rows and columns, respectively, to combine the data.

Uploaded by

nir
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views

Lab Programs

The document contains code and output from several pandas programs: 1. The first program creates a pandas series from a NumPy array and dictionary, and performs arithmetic operations on two pandas series. 2. The second program selects rows from a dataframe where the percentage is greater than 70, and also between 70-90. 3. A third program allows the user to input a row index and change the percentage in that row. 4. The last two programs join two dataframes along rows and columns, respectively, to combine the data.

Uploaded by

nir
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 53

OUTPUT

0 1
1 3
2 4
3 7
4 8
5 9
dtype: int32
x 10
y 20
z 30
dtype: int64

PROGRAM 1
1)Q. Create a pandas series from a dictionary of
values and ndarray
AIM: To create a pandas series from dictionary
of values
Software Required: Anaconda Software
Program:
import pandas as pd
import numpy as np
s=pd.Series(np.array([1,3,4,7,8,9]))
print(s)
#create a dictionary
dictionary={'x':10,'y':20,'z':30}
#create a series
series=pd.Series(dictionary)
print(series)
Result: Thus the program of pandas series from
dictionary has been executed successfully
OUTPUT
add two series
0 5
1 10
2 15
3 20
4 25
dtype: int64
subtract two series
0 1
1 2
2 3
3 4
4 5
dtype: int64
multiply two series
0 6
1 24
2 54
3 96
4 150
dtype: int64
divide series 1 by series 2
0 1.5
1 1.5
2 1.5
3 1.5
4 1.5
dtype: float64
divide series 1 by series 2 with floor division
0 1
1 1
2 1
3 1
4 1
dtype: int64
2)Q. Create a Pandas program to perform arithmetic
operations on two pandas series
AIM: To create a pandas series from dictionary of values
Software Required: Anaconda Software
Program:
#write a pandas program tp perform arithmetic operations
import pandas as pd
ds1=pd.Series([3,6,9,12,15])
ds2=pd.Series([2,4,6,8,10])
ds=ds1+ds2
print("add two series")
print(ds)
ds=ds1-ds2
print("subtract two series")
print(ds)
ds=ds1*ds2
print("multiply two series")
print(ds)
ds=ds1/ds2
print("divide series 1 by series 2")
print(ds)
ds=ds1//ds2
print("divide series 1 by series 2 with floor division")
print(ds)
RESULT: THUS THE PROGRAM TO DO PERFORM ARITHMETIC
SERIES EXECUTED SUCCESSFULLY
OUTPUT
3)Q. Write a pandas program to add data to an
existing series
AIM:
OUTPUT
Number of student whose percentage more
than 70:
name perc qualify
A Aman 79.5 yes
C Amjad 90.5 yes
J Pooja 89.0 yes
4)A)Q. Write a pandas programs to select rows
where the percentage is greater than 70
AIM: to create a pandas program to select the
rows where the percentage is greater than 70
SOFTWARE REQUIRED: Spyder Pandas Library
PROGRAM:
import pandas as pd
import numpy as np
exam_data = {'name': ['Aman', 'Kamal', 'Amjad',
'Rohan', 'Amit', 'Sumit', 'Matthew', 'Kartik',
'Kavita', 'Pooja'],
'perc': [79.5, 29, 90.5, np.nan, 32, 65, 56,
np.nan, 29, 89],
'qualify': ['yes', 'no', 'yes', 'no', 'no', 'yes',
'yes', 'no', 'no', 'yes']}
labels = ['A', 'B', 'C', 'B', 'E', 'F', 'G', 'H', 'I', 'J']
df = pd.DataFrame(exam_data, index=labels)
print("Number of student whose percentage
more than 70:")
print(df[df['perc'] > 70])
RESULT: Thus, the program has been executed
successfully
OUTPUT
number of students whose percentage is
between 70 and 90:
name perc qualify
A Aman 79.5 yes
J Pooja 89.0 yes
4)A)Q. Write a pandas programs to select rows
where the percentage is greater than 70
AIM: to create a pandas program to select the
rows where the percentage is greater than 70
SOFTWARE REQUIRED: Spyder Pandas Library
PROGRAM:
import pandas as pd
import numpy as np
exam_data = {'name': ['Aman', 'Kamal', 'Amjad',
'Rohan', 'Amit', 'Sumit', 'Matthew', 'Kartik',
'Kavita', 'Pooja'],
'perc': [79.5, 29, 90.5, np.nan, 32, 65, 56,
np.nan, 29, 89],
'qualify': ['yes', 'no', 'yes', 'no', 'no', 'yes',
'yes', 'no', 'no', 'yes']}
labels = ['A', 'B', 'C', 'B', 'E', 'F', 'G', 'H', 'I', 'J']
df=pd.DataFrame(exam_data,index=labels)
print("number of students whose percentage is
between 70 and 90:")
print(df[df['perc'].between(70,90)])
RESULT: Thus, the program has been executed
successfully
OUTPUT
number of students whose percentage is
between 70 and 90:
name perc qualify
A Aman 79.5 yes
J Pooja 89.0 yes
4)B)Q. Write a pandas programs to select rows
where the percentage is between 70 and 90
AIM: to create a pandas program to select the
rows where the percentage is greater than 70
SOFTWARE REQUIRED: Spyder Pandas Library
PROGRAM:
import pandas as pd
import numpy as np
exam_data = {'name': ['Aman', 'Kamal',
'Amjad', 'Rohan', 'Amit', 'Sumit', 'Matthew',
'Kartik', 'Kavita', 'Pooja'],
'perc': [79.5, 29, 90.5, np.nan, 32, 65, 56,
np.nan, 29, 89],
'qualify': ['yes', 'no', 'yes', 'no', 'no', 'yes',
'yes', 'no', 'no', 'yes']}
labels = ['A', 'B', 'C', 'B', 'E', 'F', 'G', 'H', 'I', 'J']
df=pd.DataFrame(exam_data,index=labels)
print("number of students whose percentage is
between 70 and 90:")
print(df[df['perc'].between(70,90)])

RESULT: Thus, the program has been executed


successfully
OUTPUT
original dataframe:
name perc qualify
A Aman 79.5 yes
B Kamal 29.0 no
C Amjad 90.5 yes
B Rohan NaN no
E Amit 32.0 no
F Sumit 65.0 yes
G Matthew 56.0 yes
H Kartik NaN no
I Kavita 29.0 no
J Pooja 89.0 yes
enter the index of the row:A
enter percentage to be changed:90

change the percentage in row A to 90.0


name perc qualify per
A Aman 79.5 yes 90.0
B Kamal 29.0 no NaN
C Amjad 90.5 yes NaN
B Rohan NaN no NaN
E Amit 32.0 no NaN
F Sumit 65.0 yes NaN
G Matthew 56.0 yes NaN
H Kartik NaN no NaN
I Kavita 29.0 no NaN
J Pooja 89.0 yes NaN
5)Q. Write a pandas programs to change the
percentage in a given row
AIM: to create a pandas program programs to
change the percentage in a given row by the
user
SOFTWARE REQUIRED: Spyder Pandas Library
PROGRAM:
#write a pandas program to change the % in
given row by user
import pandas as pd
import numpy as np
exam_dic = {'name': ['Aman', 'Kamal', 'Amjad',
'Rohan', 'Amit', 'Sumit', 'Matthew', 'Kartik',
'Kavita', 'Pooja'],
'perc': [79.5, 29, 90.5, np.nan, 32, 65, 56,
np.nan, 29, 89],
'qualify': ['yes', 'no', 'yes', 'no', 'no', 'yes',
'yes', 'no', 'no', 'yes']}
labels = ['A', 'B', 'C', 'B', 'E', 'F', 'G', 'H', 'I', 'J']
df = pd.DataFrame(exam_dic , index=labels)
print("\noriginal dataframe:")
print(df)
ch=input("enter the index of the row:")
per=float(input("enter percentage to be
changed:"))
print('\nchange the percentage in row '+ch+ '
to',per)
df.loc[ch,'per']=per
print(df)

RESULT: Thus, the program has been executed


successfully
OUTPUT
original dataframe:
name perc qualify
0 Aman 79.5 yes
1 Kamal 29.0 no
2 Amjad 90.5 yes
3 Rohan NaN no
4 Amit 32.0 no
5 Sumit 65.0 yes
6 Matthew 56.0 yes
7 Kartik NaN no
8 Kavita 29.0 no
9 Pooja 89.0 yes
----------------------------------------------------------------------------
Name perc qualify
0 parveen 89.5 yes
1 ahil 92.0 yes
2 ashaz 90.5 yes
3 shifin 91.5 yes
4 hanash 90.0 yes
\join the said two dataframes along rows:
name perc qualify
0 Aman 79.5 yes
1 Kamal 29.0 no
2 Amjad 90.5 yes
3 Rohan NaN no
4 Amit 32.0 no
5 Sumit 65.0 yes
6 Matthew 56.0 yes
7 Kartik NaN no
8 Kavita 29.0 no
9 Pooja 89.0 yes
0 parveen 89.5 yes
1 ahil 92.0 yes
2 ashaz 90.5 yes
3 shifin 91.5 yes
4 hanash 90.0 yes
6)Q. Write a pandas programs to join the two
given dataframes along rows and assign all data
AIM: to create a pandas program programs to
join the two given dataframes along rows and
assign all data
SOFTWARE REQUIRED: Spyder Pandas Library
PROGRAM:
#write a pandas program to change the % in
given row by user
import pandas as pd
import numpy as np
exam_dic1 = {'name': ['Aman', 'Kamal', 'Amjad',
'Rohan', 'Amit', 'Sumit', 'Matthew', 'Kartik',
'Kavita', 'Pooja'],
'perc': [79.5, 29, 90.5, np.nan, 32, 65, 56,
np.nan, 29, 89],
'qualify': ['yes', 'no', 'yes', 'no', 'no', 'yes',
'yes', 'no', 'no', 'yes']}
exam_data1=pd.DataFrame(exam_dic1)
exam_dic2={'name':
['parveen','ahil','ashaz','shifin','hanash'], 'perc':
[89.5,92,90.5,91.5,90],
'qualify':['yes','yes','yes','yes','yes']}
exam_data2=pd.DataFrame(exam_dic2)
print("original dataframe:")
print(exam_data1)
print("------------------")
print(exam_data2)
print("\join the said two dataframes along
rows:")
result_data=pd.concat([exam_data1,
exam_data2])
print(result_data)
RESULT: Thus, the program has been executed
successfully
OUTPUT

original dataframe:
name perc qualify
0 Aman 79.5 yes
1 Kamal 29.0 no
2 Amjad 90.5 yes
3 Rohan NaN no
4 Amit 32.0 no
5 Sumit 65.0 yes
6 Matthew 56.0 yes
7 Kartik NaN no
8 Kavita 29.0 no
9 Pooja 89.0 yes
------------------
name perc qualify
0 parveen 89.5 yes
1 ahil 92.0 yes
2 ashaz 90.5 yes
3 shifin 91.5 yes
4 hanash 90.0 yes
\join the said two dataframes along rows:
name perc qualify name perc qualify
0 Aman 79.5 yes parveen 89.5 yes
1 Kamal 29.0 no ahil 92.0 yes
2 Amjad 90.5 yes ashaz 90.5 yes
3 Rohan NaN no shifin 91.5 yes
4 Amit 32.0 no hanash 90.0 yes
5 Sumit 65.0 yes NaN NaN NaN
6 Matthew 56.0 yes NaN NaN NaN
7 Kartik NaN no NaN NaN NaN
8 Kavita 29.0 no NaN NaN NaN
9 Pooja 89.0 yes NaN NaN NaN
7)Q. Write a pandas programs to join the two
given dataframes along columns and assign all
data
AIM: to create a pandas program programs to
join the two given dataframes along columns
and assign all data
SOFTWARE REQUIRED: Spyder Pandas Library
PROGRAM:
import pandas as pd
import numpy as np
exam_dic1 = {'name': ['Aman', 'Kamal', 'Amjad',
'Rohan', 'Amit', 'Sumit', 'Matthew', 'Kartik',
'Kavita', 'Pooja'],
'perc': [79.5, 29, 90.5, np.nan, 32, 65, 56,
np.nan, 29, 89],
'qualify': ['yes', 'no', 'yes', 'no', 'no', 'yes',
'yes', 'no', 'no', 'yes']}
exam_data1=pd.DataFrame(exam_dic1)
exam_dic2={'name':
['parveen','ahil','ashaz','shifin','hanash'], 'perc':
[89.5,92,90.5,91.5,90],
'qualify':['yes','yes','yes','yes','yes']}
exam_data2=pd.DataFrame(exam_dic2)
print("original dataframe:")
print(exam_data1)
print("------------------")
print(exam_data2)
print("\join the said two dataframes along
rows:")
result_data=pd.concat([exam_data1,exam_dat
a2],axis=1)
print(result_data)

RESULT: Thus, the program has been executed


successfully
OUTPUT

original dataframe:
name perc qualify
0 Aman 79.5 yes
1 Kamal 29.0 no
2 Amjad 90.5 yes
3 Rohan NaN no
4 Amit 32.0 no
5 Sumit 65.0 yes
6 Matthew 56.0 yes
7 Kartik NaN no
8 Kavita 29.0 no
9 Pooja 89.0 yes
\dictionary:
name Sukhvir
perc 54
qualify yes
dtype: object
8)Q. Write a pandas programs to append a list
of dictionaries or series to an existing
dataframe and display the combined data
AIM: to create a pandas program programs to
append a list of dictionaries or series to an
existing dataframe and display the combined
data
SOFTWARE REQUIRED: Spyder Pandas Library
PROGRAM:
import pandas as pd
import numpy as np
exam_dic1 = {'name': ['Aman', 'Kamal', 'Amjad',
'Rohan', 'Amit', 'Sumit', 'Matthew', 'Kartik', 'Kavita',
'Pooja'],
'perc': [79.5, 29, 90.5, np.nan, 32, 65, 56,
np.nan, 29, 89],
'qualify': ['yes', 'no', 'yes', 'no', 'no', 'yes', 'yes',
'no', 'no', 'yes']}
exam_data1=pd.DataFrame(exam_dic1)
s=pd.Series(['Sukhvir',54,'yes'],index=['name','perc','
qualify'])
dicts=[{'name':'Krish','perc':45,'qualify':'yes'},
{'name':'kumar','perc':67,'qualify':'yes'}]
print("original dataframe:")
print(exam_data1)
print("\dictionary:")
print(s)
#add series
combined_data=exam_data1.append(s,ignore_inde
x=True,sort=False)
#add dictionary
combined_info=combined_data.append(dicts,ignor
e_index=True,sort=False)
print("\ncombined data:")
#print combined data info
print(combined_info)
RESULT: Thus, the program has been executed
successfully
OUTPUT

0 1
1 3
2 4
3 7
4 8
5 8
6 9
dtype: int32
75th percentile of the series is:::
8.0
9)Q. given a series, print all the elements that
are above the 75th percentile
AIM: to create a pandas program to print all the
elements that are above 75th percentile in a
given series.
SOFTWARE REQUIRED: Spyder Pandas Library
PROGRAM:
import pandas as pd
import numpy as np
s=pd.Series(np.array([1,3,4,7,8,8,9]))
print(s)
res=s.quantile(q=0.75)
print()
print('75th percentile of the series is:::')
print(res)
print()
print('the elements that are above the 75th
percentile::')
print(s[s>res])
OUTPUT

itemcat itemname Expenditure


0 car ford 700000
1 ac hitatchi 50000
2 aircooler symphony 12000
3 washing machine LG 14000
result after following dataframe
10)Q. create a dataframe quarterly sales where
each row contains the item category, itemname
and expenditure. Group the rows by category
AIM: to create a pandas program to print all the
elements that are above 75th percentile in a
given series.
SOFTWARE REQUIRED: Spyder Pandas Library
PROGRAM:
import pandas as pd
dic={'itemcat':['car','ac','aircooler','washing
machine'],'itemname':
['ford','hitatchi','symphony','LG'],
'Expenditure':[700000,50000,12000,14000]}
quartsales=pd.DataFrame(dic)
print(quartsales)
qs=quartsales.groupby('itemcat')
print('result after following dataframe')
print(qs['itemcat','expenditure'].Sum())

RESULT: Thus, the program has been executed


successfully

OUTPUT
class pass_percentage
0 1 100.0
1 2 100.0
2 3 100.0
3 4 100.0
4 5 100.0
5 6 100.0
6 7 100.0
7 8 100.0
8 9 100.0
9 10 98.6
10 11 100.0
11 12 99.0
class int64
pass_percentage float64
dtype: object
shape of the dataframe is:::::::
(12, 2)

11)Q. create a dataframe for examination result


and display row labels, column labels datatypes
of each column and the dimensions
AIM: to display row labels, column labels
datatypes of each column and the dimensions.
SOFTWARE REQUIRED: Spyder Pandas Library
PROGRAM:
import pandas as pd
dic={'class':[1,2,3,4,5,6,7,8,9,10,11,12],
'pass_percentage':
[100,100,100,100,100,100,100,100,100,98.6,10
0,99]}
result=pd.DataFrame(dic)
print(result)
print(result.dtypes)
print('shape of the dataframe is:::::::')
print(result.shape)
OUTPUT

name marksinIP
4 pankaj 98
5 sohit 96
2 deepak 92

11)Q. locate the largest 3 values in a dataframe


AIM: to locate the largest 3 values in a
dataframe
SOFTWARE REQUIRED: Spyder Pandas Library
PROGRAM:
import pandas as pd
dic={'name':
['rohit','mohit','deepak','anil','pankaj','sohit','ge
eta'],
'marksinIP':[85,45,92,85,98,96,84]}
marks=pd.DataFrame(dic)
#find 3 largest value for marks in IP column
print(marks.nlargest(3,['marksinIP']))

RESULT: Thus, the program has been executed


successfully

You might also like