# Class 12 Informatics Practices Data Handling using pandas – I NCERT Solution

### NCERT Exercise Solution of Class 12 Informatics Practices

#### 1. What is a Series and how is it different from a 1-D array, a list and a dictionary?

Answer: A Series is a one-dimensional array containing a sequence of values of any data type (int, float, list, string, etc) which by default have numeric data labels starting from zero.

The data label associated with a particular value is called its index.

Series vs List

Series vs Dictionary

Series vs NumPy Array

#### 2. What is a DataFrame and how is it different from a 2-D array?

Answer: A DataFrame is a two-dimensional labelled data structure like a table of MySQL.

It contains rows and columns, and therefore has both a row and column index. Each column can have a different type of value such as numeric, string, boolean, etc., as in tables of a database.

Pandas store such tabular data using a DataFrame.

Dataframe vs 2-D Array (2D ndarray)

#### 3. How are DataFrames related to Series?

Answer: Dataframe is the collection of Series. Individual columns of dataframe’s can be considered equivalent to Series object.

Differences:-

(A) Individual columns of dataframe are size-mutable, while series are not a size mutable.

(B) Dataframe can contain heterogeneous data while Series can contain homogeneous data.

#### 4. What do you understand by the size of (i) a Series, (ii) a DataFrame?

Answer: (i) size of Series means number of elements in Series. size is an attribute of Series object, which returns number of elements.

seriesObject.size

Example:

>>> import pandas as pd
>>> s = pd.Series([1,2,3])
>>> s
0 1
1 2
2 3
dtype: int64
>>> s.size
3

Answer: (ii) size of Dataframe means number of elements in Dataframe. size is an attribute of DataFrame object, which returns number of elements.

dataframeObject.size

Example:

>>> import pandas as pd

>>> d = pd.DataFrame([[1,2,3],[4,5,6]])
>>> d
0 1 2
0 1 2 3
1 4 5 6
>>> d.size
6

#### 5. Create the following Series and do the specified operations:

##### Answer: Method -1

>>> import pandas as pd

>>> list1 = [chr(n) for n in range(97, 123)]

>>> EngAlph = pd.Series(list1)

>>> print(EngAlph)

##### Method-2

>>> import pandas as pd

>>> import string

>>> list1 = list(string.ascii_lowercase)

>>> EngAlph = pd.Series(list1)

>>> EngAlph

##### b) Vowels, having 5 elements with index labels ‘a’, ‘e’, ‘i’, ‘o’ and ‘u’ and all the five values set to zero. Check if it is an empty series.

>>> import pandas as pd

>>> Vowels = pd.Series(0, index = [‘a’, ‘e’, ‘i’, ‘o’, ‘u’])

>>> Vowels

>>> Vowles.empty

False #Not Empty

##### c) Friends, from a dictionary having roll numbers of five of your friends as data and their first name as keys.

>>> import pandas as pd

>>> dict = {‘Suman’ : 1, ‘Raman’ : 2, ‘Anjeev’ : 3 , ‘Singh’ : 4, ‘Mohit’: 5}

>>> Friends = pd.Series(dict)

>>> Friends

Suman 1
Raman 2
Anjeev 3
Singh 4
Mohit 5
dtype: int64

##### d) MTseries, an empty Series. Check if it is an empty series.

>>> import pandas as pd

>>> MTseries = pd.Series([])
# pd.Series( ) is also valid for creating empty Series, but it raise DeprecationWarning

>>> MTSeries.empty

True

##### e) MonthDays, from a numpy array having the number of days in the 12 months of a year. The labels should be the month numbers from 1 to 12.

>>> import numpy as np

>>> import pandas as pd

>>> monthDaysAr = np.array([31,28,31,30,31,30,31,31,30,31,30,31])

>>> MonthDays = pd.Series(monthDaysAr, index = range(1,13))

>>> MonthDays

#### 6. Using the Series created in Question 5, write commands for the following:

##### a) Set all the values of Vowels to 10 and display the Series.

>>> Vowels[ : ] = 10

>>> Vowels

a 10

e 10

i 10

o 10

u 10

#### b) Divide all values of Vowels by 2 and display the Series.

>>> Vowels = Vowels / 2

