0% found this document useful (0 votes)
8 views10 pages

Python Programming and Data Analysis Guide

This document provides an overview of Python, highlighting its features, libraries like Pandas and Matplotlib, and their applications in data manipulation and visualization. It includes examples of creating DataFrames, using functions like head() and tail(), and various plotting techniques. Additionally, it explains the significance of data visualization and the types of graphs supported by Matplotlib.

Uploaded by

Saran Nagiya
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views10 pages

Python Programming and Data Analysis Guide

This document provides an overview of Python, highlighting its features, libraries like Pandas and Matplotlib, and their applications in data manipulation and visualization. It includes examples of creating DataFrames, using functions like head() and tail(), and various plotting techniques. Additionally, it explains the significance of data visualization and the types of graphs supported by Matplotlib.

Uploaded by

Saran Nagiya
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

PYTHON NOTES

Class XII – Computer Science

PYTHON
• Python is a High level programming language for software developer as well as general
programmer, beginners and educationist
• Python was developed by Guido van Rossom
• The first version of python was 1.0.0 released in the year 1991 and now we are using the latest
version of python that is 3.12.2 ( and even better)
• extension –.py

features of python
1. User friendly
2. It is interpreted language that excess code line by line
3. Platform independent language
4. Wide range library
5. Two ways to write code in python->
1) Script
2) interactive
6. Expressive language or short code
7. Free and open source
PYTHON LIBRARIES
PANDAS
• PANAS is one of the pythons most famous and powerful library for data and data science
management, as it makes inputting, analysis and manipulating data much easier than other.
• PANDAS build on packaging like NumPy and MATPLOTLIVE to give us a single convenient
place for data analysis and operations

FEATURES OF PYTHON PANDAS


• Data frame objects help a last in keeping a frame of our data
• Tools for loading data into memory data objects from different file like b shaping and PY
boarding of data
• Good input/output capabilities
MATPLOTLIB
Mathematical plotting library
Mathematical plotting library for creating static, animated, interactive visualization of data in
python
1. Develop publication quality plots with just a few lines of code.
2. Use interactive figures that can be loom updated or modified.
3. We can (cost) customize and take full contract of line style, font properties and access properties

CSV Files

1
Comma separate valves
A CSV file in python is generally a plain text file format used to store tabular data
Each line in csv file represents a row are separated by a comma or with the help of delimiter
This format is widely used for exchanging data between various applications such as
Spread sheet, database, and some other programming language.

For example:
1. Generate a database from CSV files:
Import pandas as pd
#reads data from files “[Link]”
Data =[Link] CSV(“[Link]”)
[Link](1)
#read only 1 line data from top of the items.

2. Column addition in database from CSV files:


Import pandas as pd
df =pd. Data frame ({“a” :[1,2,3]. “b”:[4,5,6]})
c =[7,8,9]
df[c]=C
print=(df)
#adding new column with name c add valves 7,8,9

Note: df [C]=c

Data Frame structure


• Data frame is like a two-dimensional array and structure with heterogenous data (different
types of data)
• In the Data frame the data is stored in the form of table where we can add the number of
row and columns in the two-dimensional array (rows and columns in 1d individually)
• In the data frame we can change, update or manipulate the data as per requirement
or example:-

➢ The following table/Data frame is a collection of heterogenous data frame and each
element of the table (rows and columns) are in the form array in 2d.

No Student name Father name Address class


1. Anurag Kumar S.P Kumar TVS 12th
2. Om Lavania R.K Lavania Mathura 12th
3. Pratham Gupta P.K Gupta Delhi 12th
4. Shubham Singh R.S Sing Agra 12th
5. Anand Sharma R.D Sharma Banaras 12th
6. Saran Nagiya N Nagiya Dayal bagh 12th

2
➢ Features of Data frame
I. In data frame there will be heterogenous data
II. The valve of is mutable
III. Size of data frame is also mutable
IV. In the data frame, columns. May be of different types
V. Labels in axis in data frame (like x for rows y for columns)
VI. Arithmetic operation can be possible in rows and columns

➢ Format of data frame


