0% found this document useful (0 votes)
4 views10 pages

Chapter 2 Data handling using Pandas - I

The document provides NCERT solutions for Chapter 2 on Data Handling using Pandas for class 12 Informatics Practices. It covers theoretical questions about Series and DataFrames, practical exercises for creating and manipulating Series and DataFrames, and various commands for data analysis. The document concludes with examples of data operations and file handling using Pandas.

Uploaded by

moammed
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
4 views10 pages

Chapter 2 Data handling using Pandas - I

The document provides NCERT solutions for Chapter 2 on Data Handling using Pandas for class 12 Informatics Practices. It covers theoretical questions about Series and DataFrames, practical exercises for creating and manipulating Series and DataFrames, and various commands for data analysis. The document concludes with examples of data operations and file handling using Pandas.

Uploaded by

moammed
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 10

Downloaded from www.tutorialaicsip.

com
NCERT Solutions Chapter 2 Data handling using pandas I

Data handling using pandas class 12 NCERT solutions i.e. Chapter 2 of Informatics Practices
class 12. Let us start!

Data handling using pandas class 12 NCERT solutions

The exercise of Data handling using pandas class 12 NCERT solutions starts with few
theoretical questions. Let us begin!

The first section of Chapter 2 Data handling using pandas class 12 NCERT solutions consists
of the questions based on series and data frame. So let's have a look at Chapter 2 Data
handling using pandas class 12 NCERT solutions.

1. What is a Series and how is it different from a 1-D array, a list, and a dictionary?
o A Series is a one-dimensional array having a sequence of values of any data
type (int, float, list, string, etc)
o By default series have numeric data labels starting from zero.
o Series vs 1-D array
 Series can have default as well as predefined index labels whereas a
numpy 1-d array has only default indexes
 Series can contain values of any datatype whereas arrays can contain
elements of the same data type
o Series vs List
 Series can have default as well as predefined index labels whereas a list
has only default indexes
o Series vs dictionary
 Series elements can be accessed using default indexes as well as its row
labels Whereas dictionary elements cannot be accessed using default
indexes. They have to be accessed using the predefined keys.
2. What is a DataFrame and how is it different from a 2-D array?
o Dataframe can store data of any type whereas a numpy 2D array can contain
data of a similar type.
o Dataframe elements can be assessed by their default indexes for row and cols
along with the defined labels.
o Numpy 2Darray elts can be assessed using default index specifications only.
o Dataframe is data structure from the pandas library having a simpler interface
for operations like file loading, plotting, selection, joining, GROUP BY, which
come very handy in data-processing applications
3. How are DataFrames related to Series?
o Dataframe and series both are data structures from the Pandas library.
o Series is a one-dimensional structure whereas Dataframe is a two-dimensional
structure.
Downloaded from www.tutorialaicsip.com
4. What do you understand by the size of (i) a Series, (ii) a DataFrame?
o Size attribute gives the number of elements present in Series or Dataframes

The next question of Chapter 2 Data handling using pandas class 12 NCERT solutions is
based on series creation.

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

EngAlph, having 26 elements with the alphabets as values and default index values.

import pandas as pd

EngAlph=pd.Series(['a','b','c','d','e','f','g'])
print(EngAlph)
0a
1b
2c
3d
4e
5f
6g
dtype: object
 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
vow=pd.Series(0,index=['a','e','i','o','u'])
vow
a0
e0
i0
o0
u0
dtype: int64
vow.empty
False
 Friends, from a dictionary having roll numbers of five of your friends as data and their
first name as keys.
d={"Samir":1,"Manisha":2,"Dhara":3,"Shreya":4,"Kusum":5}
friends=pd.Series(d) Dhara 3
friends Shreya 4
Samir 1 Kusum 5
Manisha 2 dtype: int64
Downloaded from www.tutorialaicsip.com
 MTseries, an empty Series. Check if it is an empty series.

import pandas as pd

p1=pd.Series(dtype=int)
p1
Series([], dtype: float64)
p1.empty
True

 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
