Chapter 2 Data handling using Pandas - I
Chapter 2 Data handling using Pandas - I
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!
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.
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)
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
vow3=vow+vow1
vow3
vow-vow1
vow*vow1
vow/vow1
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)
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
Method 2:
month.loc['Mar':'Aug']
Method 3:
month['Mar':'Aug']
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')
sales.columns
Int64Index([2014, 2015, 2016, 2017], dtype='int64')
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:
sales=sales.append(sales2)
sales
sales.T
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]]
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
sales.loc[sales.index=="shailesh",2018]=100000
sales
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
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.