Skip to content

Class 12th Python Pandas Notes

Class 12th Python Pandas Notes

NOTES

Unit 1 – Introduction to Pandas (Part-I)

Unit 1 – Introduction to Pandas (Part-I)

1. Introduction to Python Libraries

What is a Python Library?

A Python library is a collection of pre-written code that provides functions and tools to perform specific tasks. Libraries help developers complete tasks faster because they do not need to write every piece of code from the beginning.

For example, instead of writing complex code for data analysis or visualization, Python offers specialized libraries that already include built-in functions for these operations.

In the fields of data science and data analysis, Python libraries are widely used because they help users:

  • Organize and manage large datasets
  • Perform calculations and manipulate data efficiently
  • Visualize information using graphs and charts

Two important libraries used in Class 12 Informatics Practices (IP) are:

  • Pandas – used for data analysis and data handling
  • Matplotlib – used for creating graphs and visualizations

2. Pandas Library

What is Pandas?

Pandas is an open-source Python library designed for data manipulation and analysis. It provides powerful tools to store, process, and analyze structured data such as tables, spreadsheets, or databases.

The name Pandas comes from the term “Panel Data”, which refers to multidimensional structured datasets.

Pandas is widely used because it allows users to:

  • Handle large datasets efficiently
  • Clean and organize messy data
  • Perform mathematical operations on data
  • Import and export datasets easily

Importing Pandas

Before using Pandas, it must be imported into the Python program.

import pandas as pd

The alias pd is commonly used to simplify code writing.

Class 12 IP - Unit 1 Pandas Notes

3. Matplotlib Library

What is Matplotlib?

Matplotlib is a popular Python library used for creating graphical representations of data. It helps in visualizing information using charts and graphs, which makes it easier to understand patterns, relationships and trends within the data.

Using Matplotlib, different types of graphs can be created such as:

  • Line Charts
  • Bar Graphs
  • Pie Charts
  • Histograms
  • Scatter Plots

Importing Matplotlib

import matplotlib.pyplot as plt

The module pyplot contains functions used for creating charts and visualizing datasets.

4. Data Structures in Pandas

Pandas provides powerful data structures that help organize and manipulate structured data efficiently.

The two primary data structures are:

  • Series – A one-dimensional labeled array.
  • DataFrame – A two-dimensional table consisting of rows and columns.

These structures are designed to handle large datasets and perform various operations such as filtering, sorting and statistical analysis.

5. Series in Pandas

A Series is a one-dimensional data structure capable of storing data values along with their corresponding labels called indexes. Each element of a Series consists of an index and its associated value.

Index Value
0 10
1 20
2 30

Series can store different types of data including integers, strings and floating point numbers.

6. Creating a Series

Series from ndarray

import pandas as pd
import numpy as np

data = np.array([10,20,30,40])
s = pd.Series(data)

print(s)

Series from Dictionary

import pandas as pd

data = {'Math':90,'Science':85,'English':88}

s = pd.Series(data)

print(s)

Series from Scalar Value

import pandas as pd

s = pd.Series(5,index=['A','B','C','D'])

print(s)

7. Mathematical Operations on Series

Pandas allows mathematical operations to be applied directly to all elements of a Series.

import pandas as pd

s = pd.Series([10,20,30,40])

print(s + 5)
print(s * 2)

Other operations such as subtraction and division can also be performed.

8. Head() Function

The head() function is used to display the first few rows of a dataset. By default, it shows the first five records.

s.head()

You can also specify the number of rows.

s.head(3)

9. Tail() Function

The tail() function displays the last few rows of a dataset. By default it shows the last five records.

s.tail()

10. Selection, Indexing and Slicing

Indexing

Indexing allows accessing individual elements in a Series.

s = pd.Series([10,20,30,40])

print(s[1])

Slicing

Slicing retrieves a range of values.

print(s[1:3])

11. DataFrame in Pandas

A DataFrame is a two-dimensional data structure used to store data in tabular format with rows and columns similar to a spreadsheet.

Name Marks Age
Rahul 85 17
Priya 92 18
Aman 78 17

12. Creating DataFrames

From Dictionary

import pandas as pd

data = {
'Name':['Rahul','Priya','Aman'],
'Marks':[85,92,78]
}

df = pd.DataFrame(data)

print(df)

From List of Dictionaries

data = [
{'Name':'Rahul','Marks':85},
{'Name':'Priya','Marks':92},
{'Name':'Aman','Marks':78}
]

df = pd.DataFrame(data)

From CSV File

df = pd.read_csv("students.csv")

13. Displaying DataFrame

print(df)

This displays all rows and columns of the DataFrame.

14. Iteration in DataFrame

Iteration allows accessing rows one by one.

for index,row in df.iterrows():
    print(index,row)

15. Operations on Rows and Columns

Adding a Column

df['Grade'] = ['A','A','B']

Selecting Columns

df['Marks']

Deleting Columns

df.drop('Grade',axis=1,inplace=True)

Renaming Columns

df.rename(columns={'Marks':'Score'},inplace=True)

16. Head() and Tail() in DataFrame

df.head()

Displays the first five rows.

df.tail()

Displays the last five rows.

17. Indexing Using Labels

Label based indexing can be done using the loc[] method.

df.loc[0]
df.loc[0,'Name']

18. Boolean Indexing

Boolean indexing is used to filter data based on conditions.

df[df['Marks'] > 80]

19. Importing Data between CSV Files and DataFrames

Importing Data

df = pd.read_csv("data.csv")

This command reads the CSV file and converts it into a DataFrame.

Exporting Data

df.to_csv("output.csv")

This saves the DataFrame data into a CSV file.