PDF Complete Data handling using pandas class 12 NCERT solutions

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?
    • A Series is a one-dimensional array having a sequence of values of any data type (int, float, list, string, etc)
    • By default series have numeric data labels starting from zero.
    • 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
    • Series vs List
      • Series can have default as well as predefined index labels whereas a list has only default indexes
    • 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?
    • Dataframe can store data of any type whereas a numpy 2D array can contain data of a similar type.
    • Dataframe elements can be assessed by their default indexes for row and cols along with the defined labels.
    • Numpy 2Darray elts can be assessed using default index specifications only.
    • 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?
    • Dataframe and series both are data structures from the Pandas library.
    • Series is a one-dimensional structure whereas Dataframe is a two-dimensional structure.
  4. What do you understand by the size of (i) a Series, (ii) a DataFrame?
    • 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','h','i','j','k','l','m',
                   'n','o','p','q','r','s','t','u','v','w','x','y','z'])
print(EngAlph)
  • 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'])
print(vow)
  • 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)
print(friends)
  • MTseries, an empty Series. Check if it is an empty series.
import pandas as pd

#Method 1
MTseries=pd.Series(dtype=int)
print(MTseries)

#Method 2
MTseries.empty
print(MTseries)
  • 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

MonthDays=np.array([31,28,31,30,31,30,31,31,30,31,30,31])
month=pd.Series(MonthDays,index=np.arange(1,13))
print(month)

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.

import pandas as pd
vow=pd.Series(0,index=['a','e','i','o','u'])

#Method 1
vow.loc['a':'u']=10
print(vow)

#Method 2
vow.iloc[0:5]=10
print(vow)

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

import pandas as pd
vow=pd.Series(0,index=['a','e','i','o','u'])
vow.iloc[0:5]=10
print(vow/2)

If you want to assign the calculated value in the series you need to initialize the series with another object.

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
vow=pd.Series(0,index=['a','e','i','o','u'])
vow1=pd.Series([2,5,6,3,8],index=['a','e','i','o','u'])
print(vow1)

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

import pandas as pd
vow=pd.Series(0,index=['a','e','i','o','u'])
vow1=pd.Series([2,5,6,3,8],index=['a','e','i','o','u'])
vow3=vow+vow1
print(vow3)

e) Subtract, Multiply and Divide Vowels by Vowels1.

import pandas as pd
vow=pd.Series(0,index=['a','e','i','o','u'])
vow1=pd.Series([2,5,6,3,8],index=['a','e','i','o','u'])
print(vow-vow1)
print(vow*vow1)
print(vow/vow1)

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

import pandas as pd
vow=pd.Series(0,index=['a','e','i','o','u'])
vow.index=['A','E','I','O','U']
print(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.

import numpy as np
import pandas as pd

#EngAlph
EngAlph=pd.Series(['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'])
print("Size:",EngAlph.size)
print("Dimension:",EngAlph.ndim)
print("Values:",EngAlph.values)

#vowels
vow=pd.Series(0,index=['a','e','i','o','u'])
print("Size:",vow.size)
print("Dimension:",vow.ndim)
print("Values:",vow.values)

#Friends
d={"Samir":1,"Manisha":2,"Dhara":3,"Shreya":4,"Kusum":5}
friends=pd.Series(d)
print("Size:",friends.size)
print("Dimension:",friends.ndim)
print("Values:",friends.values)

#Method 1
MTseries=pd.Series(dtype=int)

#Method 2
MTseries.empty

print("Size:",MTseries.size)
print("Dimension:",MTseries.ndim)
print("Values:",MTseries.values)



#MonthDays
MonthDays=np.array([31,28,31,30,31,30,31,31,30,31,30,31])
month=pd.Series(MonthDays,index=np.arange(1,13))
print("Size:",month.size)
print("Dimension:",month.ndim)
print("Values:",month.values)



b) Rename the Series MTseries as SeriesEmpty.

import pandas as pd
MTseries=pd.Series(dtype=int)
print(MTseries)
MTseries=MTseries.rename("SeriesEmpty")
print(MTseries)

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