m=np.array([31,28,31,30,31,30])
m
array([31, 28, 31, 30, 31, 30])
month=pd.Series(m,index=np.arange(1,7))
month
1 31
2 28
3 31
4 30
5 31
6 30
dtype: int32

The next section of Chapter 2 Data handling using pandas class 12 NCERT solutions
provides solutions based on commands.

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.

vow.loc['a':'u']=10

print(vow)
OR
vow.iloc[0:5]=10

print(vow)

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


vow/2

If you want to assign the calculated value in the series you need to initialize the
series with another object.
Downloaded from www.tutorialaicsip.com
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.

vow1=pd.Series([2,5,6,3,8],index=['a','e','i','o','u'])
vow1

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

vow3=vow+vow1
vow3

e) Subtract, Multiply and Divide Vowels by Vowels1.

vow-vow1

vow*vow1

vow/vow1

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

vow.index=['A','E','I','O','U']
vow

The next questions of Chapter 2 Data handling using pandas class 12 NCERT solutions is
based on some special commands like dimensions, size and values.

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.

vow.size
5
vow.ndim
1

vow.values
array([10, 10, 10, 10, 10], dtype=int64)

b) Rename the Series MTseries as SeriesEmpty.

MTseries=pd.Series(dtype=int)
MTseries
Downloaded from www.tutorialaicsip.com
Series([], dtype: int32)
MTseries=MTseries.rename("seriesempty")
MTseries
Series([], Name: seriesempty, dtype: int32)

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

d={"sush":1,"mannat":2,"kalash":3,"shreya":4,"kusum":5}
friends=pd.Series(d)
friends
sush 1
mannat 2
kalash 3
shreya 4

kusum 5

dtype: int64

friends.index.name="Fname"

friends.index

Index(['sush', 'mannat', 'kalash', 'shreya', 'kusum'], dtype='object', name='Fname')


d) Display the 3rd and 2nd value of the Series Friends, in that order.
friends
Fname
sush 1
mannat 2
kalash 3
shreya 4
kusum 5
dtype: int64
friends.iloc[2:0:-1]
Fname
kalash 3
mannat 2
dtype: int64

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


engalph.iloc[5:17] # assume that the series is created as per requirement
f) Display the first 10 values in the Series EngAlph.
engalph.head(10)
Downloaded from www.tutorialaicsip.com
g) Display the last 10 values in the Series EngAlph.
engalph.tail(10)
h) Display the MTseries.
print(MTSeries)
The next questions of Chapter 2 Data handling using pandas class 12 NCERT solutions is
based on series manipulation commands.
8. Using the Series created in Question 5, write commands for the following:
a) Display the names of the months 3 through 7 from the Series MonthDays.
import numpy as np
m=np.array([31,28,31,30,31,30,31,31,30,31,30,31])
month=pd.Series(m,index=np.arange(1,13))
month=pd.Series(m,index=["Jan","Feb","Mar","Apr","May","June","July","Aug","Sept",
"Oct","Nov","Dec"])
Method 1:
month.iloc[2:8]

Method 2:
month.loc['Mar':'Aug']

Method 3:
month['Mar':'Aug']

b) Display the Series MonthDays in reverse order.

Method 1:

month[::-1]

Method 2:

month.iloc[::-1]

Method 3:

month.loc["Dec":"Jan":-1]

The next section of Chapter 2 Data handling using pandas class 12 NCERT solutions
consists of questions based on data frame.

9. Create the following DataFrame Sales containing year-wise sales figures for five
salespersons in INR. Use the years as column labels, and salesperson names as
row labels.
Downloaded from www.tutorialaicsip.com
2014
2015 2016 2017
Madhu 100.5
1200 20000 50000
0
Kusum 150.8 1800 50000 60000
0
Kinshuk 200.9 2200 70000 70000
0
Ankit 30000 3000 10000 80000
0
Shruti 40000 4500 125000 90000
0

The next questions of Chapter 2 Data handling using pandas class 12 NCERT solutions is
based on dataframe commands.
10. Use the DataFrame created in Question 9 above to do the following:
a) Display the row labels of Sales.

sales.index
Index(['madhu', 'kusum', 'kinshuk', 'Ankit', 'Shruti'], dtype='object')