>>> Vowels

a 5.0

e 5.0

i 5.0

o 5.0

u 5.0

#### c) Create another series Vowels1 having 5 elements with index labels ‘a’, ‘e’, ‘i’, ‘o’ and ‘u’ having values [2,5,6,3,8] respectively.

>>> import pandas as pd

>>> Vowels1 = pd.Series([2, 5, 6, 3, 8] , index = [‘a’, ‘e’, ‘i’, ‘o’, ‘u’])

>>> Vowels1

a 2

e 5

i 6

o 3

u 8

##### d) Add Vowels and Vowels1 and assign the result to Vowels3.

>>> Vowels3 = Vowels + Vowels1

>>> Vowels3

a 7.0

e 10.0

i 11.0

o 8.0

u 13.0

##### e) Subtract, Multiply and Divide Vowels by Vowels1.

>>> Vowels – Vowels1

a 3.0

e 0.0

i -1.0

o 2.0

u -3.0

>>> Vowels * Vowels1

a 10.0

e 25.0

i 30.0

o 15.0

u 40.0

>>> Vowels / Vowels1

a 2.500000

e 1.000000

i 0.833333

o 1.666667

u 0.625000

##### f) Alter the labels of Vowels1 to [‘A’, ‘E’, ‘I’, ‘O’, ‘U’].

>>> Vowels1.index = [‘A’, ‘E’, ‘I’, ‘O’, ‘U’]

>>> Vowles1

A 2

E 5

I 6

O 3

U 8

#### 7. Using the Series created in Question 5, write commands for the following:

##### a) Find the dimensions, size and values of the Series: EngAlph, Vowels, Friends, MTseries, MonthDays.

Series EngAlph

>>> EngAlph.shape

(26, )

>>> EngAlph.size

26

>>> EngAlph.values

array([‘a’, ‘b’, ‘c’, ‘d’, ‘e’, ‘f’, ‘g’, ‘h’, ‘i’, ‘j’, ‘k’, ‘l’, ‘m’,
‘n’, ‘o’, ‘p’, ‘q’, ‘r’, ‘s’, ‘t’, ‘u’, ‘v’, ‘w’, ‘x’, ‘y’, ‘z’],
dtype=object)

Series Vowels

>>> print(Vowels.shape)

(5,)

>>> print(Vowels.size)

5

>>> print(Vowels.values)

[5. 5. 5. 5. 5.]

>>> Vowels.values

array([5., 5., 5., 5., 5.])

Series Friends

>>> print(Friends.shape)

(5,)

>>> print(Friends.size)

5

>>> print(Friends.values)

[1 2 3 4 5]

Series MTSeries

>>> print(MTseries.shape)

(0,)

>>> print(MTseries.size)

0

>>> print(MTseries.values)

[]

Serries MonthDays

>>> print(MonthDays.shape)

(12,)

>>> print(MonthDays.size)

12

>>> print(MonthDays.values)

[31 28 31 30 31 30 31 31 30 31 30 31]

##### b) Rename the Series MTseries as SeriesEmpty.

>>> MTseries.rename(“SeriesEmpty”)

Series([], Name: SeriesEmpty, dtype: float64)

##### c) Name the index of the Series MonthDays as monthno and that of Series Friends as Fname.

>>> MonthDays.index.name = ‘monthno’

>>> Friends.index.name = ‘Fname’

##### d) Display the 3rd and 2nd value of the Series Notes Friends, in that order.

>>> Friends[2 : 0 : -1]

Anjeev 3
Raman 2
dtype: int64

##### e) Display the alphabets ‘e’ to ‘p’ from the Series EngAlph.

>>> EngAlph[4 : 16]
4 e
5 f
6 g
7 h
8 i
9 j
10 k
11 l
12 m
13 n
14 o
15 p
dtype: object

##### f) Display the first 10 values in the Series EngAlph.

0 a
1 b
2 c
3 d
4 e
5 f
6 g
7 h
8 i
9 j
dtype: object

Note : EngAlph[0:10] -> Gives the same output

##### g) Display the last 10 values in the Series EngAlph.

>>> EngAlph.tail(10)
16 q
17 r
18 s
19 t
20 u
21 v
22 w
23 x
24 y
25 z
dtype: object