The Data frame can be created with the help of following formats
i. List
ii. Dict.(Distionary)
iii. Series
iv. NumPy

For example:
Eg 1-> create an empty Data frame(2D)

Import pandas as pd
df= [Link]()
Print(df)

Eg 2-> Create a Database from lish

Import pandas as pd
Data =[1,2,3,4]
df=[Link](data)
Print(df)

Eg 3-> Create a data frame from Dict.

Import pandas as pd
dict = {“one”: [Link]([1,2,3,4], index=
[“a”,”b”,”c”,]),”two”:[Link]([1,2,3,4],
Index = [“a”,”b”,”c”,”d”])}
df= [Link](dict)
print(df)

Head and tail function

Head Function

3
The head() is primarily used to view the first few rows of a dataset. It helps users to quickly
fetch and overview the data and its structure.

The head() function in Python displays the top selected rows of the DataFrame. It takes a
single parameter, the number of rows.

We can use the parameter to display the number of rows of our choice from the top of the
DataFrame. By default, head() can display only 5 rows of the DataFrame.
Syntax
[Link](n)
N≤5
Example
import pandas as pd

data = {
'Name': ['Aman', 'Suman', 'Om', 'Jit'],
'Age': [18, 21, 14, 15],
'City': ['Agra', 'Mathura', 'Aligarh', 'Agra']
}

df = [Link](data)
print([Link](3))
Tail Function
The tail() provides a rapid view of the final few rows of the dataset. Just like the head()
function, tail() is especially helpful when working with huge datasets as it enables users to
check or spot any trends at the dataset end.

OR
The tail() in Python displays the last few rows of the DataFrame. It takes a single parameter,
the number of rows.
It also supports N number of values. By default, it takes up to 5 last rows of the dataset.
Syntax
[Link](N)
For Example
import pandas as pd

data = {
'Name': ['Mayank', 'Saumya', 'Ananya', 'Om', 'Janni'],
'Age': [18, 15, 14, 12, 10],
'City': ['Agra', 'Mathura', 'Chennai', 'Agra', 'Vrindavan']
}

4
df = [Link](data)
print([Link](2))

Pandas Series and DataFrame Programming

SERIES PROGRAM

Syntax:
[Link](data, index, dtype, copy)

A Series can be created using:


(i) ndarray
(ii) Dictionary
(iii) Constants

Example 1: Create an Empty Series


import pandas as pd
s = [Link]()
print(s)

Example 2: Create a Series from Scalar


import pandas as pd
import numpy as np
s = [Link](4, index=[0,1,2,3])
print(s)

Output:
0 4
1 4
2 4
3 4
dtype: int64

Example 3: Create Series without Index


import pandas as pd
import numpy as np
data = [Link](['a','b','c','d'])
s = [Link](data)
print(s)
dtype: object

Example 4: Create Series with Index


import pandas as pd
import numpy as np
data = [Link](['a','b','c','d'])
s = [Link](data, index=[10,11,12,13])

5
print(s)

Output:
10 a
11 b
12 c
13 d
dtype: object

Mathematical Operation with Series


import pandas as pd
import numpy as np
s = [Link]([1,2,3,4])
u=s+s
print(u)
u=s*s
print(u)

Retrieve Data Using Label (Index)


import pandas as pd
s = [Link]([1,2,3,4,5], index=['a','b','c','d','e'])
print(s[['c','e']])

Output:
c 3
e 5
dtype: int64

DATAFRAME PROGRAMMING

Create a DataFrame from List


import pandas as pd
data = [1,2,3,4,5]
df = [Link](data)
print(df)

Create an Empty DataFrame


import pandas as pd
df = [Link]()
print(df)

Create DataFrame from Dictionary of List


import pandas as pd
data = [{'x':1,'y':2},{'x':5,'y':6,'z':4}]
df = [Link](data)
print(df)

6
Create DataFrame from Dictionary of Series
import pandas as pd
d={
'One': [Link]([1,2,3], index=['a','b','c']),
'Two': [Link]([1,2,3,4], index=['a','b','c','d'])
}
df = [Link](d)
print(df)

