2. Data Handling using Pandas – 1 Syllabus
Series: Creation of Series from – ndarray, dictionary, scalar value;
mathematical operations; Head and Tail functions; Selection,
Indexing and Slicing.
1
Sangita Panchal
3. Series
A pandas Series is a one-dimensional array
with homogeneous data.
The axis labels are collectively called Index.
The general format:
pandas. Series (data, index, dtype)
data: It is like ndarray, list, constants
index: It is unique, by default 0..n-1 (np.arange(n))
dtype: data type
2
Sangita Panchal
Key Features:
• Homogeneous data
• Size immutable
• Values of data mutable
4. Creation of Series
A series can be created using: array, dict, scalar value or constant.
3
Sangita Panchal
import pandas as pd
s = pd.Series()
print(s)
Series([], dtype: float64)
import pandas as pd
import numpy as np
data = np.array(['a','b','c','d'])
s = pd.Series(data)
print(s) 0 a
1 b
2 c
3 d
dtype: object
Default
Index
import pandas as pd
data = ['a','b','c','d']
s = pd.Series(data)
print(s)
5. Creation of Series 4
Sangita Panchal
import pandas as pd
data = 100
s = pd.Series(data,index = [1,2,3])
print(s)
import pandas as pd
data = {'a':1,'b':2,'c':3}
s = pd.Series(data)
print(s)
a 1
b 2
c 3
1 100
2 100
3 100
Key is declared
as Index
Defined your
own Index
6. Creation of Series – give a name to the series 5
Sangita Panchal
a.name = "Marks"
print(a)
import pandas as pd
data = [20,25,19,12,17]
a = pd.Series(data,name = "Rollno")
print(a)
0 20
1 25
2 19
3 12
4 17
Name: Rollno, dtype: int64
0 20
1 25
2 19
3 12
4 17
Name: Marks, dtype: int64
Series name is Rollno
Series name is Marks
7. Pandas Series – Created by naming index, data
import pandas as pd
s = pd.Series(index = ['Ram','Joe','Nia'],data = [1,2,3])
s1 = pd.Series([1,2,3],['Ram','Joe','Nia'])
print(s)
print(s1)
6
Sangita Panchal
Ram 1
Joe 2
Nia 3
dtype: int64
Output
8. Pandas Series – isnull()
import pandas as pd
data = {'apples':500,'kiwi':20,'oranges':100}
a = ['apples’, 'banana’, 'kiwi’, 'oranges']
b = pd.Series(data, index = a)
print(b)
print(b. isnull())
7
Sangita Panchal
apples 500.0
banana NaN
kiwi 20.0
oranges 100.0
dtype: float64
apples False
banana True
kiwi False
oranges False
dtype: bool
Output
10. Arithmetic operations on Series (Scalar /Vector)
performed on all the values in a Series
9
Sangita Panchal
Operation Function/Method
Add all values sum()
Multiply all values prod()
Mean of all values mean()
Maximum max()
Minimum min()
Count Non- NA/Null values count()
return N elements from top(default is 5) head()
return N elements from bottom(default is 5) tail()
return sorted series (ascending = True / False) sort_values()
11. Pandas Series (max, min, count, sum, mean)
import pandas as pd
data = [11, 9, 24, 17,15]
a = pd.Series(data)
print(data)
print("Total",a.sum())
print("Mean",a.mean())
print("Maximum",a.max())
print("Minimum",a.min())
print("Count",a.count())
10
Sangita Panchal
[11, 9, 24, 17, 15]
Total 76
Mean 15.2
Maximum 24
Minimum 9
Count 5
import pandas as pd
data = [11, 9, None, 17,15]
a = pd.Series(data)
print(data)
print("Total",a.sum())
print("Mean",a.mean())
print("Maximum",a.max())
print("Minimum",a.min())
print("Count",a.count())
[11, 9, None, 17, 15]
Total 52.0
Mean 13.0
Maximum 17.0
Minimum 9.0
Count 4
Output
12. Arithmetic operations on Series(Scalar/Vector)
import pandas as pd
s = pd.Series([18,None,23],['Ram','Joe','Nia’])
value = 10
print(s)
print(s+value)
11
Sangita Panchal
Ram 18.0
Joe NaN
Nia 23.0
dtype: float64
Ram 28.0
Joe NaN
Nia 33.0
dtype: float64
Output
13. Arithmetic operations on Series(Scalar/Vector)
import pandas as pd
s = pd.Series([18,None,23],['Ram','Joe','Nia'])
s1 = pd.Series([23,25,19],['Ram','Joe','Nia'])
s2 = s + s1
print(s2)
s = s.add(s1,fill_value = 0)
print(s)
12
Sangita Panchal
Ram 41.0
Joe NaN
Nia 42.0
dtype: float64
Ram 41.0
Joe 25.0
Nia 42.0
dtype: float64
Output
14. append() and drop()
import pandas as pd
data = [23,19,21]
index = ['Ram','Joe','Nia']
comp1 = pd.Series(data,index)
comp2 = pd.Series([20],['Mona'])
comp = comp1.append(comp2)
print(comp)
comp['Joe'] = 24
comp.loc['Nia'] = 25
print(comp)
comp = comp.drop(['Ram','Mona'])
print(comp)
13
Sangita Panchal
Ram 23
Joe 19
Nia 21
Mona 20
dtype: int64
Ram 23
Joe 24
Nia 25
Mona 20
dtype: int64
Joe 24
Nia 25
dtype: int64
Output
15. Add two Series
import pandas as pd
sub1 = [23,19,21]
sub2 = [12,23,19]
a = pd.Series(sub1)
print(a)
b = pd.Series(sub2)
print(b)
total = a + b
c = pd.Series(total)
print(c)
per = total/50 * 100
print(per)
print(per.sort_values(ascending = False))
14
Sangita Panchal
0 23
1 19
2 21
dtype: int64
0 12
1 23
2 19
dtype: int64
0 35
1 42
2 40
dtype: int64
Output
0 70.0
1 84.0
2 80.0
dtype: float64
1 84.0
2 80.0
0 70.0
dtype: float64
18. Accessing data from Series (Indexing and Slicing) 17
Sangita Panchal
import pandas as pd
data = [1,2,3]
s = pd.Series(data,index = ['Ram','Joe','Nia'])
print(s)
print(s[1])
print(s[1:2])
print(s['Ram'])
print(s[['Ram','Joe']])
Ram 1
Joe 2
Nia 3
dtype: int64
2
Joe 2
dtype: int64
1
Ram 1
Joe 2
dtype: int64
Output
20. CBSE QUESTIONS
1. Given a Pandas series called Sequences, the command which will display the first 4 rows
is __________________.
a. print(Sequences.head(4))
b. print(Sequences.Head(4))
c. print(Sequences.heads(4)
d. print(Sequences.Heads(4))
2. Given the following Series S1 and S2:
Write a command to find the sum of the series.
19
Sangita Panchal
21. CBSE QUESTIONS
1. Given a Pandas series called Sequences, the command which will display the first 4 rows
is __________________.
a. print(Sequences.head(4))
b. print(Sequences.Head(4))
c. print(Sequences.heads(4)
d. print(Sequences.Heads(4))
2. Given the following Series S1 and S2:
Write a command to find the sum of the series.
20
Sangita Panchal
print(Sequences.head(4))
print(S1 + S2)
print(S1.add(S2))
22. CBSE QUESTIONS
3. Consider a given Series , M1:
Write a program in Python Pandas to create the series.
.
4. Consider the following Series Object S_amt:
ii. Write the command which will display the name of the furniture having rent>250.
ii. Write the command to name the series as Furniture.
21
Sangita Panchal
23. CBSE QUESTIONS
3. Consider a given Series , M1:
Write a program in Python Pandas to create the series.
.
4. Consider the following Series Object S_amt:
ii. Write the command which will display the name of the furniture having rent>250.
ii. Write the command to name the series as Furniture.
22
Sangita Panchal
import pandas as pd
Marks = [45,65,24,89]
index = [‘Term1’,’Term2’,’Term3’,’Term4’]
M1 = pd.Series(Marks, index)
print(S_amt[S_amt > 250])
S_amt.name = ‘Furniture’
24. CBSE QUESTIONS
5. Consider two objects x and y. x is a list whereas y is a Series. Both have values
20, 40,90, 110.
What will be the output of the following two statements considering that the
above objects have been created already
a. print (x*2)
b. print(y*2)
Justify your answer.
23
Sangita Panchal
25. CBSE QUESTIONS
5. Consider two objects x and y. x is a list whereas y is a Series. Both have values
20, 40,90, 110.
What will be the output of the following two statements considering that the
above objects have been created already
a. print (x*2)
b. print(y*2)
Justify your answer.
24
Sangita Panchal
x is a list
[20,40,90,110, 20,40,90,110]
y is a series
0 40
1 80
2 180
3 220
26. CBSE QUESTIONS
Given the following Series eng and maths, answer the questions that follow:
eng maths
(i) Create a new series total by adding the corresponding values of both series.
The NaN will be added as zero (0).
(ii) Create a new series avg to store the average of eng and maths marks.
25
Sangita Panchal
27. CBSE QUESTIONS
Given the following Series eng and maths, answer the questions that follow:
eng maths
(i) Create a new series total by adding the corresponding values of both series.
The NaN will be added as zero (0).
(ii) Create a new series avg to store the average of eng and maths marks.
avg = total / 2
26
Sangita Panchal
total = eng.add(maths,fill_value=0)
avg = total / 2