MLL Ip Xii
MLL Ip Xii
“SESSION 2025-26
KENDRIYA VIDYALAYA SANGATHAN
Zonal Institute of Education & Training, Gwalior
OUR MENTOR:
MR. B.L. MORODIA
Deputy Commissioner & Director
KVS Zonal Institute of Education and Training, Gwalior
Course Coordinator
MRS. ANITA KANAJIA, T.A. ECONOMICS
RESOURCE PERSONS
1. MRS SANGEETA M CHAUHAN ,
PGT CS , PM SHRI K V NO3 GWALIOR
2. MR. ALOK GUPTA
PGT COMP, PM SHRI K V ETAWAH
VETTED BY:
1. MR RAJU DIXIT ,
PGT CS, PM SHRI K V ALIGARH
2. MR. RAKESH KUMAR SINGH YADAV
PGT CS, PM SHRI K V DOGRA LINES MEERUT CANTT
3. MR. SUNIL KUMAR BHELE
PGT CS, PM SHRI K V PUNJAB LINES MEERUT CANTT
4. MR. MANISH GUPTA
PGT CS, PM SHRI K V HATHRAS
5. MR PANKAJ SINGH
PGT(CS) , PM SHRI KV TALBEHAT
SERIES
Pandas provides the Series data structure, which is a one-dimensional labeled array capable of holding
data of any type (integers, strings, floats, etc.). A Series can be created from various data sources, including
ndarrays, dictionaries, and scalar values.
1. Creating Series from ndarray An ndarray (NumPy array) can be used to create a Series. If no index is
provided, the default index will be [0, 1, 2, ..., n-1].
import pandas as pd
import numpy as np Output-
arr = np.array([10, 20, 30, 40]) 0 10
s = pd.Series(arr) 1 20
print(s) 2 30
3
40dtype: int64
2. Creating Series from Dictionary
A dictionary can be directly converted into a Series where the keys become the index labels, and the
values become the data.
Arithmetic Operations in Pandas Series Students will be able to perform and understand basic arithmetic
operations (addition, subtraction, multiplication, division) on Pandas Series.
1. Series Addition (+)
Description: Adds values in two Series element-wise (position-wise). If index labels match, values are
added; if not, result is NaN.
Code Example:
Output-
import pandas as pd
0 11
s1 = pd.Series([10, 20, 30])
1 22
s2 = pd.Series([1, 2, 3])
2 33
print(s1 + s2)
dtype:
2. Series Subtraction (-) int64
Description: Subtracts the values of one Series from another element-wise.
Code Example:
print(s1 - s2)
Output-
0 9
1 18
2 27
dtype: int64
3. Series Multiplication (*)
Description: Multiplies values of two Series element-wise.
Code Example:
print(s1 * s2)
Output-
0 10
1 40
2 90
dtype: int64
Key Takeaways:
- Operations are vectorized (fast and automatic).
- Results are aligned by index, not just position.
- Missing values are shown as NaN (Not a Number).
- Use .fillna() to handle NaNs if needed.
Head() and Tail() functions in Series
head() tail()
Returns elements from beginning of Returns elements from end of series
series
head(n) Returns the first n elements of a tail(n) returns the last n elements of a Series.
Series.
if no argument is given, then both functions return 5 elements i.e. head() will give 5
elements from beginning and tail will give 5 elements from end of series
head(3) tail(3)
↓↓↓ ↑↑↑
Index: [0] [1] [2] Data: ... (omitted) | 80 | 90 | 100 |
Data: | 10 | 20 | 30 | ... (rest omitted) Index: [7] [8] [9]
Selection, Indexing and Slicing in Series
Positional Indexing (Integer-based) Label-based Indexing (Custom Index)
● Starts from 0. ● Uses custom-defined labels (if
● Syntax: seriesname[pos] provided).
● Syntax: seriesname['label']
print(s[0]) # First element → 10 s = pd.Series([10, 20, 30], index=['a', 'b', 'c'])
print(s[3]) # Fourth element → 40 print(s['b']) # Output: 20
Slicing
Positional Slicing: using loc Label-based Slicing: using iloc
s[start:stop:step] (exclusive of stop) s['start':'stop'] (inclusive of stop)
DATA FRAME DataFrame is two-dimensional size-mutable, potentially heterogeneous tabular data structure
with labeled axes (rows and columns). A Data frame is a two-dimensional data structure, i.e., data is aligned
in a tabular fashion in rows and columns. Pandas DataFrame consists of three principal components,
the data, rows, and columns.
Creation of DataFrame :
There are following ways to create a DataFrame :
Creation of an empty DataFrame:
An empty DataFrame can be created as follows:
Coding:
import pandas as df
data=df.DataFrame()
print(data)
Creation of DataFrame from List of Dictionaries:
We can create DataFrame from a list of Dictionaries, for example:
Coding:
import pandas as pd
d1={'Name':'Ramesh','Age':20,'Marks':75}
d2={'Name':'Dinesh','Age':22,'Marks':88}
d3={'Name':'Suresh','Age':18,'Marks':90}
df1=pd.DataFrame([d1,d2,d3])
print(df1)
Notes:
The dictionary keys are taken as column labels
▪ The values corresponding to each key are taken as data
▪ No of dictionaries= No of rows, As No of dictionaries=3, No of rows=3
▪ No of columns= Total Number of unique keys of all the dictionaries of the list, as all dictionaries
have same 3 keys, no of columns=3
Coding:
import pandas as pd
d1={'Name':'Ramesh','Age':20,'Marks':75, ‘Gender’:’Male’}
d2={'Name':'Dinesh','Age':22,'Marks':88}
d3={'Name':'Suresh','Age':18,'Marks':90,’Grade’:’B’}
df1=pd.DataFrame([d1,d2,d3])
print(df1)
Coding:
import pandas as DF
name=['ramya','ravi','abhinav','priya','akash']
age=[16,17,18,17,16]
gender=['f','m','m','f','m']
marks=[88,34,67,73,45]
d1={'name':name,'age':age,'gender':gender,'marks' :marks}
df1=DF.DataFrame(d1)
print(df1)
Note:
Dictionary keys become column labels by default in a Data Frame, and the lists become the rows
Creation of DataFrame from Series:
DataFrame created from One Series:
Coding:
import pandas as pd
s1=pd.Series([300,400,500,600])
df1=pd.DataFrame(s1)
print(df1)
Note:
DataFrame from One Series: No of rows = No of elements in Series=4 (As s1 has 4 elements) No of
columns = one (As single series used)
Creation of DataFrame from Dictionary of Series:
Coding:
import pandas as pd
name=pd.Series(['ramya','ravi','abhinav','priya','akash'])
age=pd.Series([16,17,18,17,16])
gender=pd.Series(['f','m','m','f','m'])
marks=pd.Series([88,34,67,73,45]) d1={'name':name,'age':age,'gender':gender,'marks':marks}
df1=pd.DataFrame(d1)
print(df1)
DataFrame Display
- Understand how to create a DataFrame using pd.DataFrame().
- Use print(df) or simply df in interactive environments to display the DataFrame.
- Know the functions:
● • df.head() – display the first few rows.
● • df.tail() – display the last few rows.
● • df.info() – summary of columns and data types.
● • df.describe() – basic statistics for numerical columns.
DataFrame Iteration
Learn how to loop through DataFrame rows:
• for index, row in df.iterrows(): – iterate row-wise.
• for row in df.itertuples(): – more memory-efficient row-wise iteration.
- Understand the use case: read row values, apply conditions, etc.
Label-based Selects a column using its df.loc[:, 'Name'] How can you select Use .loc[:,
Column Selection label. selects all rows for a specific column? 'Column_Name'].
the column Name.
Boolean Indexing Filters rows based on df[df['Salary'] > Define Boolean Filtering rows
in DataFrame conditions applied to columns. 50000] selects rows indexing in based on
where salary is DataFrame. conditions.
greater than 50000.
Boolean Indexing Filters data using expressions. df[df['Age'] > 30] What does Filters rows
Example filters rows where df[df['Age'] > 30] where the age is
the age is above 30. do? greater than 30.
Slicing Rows in Extracts specific rows using df[2:5] selects rows Write the syntax for df[start:end].
DataFrame row index ranges. with indices 2, 3, slicing rows in
and 4. DataFrame.
Slicing Columns Extracts specific columns df.iloc[:, 0:2] selects How can slicing By selecting only
in DataFrame using .iloc[] or .loc[]. the first two simplify data relevant rows or
columns. analysis? columns.
Combined Row Simultaneous slicing of rows df.loc['A':'C', What does Selects rows A to
and Column and columns. 'Age':'Salary'] df.loc['A':'C', C and columns
Slicing selects rows A to C 'Age':'Salary'] do? Age to Salary.
and columns Age to
Salary.
DATA VISUALIZATION
Purpose of Plotting
Data visualization helps in understanding trends and patterns by representing data graphically. A line plot is
used to display changes in data over time or continuous variables.
Line Plot
# Data points
x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]
Bar Plot
Histogram
savefig() Save the plot as image/pdf file type # Adding titles and labels
plt.title('Sample Histogram')
title() Display the title of the plot/graph plt.xlabel('Value')
plt.ylabel('Frequency')
xlabel() Sets the label of x-axis
# Display the plot
ylabel() Sets the label of y-axis plt.show()
xticks() Sets the tick location and label on x-axis
>>>import pandas as pd
>>>df = pd.read_csv(‘d:\\test.csv’) #read the csv file
>>>df
Output:
0 Snumber B P
1 1 Honda Civic 22000
2 2 ToyotaCorolla 25000
3 3 FordFocus 27000
4 4 AudiA4 35000
CASE 2. If separator character is different from comma in csv file
Syntax:
<df>=pandas.read_csv(<filepath>,sep=<separator character>)
Eg.
Let us assume csv file named test3
>>>import pandas
>>>df=pandas.read_csv(‘d:\\test3.csv’,sep=’;’)
>>>df
0 1 Honda Civic 22000
1 2 ToyotaCorolla 25000
2 3 FordFocus 27000
3 4 AudiA4 35000
>>>import pandas
>>>df=pandas.read_csv(‘d:\\test.csv’,header=None,skiprows=1)
>>>df
0 1 2
0 1 Honda Civic 22000
1 2 ToyotaCorolla 25000
2 3 FordFocus 27000
3 4 AudiA4 35000
>>>import pandas
>>>df=pandas.read_csv(‘d:\\test1.csv’,names=[‘Sno’,’Brand’,’Price’])
>>>df
Sno Brand Price
0 1 Honda Civic 22000
1 2 ToyotaCorolla 25000
2 3 FordFocus 27000
3 4 AudiA4 35000
CASE 2. If we want to give default headings like 0,1,2,3…..
<df>=pandas.read_csv(<filepath>,header=None)
>>>import pandas
>>>df=pandas.read_csv(‘d:\\test1.csv’,header=None)
>>>df
0 1 2
0 1 Honda Civic 22000
1 2 ToyotaCorolla 25000
2 3 FordFocus 27000
3 4 AudiA4 35000
2.1 Exporting data from dataframe to csv file /Creating csv file from dataframe’s data
Using the to_csv() function from the pandas package, you can export data from dataframe to CSV file.
CASE 1. If we want to use comma as a separator character in csv file
Syntax:
<DF>.to_csv(<filepath>)
Eg. Write python code to store data present in a dataframe to a csv file “export1_dataframe.csv” in D:\
>>>import pandas as pd
>>>cars = {'Brand': ['Honda Civic','ToyotaCorolla','FordFocus','AudiA4'],'Price':
[22000,25000,27000,35000]}
>>>df= pd.DataFrame(cars, columns= [‘Sno’,'Brand', 'Price'])
>>>df.to_csv(‘d:\\export1_dataframe.csv’) #write the csv file
Output:
Now open csv file named export1_dataframe in D:/ Drive
CASE 2. If we want to use other separator from comma to separate data in csv file
Syntax:
<DF>.to_csv(<filepath>,sep=<separator_character>)
Eg. Write python code to store data present in a dataframe to a csv file “export1_dataframe.csv” in D:\
>>>import pandas as pd
>>>cars = {'Brand': ['Honda Civic','ToyotaCorolla','FordFocus','AudiA4'],'Price':
[22000,25000,27000,35000]}
>>>df= pd.DataFrame(cars, columns= [‘Sno’,'Brand', 'Price'])
>>>df.to_csv(‘d:\\export1_dataframe.csv’,sep=’#’) #write csv file
Output:
Now open csv file named export1_dataframe in D:/ Drive
NETWORKING