import numpy as np
import pandas as pd

#Friends
d={"Samir":1,"Manisha":2,"Dhara":3,"Shreya":4,"Kusum":5}
friends=pd.Series(d)
friends.index.name="Fname"
print(friends.index)

#MonthDays
MonthDays=np.array([31,28,31,30,31,30,31,31,30,31,30,31])
month=pd.Series(MonthDays,index=np.arange(1,13))
month.index.name="MonthNo"
print(month.index)

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

import numpy as np
import pandas as pd

#Friends
d={"Samir":1,"Manisha":2,"Dhara":3,"Shreya":4,"Kusum":5}
friends=pd.Series(d)
friends.index.name="Fname"
print(friends.iloc[2:0:-1])

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

import numpy as np
import pandas as pd

#EngAlph
EngAlph=pd.Series(['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'])
print(EngAlph.iloc[4:16])

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

import numpy as np
import pandas as pd

#EngAlph
EngAlph=pd.Series(['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'])
print(EngAlph.head(10))

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

import numpy as np
import pandas as pd

#EngAlph
EngAlph=pd.Series(['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'])
print(EngAlph.tail(10))

h) Display the MTseries.

Refer answer b). 

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
import pandas as pd

#MonthDays
MonthDays=np.array([31,28,31,30,31,30,31,31,30,31,30,31])
month=pd.Series(MonthDays,index=np.arange(1,13))
month=pd.Series(MonthDays,index=["Jan","Feb","Mar","Apr","May","June",
                         "July","Aug","Sept","Oct","Nov","Dec"])
#Method 1
print(month.iloc[2:7])

#Method 2
print(month.loc['Mar':'July'])

#Method 3
print(month['Mar':'July'])

b) Display the Series MonthDays in reverse order.

import numpy as np
import pandas as pd

#MonthDays
MonthDays=np.array([31,28,31,30,31,30,31,31,30,31,30,31])
month=pd.Series(MonthDays,index=np.arange(1,13))
month=pd.Series(MonthDays,index=["Jan","Feb","Mar","Apr","May","June",
                         "July","Aug","Sept","Oct","Nov","Dec"])
#Method 1
print(month[::-1])

#Method 2
print(month.iloc[::-1])

#Method 3
print(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.

2014201520162017
Madhu100.5120002000050000
Kusum150.8180005000060000
Kinshuk200.9220007000070000
Ankit30000300001000080000
Shruti400004500012500090000
import pandas as pd
d = {2014:[100.5,150.8,200.9,30000,4000],
     2015:[12000,18000,22000,30000,45000],
     2016:[20000,50000,70000,10000,125000],
     2017:[50000,60000,70000,80000,90000]}
Sales=pd.DataFrame(d,index=['Madhu',"Kusum","Kinshuk","Ankit","Shruti"])
print(Sales)

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.

import pandas as pd
d = {2014:[100.5,150.8,200.9,30000,4000],
     2015:[12000,18000,22000,30000,45000],
     2016:[20000,50000,70000,10000,125000],
     2017:[50000,60000,70000,80000,90000]}
Sales=pd.DataFrame(d,index=['Madhu',"Kusum","Kinshuk","Ankit","Shruti"])
print(Sales.index)

Consider the dataframe creation command as above in the following answers, I have written only relevant statement for the answers.

b) Display the column labels of Sales.

sales.columns

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

Sales.dtypes

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

print("Dimensions:",Sales.ndim)
print("Shape:",Sales.shape)
print("Size:",Sales.size)
print("Values:",Sales.values)

e) Display the last two rows of Sales.

#Method 1
print(Sales.tail(2))

#Method 2
print(Sales.iloc[-2:])

#With Specific Columns, I have prnted two columns
print(Sales.iloc[-2:,-2:])

f) Display the first two columns of Sales.

#Method 1
print(Sales[[2014,2015]])

#Method 2
print(Sales[Sales.columns[0:2]])

#Method 3
print(Sales.iloc[:, 0:2] )

g) Create a dictionary using the following data. Use this dictionary to create a DataFrame Sales2.

