Vector Operations On Pandas Series

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

Vector operations on Pandas Series

#Eg2- Consider the series obj2:

#A- Add 2 to obj2.


print(obj2+2)

#B- Multiply 3 to obj2.


print(obj2*3)

#C- print the 2nd power of obj2.


print(obj2**2)

Retrieving values from series using conditions


(Filtering entries)
You can filter out entries from a Series object using expression
that are of Boolean type(True/False)
#Eg1- Consider the series obj1 and obj2 .
obj1 obj2
#A-Check whether the elements of obj1 greater than 5.
#B-Check whether the elements of obj2 less than 10.
#C-Check whether the elements of obj2 greater than 15.
#A-print(obj1>5) #B-print(obj2<10) #C-print(obj2>15)

Note:- When you apply a comparison operator directly on a Pandas


Series object , then it works like vectorized operation and applies this
check on each individual element of Series object.
#A-Display the elements of obj1 which are greater than 5.
#B- Display the elements of obj2 which are less than or equal to 24.
#A-print(obj1[obj1>5]) #B-print(obj2[obj2<=24])

Note:- But when you apply this check with the Series object inside [ ] ,
you will find that it returns filtered result containing only the values
that return True for the given Boolean expression.
Modifying elements of a series
The data values of a series object can be modified through
item assignment.
The syntax is :
series[index]=new value
or
series[start index:stop index]=new value
#Eg- Consider the series S1,S2,S3,S4 .
S1 S2 S3 S4

#A- Assign the value 1.85 to the index 0 of S1.


S1[0]=1.85 S1.iloc[0]=1.85
print(S1) or print(S1)
#B- Assign the value -15.75 to the indices 2 and 3 of S1.
S1[2:4]=-15.75 S1.iloc[2:4]= -15.75
print(S1) or print(S1)

#C- Assign the value 380.00 to the indices b and d of S2.


S2[['b','d']]=380.00
print(S2) or
S2[1:4:2]=380.00
print(S2) or
S2.iloc[1:4:2]=380.00
print(S2)
#D- Assign the value 500 to the indices 102 to 104 of S3.
S3.loc[102:104]=500
print(S3) or
S3[1:4]=500
print(S3) or
S3.iloc[1:4]=500
print(S3)
#E- Assign the values for apple and orange as 10 of S4.
S3[“Apple”:”Orange]=10
Print(S3) or
S4.loc["Apple":"Orange"]=10
print(S4) or
S4.iloc[:2]=10
print(S4) or
S4[["Apple","Orange"]]=10
print(S4)
Deleting elements from series
Sometimes , you do not need a data value at a particular index.
You can remove that entry using drop().
#Eg- Consider the series S1,S2,S3,S4 .
S1 S2 S3 S4
#A- Delete the data at the index 1 of S1.
print(S1.drop(1))

But if you print the Series S1 , after the previous drop statement,
print(S1)

You can also create a new series element with same name S1,after
drooping the data at index 1.
S1=S1.drop(1)
print(S1)

#B- Delete the data at the index 102 of S3.

print(S3.drop(102))

print(S3.drop(1))

#C- Delete the data of orange from series S4.

print(S4.drop("Orange"))

print(S4.drop(1))

#D- Delete the data at the index ‘b’ and ‘e’ of S2.

print(S2.drop(['b','e']))

Note:- Series object’s values can be modified but size cannot .


You can say that Series objects are value mutable but size immutable.
#Eg2-Consider the series S5:

Write the output:


print(S5.drop(2))

print(S5)

Renaming indexes of a series


1. .index
Syntax is :
seriesname.index=[new index array]

#Eg1- Consider the series S1.


S1

To change the index of the series S1 to [‘a’ ,’b’, ’c’, ’d’], we


write the statement:
S1.index=[‘a’,’b’,’c’,’d’]
print(S1)

#Eg2- Consider the series S2

#A-To change the index of the series S2 to [‘y’ ,’x’, ’z’, ’m’,’n’], we
write the statement:

S2.index=[‘y’ ,’x’, ’z’, ’m’,’n’]


print(S2)
#B-To change the index of the series S2 to [‘y’ ,’x’, ’z’, ’m’]
S2.index=[‘y’ ,’x’, ’z’, ’m’]
print(S2)

Note:- The size of new index array must match with existing index
array’s size.

2. Reindexing
Sometimes you need to create a similar object but with a
different order of same indexes. You can use reindexing for
the same purpose.
Syntax is :
seriesname = seriesname.reindex=[new index array]
#Eg1- Consider the series S2

#A-To change the order of the index as ['e','d','c','b','a'].


Ser2=S2.reindex(['e','d','c','b','a'])
print(Ser2)

print(S2)

#Eg2- Write the output


Output:
print(S1) print(S2)

Difference between Series and Lists(Reader Page :28)


Series object Lists
1 One dimensional Can be one dimensional and even
multi dimensional with nested lists
in it.
2 It can have numeric indexes It can take numeric indexes only.
as well as labels
3 Indexes can be duplicate Indexes cannot be duplicate
4 Homogeneous Heterogeneous elements-Lists can
elements(elements of same store elements of different data
data type)i.e., values may types.
be different but their
datatype is the same for
each element.
Difference between Series and Dictionaries(Reader Page :29)
Series object Dictionaries
1 One dimensional. Can be one dimensional and even
multi dimensional with nested
dictionaries in it as values.
2 It can be thought of as It can be thought of as similar to
similar to dictionaries as series as its keys can be considered
dictionaries store values equivalent to that of indexes/labels
against keys, series stores of Series .
values against
indexes/labels .
3 Indexes can be numbers or Keys can be of immutable type
labels only. only.
Difference between Series and Numpy Array(Reader Page :29)
Series object Numpy Array
1 can store elements of can store elements of different
different data types. data types.
2 Can have indexes Can have only numeric index.
numeric/strings.
3 Can perform mathematical Cannot perform mathematical
operations on two series operations on two nd arrays only if
objects , even if their shapes their shapes match.
are different by using NaN
for non matching indexes.
4 It takes more memory It takes lesser memory compared
compared to a numpy array. to a Series object .

You might also like