b) Display the column labels of Sales.

sales.columns
Int64Index([2014, 2015, 2016, 2017], dtype='int64')

c) Display the data types of each column of Sales.


sales.dtypes
2014 float64
2015 int64
2016 int64
2017 int64
dtype: object

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


Downloaded from www.tutorialaicsip.com
sales.ndim
2
sales.shape
(5, 4)
sales.size
20
sales.values
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.tail(2)
2014 2015 2016 2017
Ankit 30000.0 30000 100000 80000
Shruti 40000.0 45000 125000 90000
f) Display the first two columns of Sales.
sales[[2014,2015]]
2014 2015
madhu 100.5 12000
kusum 150.8 18000
kinshuk 200.9 22000
Ankit 30000.0 30000
Shruti 40000.0 45000
g) Create a dictionary using the following data. Use this dictionary to create a DataFrame
Sales2.
2018
Madhu 160000
Kusum 110000
Kinshu 500000
k
Ankit 340000
Shruti 900000
dict1={2018:[160000,110000,500000,340000,900000]}
sales2=pd.DataFrame(dict1,index=["madhu","kusum","kinshuk","Ankit","Shruti"])
print(sales2)
h) Check if Sales2 is empty or it contains data.

sales2.empty
False

The last questions of Chapter 2 Data handling using pandas class 12 NCERT
solutions based on dataframe operations.
Downloaded from www.tutorialaicsip.com
11. Use the DataFrame created in Question 9 above to do the following:

a) Append the DataFrame Sales2 to the DataFrame Sales.

sales=sales.append(sales2)
sales

b) Change the DataFrame Sales such that it becomes its transpose.

sales.T

c) Display the sales made by all salespersons in the year 2017.

sales[2018]=[160000,110000,500000,340000,900000]

sales

sales[2017] OR

sales.loc[:,2017]

d) Display the sales made by Madhu and Ankit in the year 2017 and 2018.

sales.loc[sales.index.isin(["madhu","Ankit"]),[2017,2018]]

e) Display the sales made by Shruti 2016.

sales.loc[sales.index=='Shruti',2016]

f) Add data to Sales for salesman Sumeet where the sales made are [196.2, 37800, 52000,
78438, 38852] in the years [2014, 2015, 2016, 2017, 2018] respectively.

sales.loc["sumeet"]=[196.2,37800,52000,78438,38852]
sales
g) Delete the data for the year 2014 from the DataFrame Sales.
Temporary deletion
sales.drop(columns=2014)
Permanent deletion
sales.drop(columns=2014,inplace=True)
sales

h) Delete the data for salesman Kinshuk from the DataFrame Sales.

sales.drop("kinshuk",axis=0)
Downloaded from www.tutorialaicsip.com
sales.drop("kinshuk")

i) Change the name of the salesperson Ankit to Vivaan and Madhu to Shailesh.

sales=sales.rename({"Ankit":"vivaan","madhu":"shailesh"},axis="index")
sales

j) Update the sale made by Shailesh in 2018 to 100000.

sales.loc[sales.index=="shailesh",2018]=100000
sales

k) Write the values of DataFrame Sales to a comma-separated file SalesFigures.csv on the


disk. Do not write the row labels and column labels.

sales.to_csv("g:\salesFigures.csv",index=False,header=False)

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.

salesretrieved=pd.read_csv("g:\salesFigures.csv",names=['2015','2016','2017','2018'])
salesretrieved

salesretrieved.index=['madhu', 'kusum', 'kinshuk', 'Ankit', 'Shruti','sumeet']


salesretrieved

That's all from Data handling using pandas class 12 NCERT solutions. I hope you
understood and satisfied with the answers provided in this article Chapter 2 Data handling
using pandas class 12 NCERT solutions. If you have any concerns regarding any question
explained in this article you can write in the comment section.

Thank you for reading this article on Data handling using pandas class 12 NCERT solutions.

Follow the below-given link to browse more contents of IP class 12 which is related to
Chapter 2 Data handling using pandas class 12 NCERT solutions.

Informatics Practices class 12

You might also like