Artificial Intelligence

Recap of Pandas Library (CBSE Class 12 Artificial Intelligence)

Class 12 · Artificial Intelligence

Recap of Pandas Library

Artificial Intelligence systems generate and process enormous amounts of structured and unstructured data. Before this data can be used to train Machine Learning models, it must be organized, cleaned, analyzed, and transformed into a meaningful format. The Pandas library provides powerful tools to perform these tasks efficiently.

In Class XI, you learned the basics of the Pandas library. In this chapter, you will revise its important concepts, understand its data structures, and learn how DataFrames simplify data analysis for Artificial Intelligence applications.


Learning Objectives

  • Understand the Pandas library.
  • Learn the importance of Pandas in Artificial Intelligence.
  • Understand Series and DataFrame.
  • Create DataFrames using different methods.
  • Add, modify and delete rows and columns.
  • Understand important DataFrame attributes.

What is Pandas?

Pandas is an open-source Python library used for data manipulation, organization, cleaning, analysis, and visualization. It provides flexible and efficient data structures that allow programmers to work with tabular data similar to Excel spreadsheets or database tables.

Definition

Pandas is a Python library that provides powerful data structures and functions for storing, organizing, manipulating, cleaning, and analyzing structured data.


Why is Pandas Used in Artificial Intelligence?

Artificial Intelligence models require clean and organized datasets for training. Pandas makes it easy to read datasets, handle missing values, filter records, perform calculations, and prepare data before applying Machine Learning algorithms.


Real-Life Example

Suppose a company launches multiple marketing campaigns. The company collects information such as campaign name, budget, duration, customer engagement, sales, and profit. Using Pandas, data analysts can quickly calculate average sales, compare campaign performance, group campaigns by category, and generate useful business insights.


Applications of Pandas

  • Data cleaning
  • Data preprocessing
  • Business analytics
  • Financial analysis
  • Medical data analysis
  • Student performance analysis
  • Customer behaviour analysis
  • Machine Learning data preparation

Importing the Pandas Library



import pandas as pd

The alias pd is commonly used while importing the Pandas library.


Pandas Data Structures

Pandas mainly provides two data structures for organizing data.

Data Structure Description
Series One-dimensional labeled array.
DataFrame Two-dimensional tabular data structure consisting of rows and columns.

1. Series

A Series is a one-dimensional labeled array capable of storing different types of data.

Example



import pandas as pd

marks = pd.Series([85,90,95,88])

print(marks)

Output



0    85

1    90

2    95

3    88

dtype: int64


2. DataFrame

A DataFrame is the most important data structure in Pandas. It stores data in rows and columns similar to an Excel worksheet or database table.


Creating a DataFrame from a Dictionary



import pandas as pd

data = {

'Name':['Aman','Riya','Rahul'],

'Marks':[88,91,85]

}

df = pd.DataFrame(data)

print(df)

Output



    Name   Marks

0   Aman      88

1   Riya      91

2  Rahul      85


Creating a DataFrame from NumPy Arrays



import numpy as np

import pandas as pd

array1 = np.array([90,100,110])

array2 = np.array([60,70,80])

df = pd.DataFrame([array1,array2],

columns=['A','B','C'])

print(df)


Creating a DataFrame from a List of Dictionaries



import pandas as pd

data = [

{'Name':'Aman','Marks':85},

{'Name':'Riya','Marks':92},

{'Name':'Rahul','Marks':88}

]

df = pd.DataFrame(data)

print(df)


Adding a New Column



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

print(df)


Adding a New Row



df.loc[3] = ['Neha',95,'A+']

print(df)


Updating a Row



df.loc[1] = ['Riya',93,'A+']


Deleting Rows and Columns

Delete a Row



df = df.drop(1,axis=0)

Delete a Column



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


Important DataFrame Attributes

Attribute Description
index Returns row labels.
columns Returns column names.
shape Returns number of rows and columns.
head(n) Displays first n rows.
tail(n) Displays last n rows.

Example



print(df.shape)

print(df.columns)

print(df.head())

print(df.tail())


Output



(4,3)

Index(['Name','Marks','Grade'])

First five rows...

Last five rows...


Flow of Data Processing using Pandas



Collect Dataset

      │

      ▼

Load into DataFrame

      │

      ▼

Clean Data

      │

      ▼

Filter & Modify

      │

      ▼

Analyze Data

      │

      ▼

Machine Learning


Difference Between Series and DataFrame

Series DataFrame
One-dimensional Two-dimensional
Single column Multiple columns
Stores one type of information Stores multiple related columns
Comparable to a single Excel column Comparable to an Excel worksheet

Pandas vs NumPy

NumPy Pandas
Works mainly with arrays. Works mainly with tables.
Numerical computing. Data analysis.
Homogeneous data. Heterogeneous data.
High-speed mathematical operations. Data cleaning and manipulation.

Industry Applications

  • Hospital patient record analysis.
  • Online shopping recommendation systems.
  • Student result analysis.
  • Bank transaction analysis.
  • Customer segmentation.
  • Sales performance reporting.
  • Weather forecasting.

Think Like an AI Engineer

A school stores the attendance records of 8,000 students in an Excel file. Before predicting students who are at risk of low attendance, which Python library should be used to organize and analyze this data?

Click to View Answer

Pandas, because it is designed to read, organize, clean, filter, and analyze tabular datasets efficiently.


Competency-Based Question

A hospital has collected patient records containing age, blood pressure, sugar level, and diagnosis. Explain why a DataFrame is more suitable than a Series for storing this information.


Common Beginner Mistakes

  • Forgetting to import Pandas.
  • Confusing Series with DataFrame.
  • Using incorrect axis values while deleting rows or columns.
  • Assuming head() displays the entire dataset.
  • Ignoring row labels (index).

Quick Revision

  • Pandas is used for data analysis and manipulation.
  • Main data structures are Series and DataFrame.
  • DataFrame stores data in rows and columns.
  • Pandas is widely used for preparing AI datasets.
  • DataFrames support adding, updating, deleting, filtering, and analyzing data.

Memory Trick

PANdas → PANel of Data (Table)

Exam Tips

  • Remember the two primary data structures: Series and DataFrame.
  • Know how to create DataFrames using different methods.
  • Practice adding and deleting rows and columns.
  • Memorize important DataFrame attributes such as shape, columns, index, head(), and tail().

Summary

  • Pandas is a powerful library for organizing, cleaning, and analyzing structured data.
  • Series and DataFrame are the two primary data structures.
  • DataFrames are extensively used in Artificial Intelligence and Machine Learning.
  • Pandas simplifies data preprocessing before training AI models.
  • It provides powerful tools to manipulate large datasets efficiently.