Note : EngAlph[16:26] , will give the same output

##### h) Display the MTseries.

>>> print(MTseries)
Series([], dtype: float64)

MonthDays[2:7]
3 31
4 30
5 31
6 30
7 31
dtype: int32

MonthDays[::-1]
12 31
11 30
10 31
9 30
8 31
7 31
6 30
5 31
4 30
3 31
2 28
1 31
dtype: int32

#### 9. Create the following DataFrame Sales containing year wise sales figures for five sales persons in INR. Use the years as column labels, and sales person names as row labels.

salesDict = {
2014 : {‘Madhu’ : 100.5, ‘Kusum’ : 150.8, ‘Kinshuk’ : 200.9, ‘Ankit’:30000, ‘Shruti’: 40000},
2015 : {‘Madhu’ : 12000, ‘Kusum’ : 18000, ‘Kinshuk’ : 22000, ‘Ankit’:30000, ‘Shruti’: 45000},
2016 : {‘Madhu’ : 20000, ‘Kusum’ : 50000, ‘Kinshuk’ : 70000, ‘Ankit’:100000, ‘Shruti’: 125000}, 2017 : {‘Madhu’ : 50000, ‘Kusum’ : 60000, ‘Kinshuk’ : 70000, ‘Ankit’:80000, ‘Shruti’: 90000}
}
Sales = pd.DataFrame(salesDict)
print(Sales)

Output:
2014 2015 2016 2017
Madhu 100.5 12000 20000 50000
Kusum 150.8 18000 50000 60000
Kinshuk 200.9 22000 70000 70000
Ankit 30000.0 30000 100000 80000
Shruti 40000.0 45000 125000 90000

#### 10. Use the DataFrame created in Question 9 above to do the following:

##### a) Display the row labels of Sales.

Index([‘Madhu’, ‘Kusum’, ‘Kinshuk’, ‘Ankit’, ‘Shruti’], dtype=’object’)

##### b) Display the column labels of Sales.

Int64Index([2014, 2015, 2016, 2017], dtype=’int64′)

2014 float64
2015 int64
2016 int64
2017 int64
dtype: object

##### d) Display the dimensions, shape, size and values of Sales.

Answer: >>> Sales.ndim, Sales.shape, Sales.size, Sales.values

(2, (5, 4), 20, array([[1.005e+02, 1.200e+04, 2.000e+04, 5.000e+04],
[1.508e+02, 1.800e+04, 5.000e+04, 6.000e+04],
[2.009e+02, 2.200e+04, 7.000e+04, 7.000e+04],
[3.000e+04, 3.000e+04, 1.000e+05, 8.000e+04],
[4.000e+04, 4.500e+04, 1.250e+05, 9.000e+04]]))

##### e) Display the last two rows of Sales.

>>> Sales.iloc[3:,]
2014 2015 2016 2017
Ankit 30000.0 30000 100000 80000
Shruti 40000.0 45000 125000 90000

Method 2:

Sales.tail(2)
2014 2015 2016 2017
Ankit 30000.0 30000 100000 80000
Shruti 40000.0 45000 125000 90000

False

#### 11. Use the DataFrame created in Question 9 above to do the following:

##### Answer: #For Transposed Sales

SalesT.loc[:, ‘Sumit’] = [196.2, 37800, 52000, 78438, 38852]

#For Actual Sales

Sales.loc[‘Sumit’,:] = [196.2, 37800, 52000, 78438, 38852]

##### l) Read the data in the file SalesFigures.csv into a DataFrame SalesRetrieved and Display it. Now update the row labels and column labels of SalesRetrieved to be the same as that of Sales.

>>> SalesRetrived = pd.read_csv(“d:\\MyPythonProgram\\SalesFigures.csv”, names = [2014, 2015, 2016, 2017, 2018])

>>> Sales.Retrived = SalesRetrived.rename(index = {0 : ‘Madhu’, 1 : ‘Kusum’, 2 : ‘Kinshuk’, 3 : ‘Ankit’, 4 : ‘Shruti’, 5 : ‘Sumeet’} )

#### Class 12 Informatics Practices : NCERT Exercise Solutions

You cannot copy content of this page

Scroll to Top