2018
Madhu160000
Kusum110000
Kinshuk500000
Ankit340000
Shruti900000
import pandas as pd
dict1={2018:[160000,110000,500000,340000,900000]}
sales2=pd.DataFrame(dict1,index=["Madhu","Kusum","Kinshuk","Ankit","Shruti"])
print(sales2)

Note: For the below-given answers I have skipped dataframe creation statement and directly used the relevant statements.

h) Check if Sales2 is empty or it contains data.

print(sales2.empty)

The last questions of Chapter 2 Data handling using pandas class 12 NCERT solutions based on dataframe operations.

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

a) Append the DataFrame Sales2 to the DataFrame Sales.

import pandas as pd

#Sales1
d = {2014:[100.5,150.8,200.9,30000,4000],
     2015:[12000,18000,22000,30000,45000],
     2016:[20000,50000,70000,10000,125000],
     2017:[50000,60000,70000,80000,90000]}
Sales1=pd.DataFrame(d,index=['Madhu',"Kusum","Kinshuk","Ankit","Shruti"])

#Sales 2
dict1={2018:[160000,110000,500000,340000,900000]}
Sales2=pd.DataFrame(dict1,index=["Madhu","Kusum","Kinshuk","Ankit","Shruti"])

#Appending Dataframes
Sales1=Sales1.append(Sales2)
print(Sales1)

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

import pandas as pd


d = {2014:[100.5,150.8,200.9,30000,4000],
     2015:[12000,18000,22000,30000,45000],
     2016:[20000,50000,70000,10000,125000],
     2017:[50000,60000,70000,80000,90000]}
Sales=pd.DataFrame(d,index=['Madhu',"Kusum","Kinshuk","Ankit","Shruti"])

print(Sales.T)

Note: Consider the dataframe creation statement as above for the next answers. I have only written statements which are relevant for a specific solution.

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

#Method 1
print(Sales[2017])

#Method 2
print(Sales.loc[:,2017])

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

import pandas as pd

d = {2014:[100.5,150.8,200.9,30000,4000],
     2015:[12000,18000,22000,30000,45000],
     2016:[20000,50000,70000,10000,125000],
     2017:[50000,60000,70000,80000,90000]}

Sales=pd.DataFrame(d,index=['Madhu',"Kusum","Kinshuk","Ankit","Shruti"])

#Add 2018 Data
Sales[2018]=[160000,110000,500000,340000,900000]

#Method 1
print(Sales.loc[['Madhu','Ankit'], [2017,2018]])

#Method 2
print(Sales.loc[Sales.index.isin(["Madhu","Ankit"]),[2017,2018]])


e) Display the sales made by Shruti 2016.

print(Sales.loc[Sales.index=='Shruti',2016])

Note: I have used only index and relational operator, check the answer of d and you can write another method as well.


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]
print(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)
print(Sales)

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

Sales.drop(“kinshuk”,axis=0)

Sales.drop(“kinshuk”)

Note: Use the print() function to check the effect of drop.

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

Sales=sales.rename({“Ankit”:”Vivaan”,”Madhu”:”Shailesh”},axis=”index”)
print(Sales)

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

Sales.loc[Sales.index==”Shailesh”,2018]=100000
print(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(“d:\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(“d:\salesFigures.csv”,names=[‘2015′,’2016′,’2017′,’2018’])
print(salesretrieved)

salesretrieved.index=[‘Madhu’, ‘Kusum’, ‘Kinshuk’, ‘Ankit’, ‘Shruti’,’Sumeet’]
print(salesretrieved)

That’s all from Data handling using pandas class 12 NCERT solutions. I hope you understood and were 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.

Watch this videos for more understanding:

Part I


Part II

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

Follow the below given link for pdf:

7 thoughts on “PDF Complete Data handling using pandas class 12 NCERT solutions”
  1. Thank you very much. You are the reason I’ll be able to score well on my exams. I really appreciate your hard work.

  2. Utkarsh Pandey says:

    Thank you so much it helps a lot. I would be grateful if you will upload last 10-15 yrs pyq from all chapters of ip
    😊

Leave a Reply