0% found this document useful (0 votes)
33 views16 pages

Untitled Document

The document discusses creating and manipulating series objects in Pandas. It demonstrates how to: 1. Create series from lists, dictionaries, NumPy arrays, and by specifying index values. Various attributes of series like index, values, dtype are explored. 2. Perform arithmetic operations like addition, subtraction etc. on series elementwise. Functions like head and tail are used to view the start and end elements. 3. Slice series using standard Python slicing syntax and filter values using conditions. Values are updated by applying functions like square. 4. Vector operations like sum, subtract, multiply etc. are applied elementwise on the series. NaN values are added and checked for existence. Ordering of elements by index and

Uploaded by

nihalparteti26
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)
33 views16 pages

Untitled Document

The document discusses creating and manipulating series objects in Pandas. It demonstrates how to: 1. Create series from lists, dictionaries, NumPy arrays, and by specifying index values. Various attributes of series like index, values, dtype are explored. 2. Perform arithmetic operations like addition, subtraction etc. on series elementwise. Functions like head and tail are used to view the start and end elements. 3. Slice series using standard Python slicing syntax and filter values using conditions. Values are updated by applying functions like square. 4. Vector operations like sum, subtract, multiply etc. are applied elementwise on the series. NaN values are added and checked for existence. Ordering of elements by index and

Uploaded by

nihalparteti26
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/ 16

PRACTICAL 7

CREATION OF LIST OBJECT


AIM: To create a series object by various method.
THEORY: We can create series object in python by various methods like with the help of list,
dictionary, tile, linspace etc.
CODE:-
import pandas as pd
import numpy as np

#by giving a list as data.


a=[1,2,3,4,5]
s=pd.Series(a)
print(s)

#by giving a dictionary as data.


b={"a":1,"b":2,"c":3,"d":4}
s1=pd.Series(b)
print(s1)

#by giving arrange values as data.


c=np.arange(1,10,2)
s2=pd.Series(c)
print(s2)

#by giving linspace value as a data.


d=np.linspace(24,64,5)
s3=pd.Series(d)
print(s3)

#by giving tile value as a data.


e=np.tile([3,5],2)
s4=pd.Series(e)
print(s4)

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)

print("ADDITION WITHOUT FUNCTION","=",s3)


print("ADDITION WITH FUNCTION","=",s4)
print("SUBTRACTION WITHOUT FUNCTION","=",s5)
print("SUBTRACTION WITHOUT FUNCTION","=",s6)
print("MULTIPLY WITHOUT FUNCTION","=",s7)
print("MULTIPLY WITHOUT FUNCTION","=",s8)
print("DIVIDE WITHOUT FUNCTION","=",s9)
print("DIVIDE WITHOUT FUNCTION","=",s10)
print("MODULUS WITHOUT FUNCTION","=",s11)
print("MODULUS WITHOUT FUNCTION","=",s12)

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

ADDITION WITH FUNCTION = 0 5.0


1 7.0
2 9.0
3 8.0
4 7.0
5 NaN
dtype: float64
SUBTRACTION WITHOUT FUNCTION = 0 -3.0
1 -3.0
2 -3.0
3 0.0
4 3.0
5 NaN
dtype: float64
SUBTRACTION WITHOUT FUNCTION = 0 -3.0
1 -3.0
2 -3.0
3 0.0
4 3.0
5 NaN
dtype: float64
MULTIPLY WITHOUT FUNCTION = 0 4.0
1 10.0
2 18.0
3 16.0
4 10.0
5 NaN
dtype: float64
MULTIPLY WITHOUT FUNCTION = 0 4.0
1 10.0
2 18.0
3 16.0
4 10.0
5 NaN
dtype: float64
DIVIDE WITHOUT FUNCTION = 0 0.25
1 0.40
2 0.50
3 1.00
4 2.50
5 NaN
dtype: float64
DIVIDE WITHOUT FUNCTION = 0 0.25
1 0.40
2 0.50
3 1.00
4 2.50
5 NaN
dtype: float64
MODULUS WITHOUT FUNCTION = 0 1.0
1 2.0
2 3.0
3 0.0
4 1.0
5 NaN
dtype: float64
MODULUS WITHOUT FUNCTION = 0 1.0
1 2.0
2 3.0
3 0.0
4 1.0
5 NaN
dtype: float64
0 NaN
1 3.0
2 14.0
3 NaN
4 NaN
dtype: float64
0 11.0
1 3.0
2 14.0
3 18.0
4 17.0
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

greater than = 0 False


1 False
2 False
3 False
4 False
5 False
dtype: bool

less than = 0 False


1 False
2 False
3 False
4 False
5 False
dtype: bool

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.

You might also like