Untitled Document
Untitled Document
OUTPUT:-
for list
dtype: int64
for dictionary
a 1
b 2
c 3
d 4
dtype: int64
for arange
0 1
1 3
2 5
3 7
4 9
dtype: int32
for linspace
0 24.0
1 34.0
2 44.0
3 54.0
4 64.0
dtype: float64
for tile
0 3
1 5
2 3
3 5
dtype: int32
Observation-From this we come to know that we can create a series object by giving any
sequence value as a data.
PRACTICAL 8
Study of attributes of series data object
AIM:- To use series object attributes in given series.
THEORY:- When we create a series type object, all information real
CODE:
import pandas as pd
s=pd.Series(["jan","feb","mar","apr"],index=range(1,5))
s.name="Mys"
s.index.name="months."
print("index","=",s.index)
print("values","=",s.values)
print("dtype","=",s.dtype)
print("shape",'=',s.shape)
print("nbytes","=",s.nbytes)
print("dimension","=",s.ndim)
print("size","=",s.size)
print("nan value check","=",s.hasnans)
print("CHECK if series is empty or not","=",s.empty)
print(s)
OUTPUT:
index = RangeIndex(start=1, stop=5, step=1, name='months.')
values = ['jan' 'feb' 'mar' 'apr']
dtype = object
shape = (4,)
nbytes = 32
dimension = 1
size = 4
nan value check = False
CHECK if series is empty or not = False
months.
1 jan
2 feb
3 mar
4 apr
Name: Mys, dtype: object
OBSERVATION:- With the help of these attributes we will be able to obtain all the necessary
details about the series.
PRACTICAL 9
ARITHMETIC OPERATION OF SERIES
OBJECT
AIM: To use all the arithmetical operations on two series objects.
THEORY: We can perform arithmetic like addition, subtraction, division etc. with two series
objects and it will calculate the result on two corresponding items of the two objects.
CODE:
import pandas as pd
s1=pd.Series([1,2,3,4,5,6])
s2=pd.Series([4,5,6,4,2])
s3=s1+s2
s4=s1.add(s2)
s5=s1-s2
s6=s1.subtract(s2)
s7=s1*s2
s8=s1.multiply(s2)
s9=s1/s2
s10=s1.div(s2)
s11=s1%s2
s12=s1.mod(s2)
a=pd.Series([1,2,5],index=range(3))
b=pd.Series([1,9,8,7],index=[1,2,3,4])
c=a.add(b)
print(c)
c=a.add(b,fill_value=10)
print(c)
OUTPUT:-
ADDITION WITHOUT FUNCTION = 0 5.0
1 7.0
2 9.0
3 8.0
4 7.0
5 NaN
dtype: float64
PRACTICAL 10
ADDING AND VERIFYING NULL VALUES IN SERIES OBJECT
AIM: To add and check the presence of null value in series.
THEORY: We can able to add NaN value in series object by using numpy code.
CODE:
import numpy as np
import pandas as pd
s1=pd.Series([3,4,5,"np.NaN",5])
print("nan as string","=",s1.hasnans)
s2=pd.Series([3,4,5,np.NaN,5])
print("nan as value","=",s2.hasnans)
OUTPUT:
nan as string = False
nan as value = True
OBSERVATION: From these code we come to know that when we give NaN value in string
hasnans function do not count it as a NaN value.
PRACTICAL 11
SLICING OPERATIONS ON SERIES
Aim: To slice the series object according to their position index.
THEORY: Like other sequences, you can extract slice too form a series object to retrieve
subsets.
CODE:
import pandas as pd
s1=pd.Series([1,9,8,6,7,5,2],index=['a','b','c','d','e','f','g'])
print(s1)
print(s1[2:5])
print(s1['a':'c'])
print(s1[3:])
print(s1[1:7:3])
print(s1[:2])
print(s1[::4])
print(s1[13:2:-4])
OUTPUT:
a 1
b 9
c 8
d 6
e 7
f 5
g 2
dtype: int64
c 8
d 6
e 7
dtype: int64
a 1
b 9
c 8
dtype: int64
d 6
e 7
f 5
g 2
dtype: int64
b 9
e 7
dtype: int64
a 1
b 9
dtype: int64
a 1
e 7
dtype: int64
g 2
dtype: int64
OBSERVATION: From the above code we come to know that we can also slice the series
object like other sequences.
PRACTICAL 12
UPDATE VALUE OF SERIES OBJECT BY THEIR SQUARE
AIM: Write a program to update the value of a series object by their square.
CODE:
import pandas as pd
a=range(0,5)
s=pd.Series([2,3,4,5,6],index=a)
for i in (a):
s[i]=s[i]**2
print(s)
OUTPUT:
0 4
1 9
2 16
3 25
4 36
dtype: int64
OBSERVATION: All the values of the series object are getting update by their square.
PRACTICAL 13
VECTOR OPERATIONS
AIM: To use all the vector operation on a series object.
THEORY: Vector operations mean that if you apply a function or expression then it is
individually applied on each item of the object.
CODE:
import pandas as pd
s=pd.Series([2,3,4,5,4,3])
print(s)
print("sum","=",s+12)
print("sub","=",s-3)
print("multi","=",s*4)
print("divide","=",s/5)
print("greater than","=",s>5)
print("less than","=",s<2)
print("mod","=",s%3)
print("power","=",s**3)
OUTPUT:
0 2
1 3
2 4
3 5
4 4
5 3
dtype: int64
sum = 0 14
1 15
2 16
3 17
4 16
5 15
dtype: int64
sub = 0 -1
1 0
2 1
3 2
4 1
5 0
dtype: int64
multi = 0 8
1 12
2 16
3 20
4 16
5 12
dtype: int64
divide = 0 0.4
1 0.6
2 0.8
3 1.0
4 0.8
5 0.6
dtype: float64
mod = 0 2
1 0
2 1
3 2
4 1
5 0
dtype: int64
power = 0 8
1 27
2 64
3 125
4 64
5 27
dtype: int64
PRACTICAL 14
HEAD AND TAIL FUNCTION
AIM: To apply head and tail function on a series object.
THEORY: The head() function is used to fetch first n rows from a Pandas object and tail()
function returns last n rows from a Pandas object.
CODE:
import pandas as pd
import numpy as np
s1=pd.Series([1,2,3,4,5,6])
s2=pd.Series([4,5,6,4,2])
print(s1.head)
print(s2.head)
print(s1.head(7))
print(s1.tail)
print(s2.tail(4))
print(s2.tail)
OUTPUT:
<bound method NDFrame.head of 0 1
1 2
2 3
3 4
4 5
5 6
dtype: int64>
<bound method NDFrame.head of 0 4
1 5
2 6
3 4
4 2
dtype: int64>
0 1
1 2
2 3
3 4
4 5
5 6
dtype: int64
<bound method NDFrame.tail of 0 1
1 2
2 3
3 4
4 5
5 6
dtype: int64>
1 5
2 6
3 4
4 2
dtype: int64
<bound method NDFrame.tail of 0 4
1 5
2 6
3 4
4 2
dtype: int64>
OBSERVATION: The head() function is used to fetch first n rows from a Pandas object and
tail() function returns last n rows from a Pandas object
PRACTICAL 15
ORDERING ELEMENT IN SERIES OBJECT
AIM: To order element of series according to their index and value.
CODE:-
import pandas as pd
l=[2567,53778,45,35,10,67,879,345, 0,89]
b=pd.Series(l)
c=b.sort_values()
print(c)
8 0
4 10
3 35
2 45
5 67
9 89
7 345
6 879
0 2567
1 53778
dtype: int64
d=b.sort_values(ascending=False) print(d)
1 53778
0 2567
6 879
7 345
9 89
5 67
2 45
3 35
4 10
dtype: int64
e=b.sort_index()
print(e)
0 2567
1 53778
2 45
3 35
4 10
5 67
6 879
7 345
8 0
9 89
dtype: int64
f=b.sort_index(ascending=False) print(f)
9 89
8 0
7 345
6 879
5 67
4 10
3 35
2 45
1 53778
0 2567
dtype: int64
PRACTICAL16
Ordering the values of series as
per index wise and values itself in
the given series object.
AIM :-Study of value filter in series object
import pandas as pd
a=[1,3,2,7,45,12,87,12,24] s=pd.Series(a)
print(s>10)
0 False
1 False
2 False
3 False
4 True
5 True
6 True
7 True
8 True
dtype: bool
print(s[s>15]) 4 45
6 87
8 24
dtype: int64
print(s[s<15]) 0 1
1 3
2 2
3 7
5 12
7 12
dtype: int64
OBSERVATION :This code giving the value True or false . if written in detail
it will give the result from the series object element wise.