Professional Documents
Culture Documents
Lab Programs
Lab Programs
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)])
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)
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
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)
name marksinIP
4 pankaj 98
5 sohit 96
2 deepak 92