Column Addition in DataFrame


import pandas as pd
data = {'A':[1,2,3], 'B':[4,5,6]}
df = [Link](data)
df['C'] = [7,8,9]
print(df)

Column Deletion in DataFrame


del df['A']
[Link]('B')

Rows Selection, Addition and Deletion


import pandas as pd
df1 = [Link]([[1,2],[3,4]], columns=['a','b'])
df2 = [Link]([[5,6],[7,8]], columns=['a','b'])
df1 = [Link](df2)
df1 = [Link](0)
print(df1)

Indexing a DataFrame Using LOC


import pandas as pd
import numpy as np
df = [Link]([Link](8,4),
index=['a','b','c','d','e','f','g','h'],
columns=['A','B','C','D'])
print([Link][:,'A'])

Output:
Numerical values
dtype: float64

DATA VISUALISATION

Presentation of data in graphical form helps to understand significance of data.


It helps to communicate information clearly and effectively.
Data visualisation plays a vital role in representing both small and large scale data.

7
Examples of Data Visualisation:
1) Matplotlib
2) Seaborn

Purpose of Data Visualisation:


1) Finding patterns
2) Understanding business insight
3) Better analysis
4) Identifying trends
5) Easy to understand and memorise

Plotting Libraries – Matplotlib

Matplotlib is a Python package that is used to create 2D graphs and plots by using Python
scripts or codes.
Pyplot is a module in Matplotlib which supports a very wide variety of graphs and plots.

Types of Graphs Supported

1. Histogram
2. Bar chart
3. Pie chart
4. Line chart
5. Scatter chart
6. Power Spectra
7. etc.

Matplotlib is used with NumPy to provide an environment similar to MATLAB.


Features of Matplotlib

• Drawing graphs easily


• Plots can be drawn based on past data through expressive functions
• Customisation:
Plots can be customised as per requirement by specifying colours, width, labels, title,
legends, etc.
• Saving:
After drawing and customising, plots can be saved for future use.

Types of Plots

• Line Plot
• Bar Chart
• Histogram

Line Plot

8
A line plot is a graph that shows the frequency of data occurring along a number line.
The line plot is represented by a series of data points connected by straight lines.
Line plots are generally used to display trends over time.
A line plot can be created using the plot() function available in the pyplot library.

Example – Line Plot

import numpy as np
import pandas as pd
import [Link] as plt

Year = [2020, 2021, 2022, 2023, 2024, 2025]


bpspercentage = [92, 95, 98, 94, 89, 96]
dpspercentage = [91, 93, 96, 88, 94, 97]

[Link](Year, bpspercentage, color='green')


[Link](Year, dpspercentage, color='red')
[Link]('Year')
[Link]('Pass Percentage')
[Link]('School Pass Percentage')
[Link]()

Bar Graph

A bar graph drawn using rectangular bars is used to show how large each value is.
A bar graph can be horizontal or vertical.
It makes it easy to compare data between various groups.
A bar graph represents categories on one axis and discrete values on the other axis.
It can also show big changes in data over time.

Example – Bar Graph

import numpy as np
import [Link] as plt

labels = ['Anurag', 'Ananya', 'Mayank', 'Bhoomi', 'Pratham', 'Om', 'Saumya']


marks = [73, 84, 47, 29, 63, 55, 71]
index = [Link](len(labels))

[Link](index, marks)
[Link]('Students Name', fontsize=12)
[Link]('Marks', fontsize=12)
[Link]('Students Marks Percentage')
[Link]()

Histogram

9
A histogram is a graphical representation which organises a group of data points into user-
specified ranges.
It provides a visual representation of numerical data by showing the number of data points
that fall within a specified range of values (bins).
A histogram is similar to a vertical bar graph but without gaps between the bars.

Example – Histogram

import numpy as np
import [Link] as plt

data = [11, 12, 13, 25, 35, 45, 55]


[Link](data, bins=[0, 10, 20, 30, 40, 50, 60], edgecolor='blue')
[Link]('Bins')
[Link]('Marks')
[Link]('Student Marks')
[Link]()

10

You might also like