0% found this document useful (0 votes)
3 views141 pages

Python Libraries

The document is a beginner's guide to using the Pandas library in Python for data analysis and manipulation. It covers key concepts including the installation, core data structures (Series and DataFrame), and various operations such as data cleaning, transformation, and visualization. Additionally, it provides examples of how to create, access, and manipulate data within these structures.

Uploaded by

madhan07052006
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)
3 views141 pages

Python Libraries

The document is a beginner's guide to using the Pandas library in Python for data analysis and manipulation. It covers key concepts including the installation, core data structures (Series and DataFrame), and various operations such as data cleaning, transformation, and visualization. Additionally, it provides examples of how to create, access, and manipulate data within these structures.

Uploaded by

madhan07052006
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 | Machine Learning | AI 🚀

@[Link]

Pandas For
for Beginners
beginners
Easy and detailed
easy
guide
by [Link]
Chapter 1

INTRODUCTION TO
PANDAS
1. What is Pandas?
2. Use of Pandas
3. Installation and Setup
4. Overview of Pandas' Core Data Structures
What is Pandas?
Pandas is a powerful and flexible open-source data
analysis and manipulation library for Python.
It is widely used in data science and analytics to work
with structured data.

It provides easy-to-use data structures and data


analysis tools for handling numerical tables and time
series data.

Use of Pandas
Pandas is commonly used for:

Data Cleaning: Handling missing data, removing


duplicates, and transforming data formats.

Data Transformation: Aggregating, merging, and


reshaping datasets.

Exploratory Data Analysis (EDA): Summarizing


data and generating descriptive statistics.

Data Visualization: Creating plots and graphs


(with integration to libraries like Matplotlib).

Time Series Analysis: Handling and analyzing


time series data effectively.

2
Installation and Setup
To get started with Pandas, you need to install it.

If you are using Python's package manager, pip, you


can install Pandas by running:

pip install pandas

Alternatively, if you are using Anaconda, Pandas


comes pre-installed, or you can install it using:

conda install pandas

Pandas' Core Data Structures


Pandas offers two main data structures that are
commonly used in data analysis: Series (1D) and
DataFrame (2D).

Both structures are integral to data manipulation and


analysis in Pandas.

2
Series:
A one-dimensional array-like object that can hold
any data type (integers, strings, floats, etc.).
It is similar to a column in a spreadsheet or a
database table.
Each element in a Series is associated with an
index label.

import pandas as pd

s = [Link]([1, 2, 3, 4], index=['a', 'b',


'c', 'd'])
print(s)

DataFrame:

A two-dimensional table with labeled axes (rows


and columns).
It is akin to a spreadsheet or SQL table.

Each column in a DataFrame can hold data of


different types (integers, strings, floats, etc.).

2
import pandas as pd

df = [Link]({
'A': [1, 2, 3],
'B': [4, 5, 6]
}, index=['row1', 'row2', 'row3'])
print(df)

2
Chapter 2

SERIES: THE 1D
DATA STRUCTURE
1. Creating a Series
2. Accessing Data in a Series
3. Operations on Series
Creating a Series
A Series in Pandas is a one-dimensional array-like
structure that can hold data of any type—integers,
strings, floats, etc.

Each element in a Series has a unique label called an


index.

You can create a Series using different types of data,


such as lists, dictionaries, or even scalar values.

Example 1: Creating a Series from a List

import pandas as pd

data = [10, 20, 30, 40]


s = [Link](data)
print(s)

This creates a Series with default integer index labels


(0, 1, 2, 3).

2
Example 2: Creating a Series with Custom Index

import pandas as pd

data = [10, 20, 30, 40]


s = [Link](data, index=['a', 'b', 'c',
'd'])
print(s)

Here, the Series is created with custom index labels


('a', 'b', 'c', 'd').

Example 3: Creating a Series from a Dictionary

import pandas as pd

data = {'a': 10, 'b': 20, 'c': 30, 'd': 40}


s = [Link](data)
print(s)

2
Accessing Data in a Series
You can access the data in a Series using the index
labels or position-based indexing (similar to
accessing elements in a list or dictionary).

Example 1: Accessing Data Using Index Labels

import pandas as pd

s = [Link]([10, 20, 30, 40], index=['a',


'b', 'c', 'd'])
print(s['b'])

This will print the value associated with index 'b',


which is 20.

Example 2: Accessing Data Using Position-Based


Indexing

import pandas as pd

s = [Link]([10, 20, 30, 40], index=['a',


'b', 'c', 'd'])
print([Link][2])

This will print the value at the 2nd position in the


Series, which is 30.

2
Example 3: Accessing Multiple Elements

import pandas as pd

s = [Link]([10, 20, 30, 40], index=['a',


'b', 'c', 'd'])
print(s[['a', 'd']])

This will print the values associated with indexes 'a'


and 'd', which are 10 and 40 respectively.

2
Operations on Series
Pandas allows you to perform various operations on
Series, such as arithmetic operations, aggregation,
and element-wise operations.

Example 1: Arithmetic Operations You can add,


subtract, multiply, or divide Series by a scalar or
another Series.

import pandas as pd

s1 = [Link]([10, 20, 30, 40])


s2 = [Link]([1, 2, 3, 4])

# Adding two Series


result = s1 + s2
print(result)

This will add corresponding elements in s1 and s2,


resulting in a new Series.

2
Example 2: Aggregation Operations You can use
functions like sum(), mean(), min(), and max() to
perform aggregation on Series.

import pandas as pd

s = [Link]([10, 20, 30, 40])

# Sum of all elements


print([Link]())

# Mean of all elements


print([Link]())

This will print the sum (100) and mean (25.0) of the
Series.

Example 3: Element-Wise Operations You can apply


functions to each element in a Series using the
apply() method.

import pandas as pd

s = [Link]([1, 2, 3, 4])

# Square each element in the Series


result = [Link](lambda x: x**2)
print(result)

2
This will return a Series with each element squared.

Series Represented By:

<class '[Link]'>

Which indicates that the object is an instance of the


Series class from the [Link] module in
the Pandas library.

2
Chapter 3

DATAFRAME: THE 2D
DATA STRUCTURE
1. Creating a DataFrame
2. Accessing Data in a DataFrame
3. Adding and Removing Columns
4. DataFrame Attributes and Methods
5. Applications of DataFrame
6. Commonly used DataFrame functions
Creating a DataFrame
A DataFrame in Pandas is a two-dimensional table,
similar to a spreadsheet or a SQL table.
It consists of rows and columns, where each column
can hold different types of data (e.g., integers,
strings, floats).
You can create a DataFrame from various data
sources such as dictionaries, lists, or even external
files like CSVs.

Example 1: Creating a DataFrame from a Dictionary

import pandas as pd

data = {
'Name': ['Alice', 'Bob', 'Charlie'],
'Age': [25, 30, 35],
'City': ['New York', 'Los Angeles',
'Chicago']
}
df = [Link](data)
print(df)

This creates a DataFrame where the dictionary keys


become column names, and the values become the
rows.

2
Accessing Data in a DataFrame
You can access data in a DataFrame using various
methods, such as accessing specific columns, rows,
or even specific elements.

Example 1: Accessing a Single Column

import pandas as pd

df = [Link]({
'Name': ['Alice', 'Bob', 'Charlie'],
'Age': [25, 30, 35]
})

# Accessing the 'Name' column


print(df['Name'])

This will print the entire 'Name' column as a Series.

2
Example 2: Accessing Multiple Columns

import pandas as pd

df = [Link]({
'Name': ['Alice', 'Bob', 'Charlie'],
'Age': [25, 30, 35]
'City': ['New York', 'Los Angeles',
'Chicago']
})

# Accessing the 'Name' column


print(df['Name', 'City'])

This will print the 'Name' and 'City' columns as a new


DataFrame.

2
Example 3: Accessing Rows Using .loc and .iloc

.loc[]: Accesses rows by label/index name.


.iloc[]: Accesses rows by position.

import pandas as pd

df = [Link]({
'Name': ['Alice', 'Bob', 'Charlie'],
'Age': [25, 30, 35]
})

# Accessing the row with label 1


print([Link][1])

# Accessing the first row by position


print([Link][0])

This will print the row data for the corresponding


label or position.

2
Example 4: Accessing Specific Elements

import pandas as pd

df = [Link]({
'Name': ['Alice', 'Bob', 'Charlie'],
'Age': [25, 30, 35]
})

# Accessing the element in the first row and


'Age' column
print([Link][0, 'Age'])

# Accessing the element by position


print([Link][1, 0])

This will print specific elements from the DataFrame.

2
Adding and Removing Columns
Pandas makes it easy to add or remove columns in a
DataFrame.

Example 1: Adding a New Column

import pandas as pd

df = [Link]({
'Name': ['Alice', 'Bob', 'Charlie'],
'Age': [25, 30, 35]
})

# Adding a new column 'City'


df['City'] = ['New York', 'Los Angeles',
'Chicago']
print(df)

This will add a new column 'City' to the existing


DataFrame.

2
Example 2: Removing a Column

import pandas as pd

df = [Link]({
'Name': ['Alice', 'Bob', 'Charlie'],
'Age': [25, 30, 35],
'City': ['New York', 'Los Angeles',
'Chicago']
})

# Removing the 'City' column


df = [Link](columns=['City'])
print(df)

This will remove the 'City' column from the


DataFrame.

2
DataFrame Attributes and Methods
Pandas DataFrames come with several attributes and
methods that help you understand and manipulate
the data.

Example 1: Common Attributes

.shape: Returns the dimensions of the DataFrame


(rows, columns).
.columns: Returns the column labels.
.index: Returns the row labels.

import pandas as pd

df = [Link]({
'Name': ['Alice', 'Bob', 'Charlie'],
'Age': [25, 30, 35]
})

# Getting the shape of the DataFrame


print([Link])

# Getting the column names


print([Link])

# Getting the row index


print([Link])

2
Example 2: Common Methods

.head(n): Returns the first n rows (default is 5).


.tail(n): Returns the last n rows (default is 5).
.describe(): Provides summary statistics for
numerical columns.
.info(): Provides a concise summary of the
DataFrame.

import pandas as pd

df = [Link]({
'Name': ['Alice', 'Bob', 'Charlie'],
'Age': [25, 30, 35],
'City': ['New York', 'Los Angeles',
'Chicago']
})

# Displaying the first 2 rows


print([Link](2))

# Getting summary statistics


print([Link]())

# Getting a concise summary


[Link]()

2
Applications of DataFrame
Work with Data Sets: Load and view data.
Analyze Data: Perform sorting, filtering, and
aggregation.
Clean Data: Drop or handle missing values.
Process Data: Transform and manipulate data.
Integrate Data: Merge or join multiple data
sources.
Export Data: Save to Excel, CSV, JSON, or binary
formats.
Math & Stats: Perform calculations and statistical
operations.
Group Data: Aggregate data using group by.

Commonly used DataFrame functions


Loading and Saving Data:
pd.read_csv()
DataFrame.to_csv()
Viewing Data:
[Link]()
[Link]()
Selecting Data:
[Link][]
[Link][]

2
Filtering and Sorting:
[Link]()
DataFrame.sort_values()
Data Cleaning:
[Link]()
[Link]()
Data Processing:
[Link]()
Grouping and Aggregation:
[Link]()
[Link]()

Merging and Joining:


[Link]()

Represented By:

<class '[Link]'>

2
Chapter 4

BASIC DATA
OPERATIONS
1. Indexing and Slicing
2. Filtering Data
3. Handling Missing Data
4. Sorting Data
Indexing and Slicing
Indexing and slicing help you access and manipulate
parts of your data.

Indexing: Refers to accessing a specific item in a list


or series using its position.

For Example:

numbers = [10, 20, 30]


print(numbers[0]) # Output: 10

Slicing: Allows you to access a range of items.

For Example:

numbers = [10, 20, 30, 40, 50]


print(numbers[1:4]) # Output: [20, 30, 40]

2
And in Pandas we can do like this:

import pandas as pd

df = [Link]({
'A': [1, 2, 3, 4],
'B': [5, 6, 7, 8]
})
print([Link][1])
# Output: A 2
# B 6
# Name: 1, dtype: int64

print([Link][1, 1]) # Output: 6

Here, [Link][1] gives the whole row at index 1,


showing the values for columns 'A' and 'B'.
[Link][1, 1] retrieves the value at the 2nd row and
2nd column directly. So, the output '6' is from
[Link][1, 1].

2
Filtering Data
Filtering helps you select specific data based on
conditions.

Basic Filtering: Select rows where a condition is true.


For Example:

import pandas as pd

df = [Link]({
'A': [1, 2, 3, 4],
'B': [5, 6, 7, 8]
})
print([Link][1])
# Output: A 2
# B 6
# Name: 1, dtype: int64

print([Link][1, 1]) # Output: 6

Here, [Link][1] shows the entire row at index 1.


[Link][1, 1] gives the specific value at the 2nd row
and 2nd column. So, '6' is the value from [Link][1, 1].

2
Multiple Conditions: Combine conditions using &
(and) or | (or).

For Example:

print(df[(df['Age'] > 25) & (df['Age'] < 35)])

# Output:
# Name Age
# 1 Bob 30

Here, The code filters the DataFrame to show rows


where 'Age' is between 25 and 35.
The output shows only the row for 'Bob' who is 30
years old.

2
Handling Missing Data
Handling Missing Data involves dealing with data
entries that are missing or incomplete.

Identifying Missing Data: Check for missing values.

For Example:

import pandas as pd
import numpy as np

df = [Link]({
'Name': ['Alice', 'Bob', [Link]],
'Age': [25, [Link], 35]
})
print([Link]()) # Output:
# Name Age
# 0 False False
# 1 False True
# 2 True False

Here, [Link]() checks for missing values in the


DataFrame.

The output shows True where there are NaN values


and False where data is present.

2
Filling Missing Data: Replace missing values with a
specific value.

For Example:

print([Link]({'Name': 'Unknown', 'Age':


0}))

# Output:
# Name Age
# 0 Alice 25.0
# 1 Bob 0.0
# 2 Unknown 35.0

Here, [Link]() replaces missing values with


specified values.

In this case, it fills missing 'Name' with 'Unknown'


and missing 'Age' with 0.

2
Dropping Missing Data: Remove rows with missing
values.

For Example:

print([Link]())

# Output:
# Name Age
# 0 Alice 25.0
# 2 Unknown 35.0

Here, [Link]() removes rows with any missing


values.

The output shows only the rows with complete data.

2
Sorting Data
Sorting Data helps you arrange data in a specific
order.

Sorting by Column: Sort a DataFrame by one column.

For Example:

df = [Link]({
'Name': ['Alice', 'Bob', 'Charlie'],
'Age': [25, 30, 35]
})

print(df.sort_values(by='Age'))

# Output:
# Name Age
# 0 Alice 25
# 1 Bob 30
# 2 Charlie 35

Here, df.sort_values(by='Age') sorts the DataFrame


by the 'Age' column.

The output shows the rows in ascending order of age.

2
Sorting in Descending Order: Use ascending=False.

For Example:

df = [Link]({
'Name': ['Alice', 'Bob', 'Charlie'],
'Age': [25, 30, 35]
})

print(df.sort_values(by='Age',
ascending=False))

# Output:
# Name Age
# 2 Charlie 35
# 1 Bob 30
# 0 Alice 25

Here, df.sort_values(by='Age', ascending=False)


sorts the DataFrame by 'Age' in descending order.

The output shows rows with the highest age first.

2
Sorting by Multiple Columns: Sort by more than one
column.

For Example:

df = [Link]({
'Name': ['Alice', 'Bob', 'Charlie',
'Alice'],
'Age': [25, 30, 30, 20]
})

print(df.sort_values(by=['Age', 'Name']))

# Output:
# Name Age
# 3 Alice 20
# 0 Alice 25
# 1 Bob 30
# 2 Charlie 30

Here, df.sort_values(by=['Age', 'Name']) sorts the


DataFrame first by 'Age', then by 'Name'.

The output shows rows sorted by age, and for the


same age, sorted by name.

2
Chapter 5

DATA
MANIPULATION
1. Adding and Modifying Rows/Columns
2. Merging and Joining DataFrames
3. Concatenating DataFrames
4. Grouping and Aggregating Data
Adding and Modifying Rows/Columns
Adding and modifying rows or columns lets you
update your DataFrame with new data or adjust
existing data.

Adding Columns: You can add a new column to a


DataFrame.

For Example:

import pandas as pd

df = [Link]({
'Name': ['Alice', 'Bob'],
'Age': [25, 30]
})
df['City'] = ['New York', 'Los Angeles']
print(df)

# Output:
# Name Age City
# 0 Alice 25 New York
# 1 Bob 30 Los Angeles

Here, The code imports pandas, creates a DataFrame


with 'Name' and 'Age', then adds a 'City' column. It
prints the table with Alice and Bob's info, including
their age and city.

2
Adding Rows: You can add a new row to a
DataFrame.

For Example:

new_row = [Link]({'Name': ['Charlie'],


'Age': [35], 'City': ['Chicago']})

df = [Link]([df, new_row],
ignore_index=True)

print(df)

# Output:
# Name Age City
# 0 Alice 25 New York
# 1 Bob 30 Los Angeles
# 2 Charlie 35 Chicago

Here, The code creates a new DataFrame for Charlie,


then concatenates it with the existing DataFrame.
The updated table now includes Alice, Bob, and
Charlie's information, showing their name, age, and
city.

2
Modifying Columns: You can update an existing
column's values.

For Example:

df['Age'] = df['Age'] + 1
print(df)

# Output:
# Name Age City
# 0 Alice 26 New York
# 1 Bob 31 Los Angeles
# 2 Charlie 36 Chicago

Here, The code updates the 'Age' column by adding 1


to each value. The new table shows Alice, Bob, and
Charlie with their updated ages.

2
Merging and Joining DataFrames
Merging and joining combine data from multiple
DataFrames based on common columns or indices.

Merging DataFrames: Combine DataFrames based on


a common column.

For Example:

df1 = [Link]({
'ID': [1, 2, 3],
'Name': ['Alice', 'Bob', 'Charlie']
})

df2 = [Link]({
'ID': [1, 2, 4],
'Age': [25, 30, 40]
})

merged_df = [Link](df1, df2, on='ID',


how='inner')
print(merged_df)

# Output:
# ID Name Age
# 0 1 Alice 25
# 1 2 Bob 30

2
Types of Joins:

inner: Only includes rows with matching keys in


both DataFrames.

left: Includes all rows from the left DataFrame


and matching rows from the right DataFrame.

right: Includes all rows from the right DataFrame


and matching rows from the left DataFrame.

outer: Includes all rows from both DataFrames,


with NaNs for missing matches.

2
Concatenating DataFrames
Concatenating combines DataFrames either vertically
(adding rows) or horizontally (adding columns).

Concatenating Vertically: Stack DataFrames on top


of each other.

For Example:

df1 = [Link]({
'Name': ['Alice', 'Bob'],
'Age': [25, 30]
})
df2 = [Link]({
'Name': ['Charlie', 'David'],
'Age': [35, 40]
})
concatenated_df = [Link]([df1, df2],
ignore_index=True)
print(concatenated_df)
# Output:
# Name Age
# 0 Alice 25
# 1 Bob 30
# 2 Charlie 35
# 3 David 40

2
Concatenating Horizontally: Combine DataFrames
side by side.

For Example:

df1 = [Link]({
'Name': ['Alice', 'Bob'],
'Age': [25, 30]
})
df2 = [Link]({
'City': ['New York', 'Los Angeles']
})
concatenated_df = [Link]([df1, df2],
axis=1)
print(concatenated_df)
# Output:
# Name Age City
# 0 Alice 25 New York
# 1 Bob 30 Los Angeles

2
Grouping and Aggregating Data
Grouping and aggregating allow you to summarize
and analyze data by dividing it into groups based on a
specific column and then performing calculations.

Grouping Data: Group rows that have the same


values in specified columns.

For Example:

df = [Link]({
'Department': ['HR', 'Finance', 'HR',
'Finance', 'IT'],
'Employee': ['Alice', 'Bob', 'Charlie',
'David', 'Eve'],
'Salary': [50000, 60000, 55000, 62000, 70000]
})

grouped_df = [Link]('Department').mean()
print(grouped_df)

# Output:
# Salary
# Department
# Finance 61000.0
# HR 52500.0
# IT 70000.0

2
Aggregating Data: Perform calculations on each
group.

For Example:

grouped_df =
[Link]('Department').agg({'Salary':
['mean', 'sum']})

print(grouped_df)

# Output:
# Salary
# mean sum
# Department
# Finance 61000.0 122000
# HR 52500.0 105000
# IT 70000.0 70000

2
Chapter 6

DATA CLEANING
1. Identifying and Handling Missing Data
2. Removing Duplicates
3. Data Type Conversion
4. Renaming Columns and Indexes
Identifying and Handling Missing Data
Missing data is a common issue in datasets. Missing
values can cause errors or inaccurate results during
analysis, so it’s important to identify and handle
them.

How to Identify Missing Data: You can identify


missing data using the .isnull() method, which
returns True for missing values, or .info() to see an
overview.

For Example:

import pandas as pd

# Sample data
data = {'Name': ['Alice', 'Bob', 'Charlie',
None],
'Age': [25, None, 30, 22]}
df = [Link](data)

# Identifying missing data


print([Link]())

2
Handling Missing Data:

Remove missing data: Use .dropna() to remove


rows with missing values.
Fill missing data: Use .fillna() to replace missing
values with a specific value.

For Example:

# Dropping rows with missing values


df_cleaned = [Link]()

# Filling missing values with a default value


df_filled = [Link]('Unknown')

2
Removing Duplicates
Duplicates in data can lead to incorrect conclusions,
so they need to be removed. You can remove
duplicate rows using .drop_duplicates().
For Example:

# Sample data with duplicates


data = {'Name': ['Alice', 'Bob', 'Alice',
'Charlie'],
'Age': [25, 22, 25, 30]}
df = [Link](data)

# Removing duplicates
df_no_duplicates = df.drop_duplicates()
print(df_no_duplicates)

This ensures only unique rows remain in the dataset.

2
Data Type Conversion
Sometimes, data in a column might not have the
correct type (e.g., a column of numbers may be
stored as strings). You can convert data types using
the .astype() method.

For Example:

# Sample data with wrong data types


data = {'Name': ['Alice', 'Bob'],
'Age': ['25', '30']}

# Age is stored as strings


df = [Link](data)

# Converting the 'Age' column to integers


df['Age'] = df['Age'].astype(int)
print([Link])

This ensures that the data is stored in the correct


format for analysis.

2
Renaming Columns and Indexes
To make your data easier to work with, you might
want to rename columns or index labels. This can be
done using the .rename() method.

For Example:

# Sample data
data = {'name': ['Alice', 'Bob'], 'age': [25,
30]}
df = [Link](data)

# Renaming columns
df_renamed = [Link](columns={'name':
'Name', 'age': 'Age'})
print(df_renamed)

You can rename columns and indexes to make the


dataset more understandable.

2
Chapter 7

DATA VISUALIZATION
WITH PANDAS
1. Plotting Data with Pandas
2. Basic Plot Types (Line, Bar, Histogram, etc.)
3. Customizing Plots
Plotting Data with Pandas
Pandas has built-in functionality to create simple
visualizations, making it easy to plot data directly
from DataFrames. The .plot() method is the main
function used to create plots.

For Example:

import pandas as pd
import [Link] as plt

# Sample data
data = {'Year': [2018, 2019, 2020, 2021],
'Sales': [200, 300, 400, 350]}
df = [Link](data)

# Plotting a line graph


[Link](x='Year', y='Sales', kind='line')
[Link]()

This generates a basic line plot of the sales over the


years.

2
Basic Plot Types(Line, Bar, Histogram)
Pandas supports several types of plots. You can
specify the type of plot using the kind argument in
the .plot() function.

Line Plot: Useful for showing trends over time.

For Example:

[Link](x='Year', y='Sales', kind='line')


[Link]()

Bar Plot: Useful for comparing categories.

For Example:

[Link](x='Year', y='Sales', kind='bar')


[Link]()

2
Histogram: Useful for showing the distribution of a
variable.

For Example:

# Sample data for histogram


data = {'Age': [22, 25, 25, 30, 22, 35, 30,
22]}
df = [Link](data)

# Plotting a histogram
[Link](y='Age', kind='hist', bins=5)
[Link]()

2
Customizing Plots
You can customize plots to make them more
informative by adding titles, labels, colors, and
adjusting the figure size.

For Example:

# Customizing the line plot


[Link](x='Year', y='Sales', kind='line',
color='green', figsize=(8, 6))

# Adding labels and title


[Link]('Yearly Sales')
[Link]('Year')
[Link]('Sales')
[Link]()

You can customize almost every aspect of the plot to


make it more readable and visually appealing.

2
Chapter 8

WORKING WITH
DATES AND TIMES
1. DateTime in Pandas
2. Converting Strings to DateTime
3. DateTime Operations
Customizing Plots
You can customize plots to make them more
informative by adding titles, labels, colors, and
adjusting the figure size.

For Example:

# Customizing the line plot


[Link](x='Year', y='Sales', kind='line',
color='green', figsize=(8, 6))

# Adding labels and title


[Link]('Yearly Sales')
[Link]('Year')
[Link]('Sales')
[Link]()

You can customize almost every aspect of the plot to


make it more readable and visually appealing.

2
DateTime in Pandas
Pandas provides powerful tools to work with dates
and times using the datetime module.

Dates and times are stored in a special data type


called datetime64 in pandas, which allows for
efficient time-based operations.

For Example:

import pandas as pd

# Sample data
data = {'Date': ['2023-01-01', '2023-02-01',
'2023-03-01']}
df = [Link](data)

# Converting the 'Date' column to datetime


df['Date'] = pd.to_datetime(df['Date'])
print(df)

Here, with the help of pandas we convert the string


dates into datetime objects that you can work with
more easily.

2
DateTime Operations
Pandas allows you to perform various operations on
datetime data, such as extracting specific parts (e.g.,
year, month) or performing calculations (e.g.,
adding/subtracting days).

For Example 1: Extracting Year, Month, Day

# Sample data
data = {'Date': ['2023-01-01', '2023-02-01']}
df = [Link](data)
df['Date'] = pd.to_datetime(df['Date'])

# Extracting year, month, and day


df['Year'] = df['Date'].[Link]
df['Month'] = df['Date'].[Link]
df['Day'] = df['Date'].[Link]
print(df)

Here, the code converts the 'Date' column to


DateTime format and then extracts the year, month,
and day into new columns.

Then, the updated DataFrame shows the original date


along with the separated year, month, and day.

2
Example 2: Adding/Subtracting Days You can use
[Link] to add or subtract time from a date.

# Adding 5 days to the date


df['Date_Added'] = df['Date'] +
[Link](days=5)
print(df)

# Subtracting 7 days from the date


df['Date_Subtracted'] = df['Date'] -
[Link](days=7)
print(df)

Here, the code adds 5 days to the 'Date' column and


stores the result in a new 'Date_Added' column.

It also subtracts 7 days from the date, saving it in a


'Date_Subtracted' column.

The DataFrame now displays the original date along


with the adjusted dates.

2
Chapter 9

INPUT/OUTPUT
OPERATIONS
1. Reading Data from CSV, Excel, and other formats
2. Writing Data to Files
3. Working with Large Datasets
Reading Data from CSV, Excel, and
Other Formats
Pandas makes it easy to load data from various
formats like CSV and Excel into DataFrames using
simple functions.

Now we will check how to read CSV, Excel files with


the help of Pandas.

Reading CSV Files: CSV (Comma-Separated Values)


is one of the most common formats for storing data.

You can read a CSV file using pd.read_csv().

For Example :

import pandas as pd

# Reading data from a CSV file


df = pd.read_csv('[Link]')

# Displaying the first 5 rows


print([Link]())

Here, the code reads data from a CSV file into a


DataFrame using pandas and then displays the first 5
rows of the data using [Link]().

2
Reading Excel Files: You can also read data from
Excel files using pd.read_excel(). You may need to
specify the sheet name.

For Example :

# Reading data from an Excel file


df = pd.read_excel('[Link]',
sheet_name='Sheet1')

# Displaying the first 5 rows


print([Link]())

Here, the code reads data from an Excel file,


specifically from 'Sheet1', into a DataFrame using
pandas.

It then displays the first 5 rows of the data using


[Link]().

2
Writing Data to Files
Once you've worked on your data, you can save it
back to various formats like CSV, Excel, or JSON
using Pandas' to_* methods.

Writing Data to a CSV File: You can save your


DataFrame as a CSV file using df.to_csv().

For Example :

# Writing data to a CSV file


df.to_csv('[Link]', index=False)

Writing Data to an Excel File: Similarly, you can


write data to an Excel file using df.to_excel().

For Example :

# Writing data to an Excel file


df.to_excel('[Link]', index=False)

You can also save the data in other formats, such as


JSON, by using the `to_json()` method.

2
Working with Large Datasets
When working with large datasets, it’s important to
handle the data efficiently to avoid memory issues.

Some ways to handle large datasets include reading


the data in chunks and optimizing memory usage.

Reading Data in Chunks: If the dataset is too


large to fit into memory, you can load it in smaller
chunks using the chunksize parameter in
read_csv().

For Example :

# Reading large CSV in chunks


chunk_size = 1000
for chunk in pd.read_csv('large_data.csv',
chunksize=chunk_size):
print([Link]())

Here, the code reads a large CSV file in chunks of


1000 rows at a time using pandas. And then it prints
the first 5 rows of each chunk.

2
Optimizing Memory Usage: You can reduce
memory usage by specifying data types when
reading the file.

For Example :

# Reducing memory usage by specifying data


types
df = pd.read_csv('large_data.csv', dtype=
{'column_name': 'int32'})

Here, the code reads a CSV file while specifying data


types for columns to reduce memory usage. For
example, it sets 'column_name' to use the 'int32' data
type.

2
Chapter 9

ADVANCED TOPICS
1. Pivot Tables
2. Reshaping Data (Melt and Pivot)
3. MultiIndex DataFrames
Pivot Tables
Pivot tables are a powerful tool to summarize and
analyze data.

They allow you to aggregate data based on different


criteria, similar to pivot tables in Excel.

Creating a Pivot Table: Use the pd.pivot_table()


function to create a pivot table from your DataFrame.

For Example :

import pandas as pd

# Sample data
data = {'Date': ['2023-01-01', '2023-01-01',
'2023-02-01'],
'Category': ['A', 'B', 'A'],
'Sales': [200, 150, 300]}
df = [Link](data)

# Creating a pivot table


pivot_table = pd.pivot_table(df,
values='Sales', index='Date',
columns='Category', aggfunc='sum')
print(pivot_table)

In this example, the pivot table summarizes the total


sales for each category on each date.

2
Reshaping Data (Melt and Pivot)
Reshaping data helps in transforming the structure of
your DataFrame to make it more suitable for analysis.

Melt: [Link]() is used to unpivot a DataFrame from


wide format to long format. It helps in converting
columns into rows.

For Example :

# Sample data
data = {'Date': ['2023-01-01', '2023-02-01'],
'Sales_A': [200, 300],
'Sales_B': [150, 250]}
df = [Link](data)

# Melting the DataFrame


melted_df = [Link](df, id_vars=['Date'],
value_vars=['Sales_A', 'Sales_B'],
var_name='Category', value_name='Sales')
print(melted_df)

In this example, the code creates a DataFrame with


sales data, then melts it to reshape the DataFrame.

It converts 'Sales_A' and 'Sales_B' columns into a


single 'Category' column with corresponding 'Sales'
values, while keeping 'Date' as the identifier.

2
Pivot: [Link]() is used to reshape data from long
format to wide format, creating a DataFrame with
hierarchical indexing.

For Example :

# Sample data
melted_data = {'Date': ['2023-01-01', '2023-
01-01', '2023-02-01', '2023-02-01'],
'Category': ['Sales_A',
'Sales_B', 'Sales_A', 'Sales_B'],
'Sales': [200, 150, 300, 250]}
melted_df = [Link](melted_data)

# Pivoting the DataFrame


pivoted_df = melted_df.pivot(index='Date',
columns='Category', values='Sales')
print(pivoted_df)

In this example, the code pivots a melted DataFrame


to reshape it, converting 'Category' values into
columns and using 'Date' as the index.

The result shows 'Sales_A' and 'Sales_B' as separate


columns with their respective sales figures.

2
MultiIndex DataFrames
A MultiIndex DataFrame allows you to have multiple
levels of indexing on rows and columns. This is
useful for working with hierarchical data.

Creating a MultiIndex DataFrame: Use the


[Link].from_tuples() function to create a
MultiIndex, and then apply it to your DataFrame.

For Example :

# Sample data with multi-level index


arrays = [['2023-01', '2023-01', '2023-02',
'2023-02'],
['A', 'B', 'A', 'B']]
index = [Link].from_arrays(arrays,
names=('Month', 'Category'))

data = {'Sales': [200, 150, 300, 250]}


df = [Link](data, index=index)
print(df)

In this example, the DataFrame features a


hierarchical index with 'Month' and 'Category' as its
levels. The code constructs this DataFrame and
displays sales data organized by these multi-level
indices, providing sales figures for each month-
category pair.

2
Akash • Python & Tech Enthusiast
@[Link]
🚀

FIL
RING SELECTING
TE
LO RO RING
EXP TA COLUMNS W
DA S
SO
PBY R
U DA TIN
RO TA G

HA SSIN
G
CO DING
NS

MI LUES
ND
VA
LUM

LIN
AD

G
G
SAV LES
CSV
CSV NG
S
FILE
DI

ING
FI
REA

PANDAS
Cheatsheet

ALL NOTES ARE UPLOADED ON TELEGRAM


- CHECK THE LINK IN BIO
Akash • Python & Tech Enthusiast
@[Link]
🚀

Install & Import Pandas


Install Pandas

$ pip install pandas

Import Pandas

import pandas as pd

Explanation:
Pandas helps us work with tables, CSV files, and
structured data easily.

ALL NOTES ARE UPLOADED ON TELEGRAM


- CHECK THE LINK IN BIO
Akash • Python & Tech Enthusiast
@[Link]
🚀

Sample CSV File


[Link]
Name Age City Marks
Akash 21 Pune 88

Riya 22 Mumbai 92

Aman 20 Delhi 76

Sneha 23 Bangalore 95

Rahul 21 Pune 67

Priya 22 Delhi 81

we will
CS V file
is the le s.
This xam p
ra ll e
e fo
us

ALL NOTES ARE UPLOADED ON TELEGRAM


- CHECK THE LINK IN BIO
Akash • Python & Tech Enthusiast
@[Link]
🚀

Read CSV File


Read Data

import pandas as pd

students = pd.read_csv("[Link]")
print(students)

Output
Name Age City Marks
0 Akash 21 Pune 88
1 Riya 22 Mumbai 92
2 Aman 20 Delhi 76
3 Sneha 23 Bangalore 95
4 Rahul 21 Pune 67
5 Priya 22 Delhi 81

Explanation:
read_csv() loads CSV data into a DataFrame.

ALL NOTES ARE UPLOADED ON TELEGRAM


- CHECK THE LINK IN BIO
Akash • Python & Tech Enthusiast
@[Link]
🚀

Quick Exploration
View First 5 Rows

print([Link]())

Output
Name Age City Marks
0 Akash 21 Pune 88
1 Riya 22 Mumbai 92
2 Aman 20 Delhi 76
3 Sneha 23 Bangalore 95
4 Rahul 21 Pune 67

Explanation:
head() shows the first rows of the dataset.

ALL NOTES ARE UPLOADED ON TELEGRAM


- CHECK THE LINK IN BIO
Akash • Python & Tech Enthusiast
@[Link]
🚀

Dataset Information
Check Info

print([Link]())

Output
<class '[Link]'>
RangeIndex: 6 entries, 0 to 5
Data columns (total 4 columns):
Name 6 non-null object
Age 6 non-null int64
City 6 non-null object
Marks 6 non-null int64

Explanation:
info() gives column names, data types, and missing
values.

ALL NOTES ARE UPLOADED ON TELEGRAM


- CHECK THE LINK IN BIO
Akash • Python & Tech Enthusiast
@[Link]
🚀

Select Columns
Single Column

print(students["Name"])

Output
0 Akash
1 Riya
2 Aman
3 Sneha
4 Rahul
5 Priya

Explanation:
Use column names to select specific data.

ALL NOTES ARE UPLOADED ON TELEGRAM


- CHECK THE LINK IN BIO
Akash • Python & Tech Enthusiast
@[Link]
🚀

Select Columns
Multiple Columns

print(students[["Name", "Marks"]])

Output
Name Marks
0 Akash 88
1 Riya 92
2 Aman 76
3 Sneha 95
4 Rahul 67
5 Priya 81

Explanation:
Use column names to select specific data.

ALL NOTES ARE UPLOADED ON TELEGRAM


- CHECK THE LINK IN BIO
Akash • Python & Tech Enthusiast
@[Link]
🚀

Filter Rows
Students With Marks Above 80

print(students[students["Marks"] > 80])

Output
Name Age City Marks
0 Akash 21 Pune 88
1 Riya 22 Mumbai 92
3 Sneha 23 Bangalore 95
5 Priya 22 Delhi 81

Explanation:
Filtering helps us get rows matching a condition.

ALL NOTES ARE UPLOADED ON TELEGRAM


- CHECK THE LINK IN BIO
Akash • Python & Tech Enthusiast
@[Link]
🚀

Multiple Conditions
Students From Pune With Marks Above 70

print(students[(students["City"] == "Pune") &


(students["Marks"] > 70)])

Output
Name Age City Marks
0 Akash 21 Pune 88

Explanation:
Use & for AND and | for OR conditions.

ALL NOTES ARE UPLOADED ON TELEGRAM


- CHECK THE LINK IN BIO
Akash • Python & Tech Enthusiast
@[Link]
🚀

Sorting Data
Sort By Marks

print(students.sort_values(by="Marks", ascending=False))

Output
Name Age City Marks
3 Sneha 23 Bangalore 95
1 Riya 22 Mumbai 92
0 Akash 21 Pune 88
5 Priya 22 Delhi 81
2 Aman 20 Delhi 76
4 Rahul 21 Pune 67

Explanation:
We can create new columns easily in Pandas.

ALL NOTES ARE UPLOADED ON TELEGRAM


- CHECK THE LINK IN BIO
Akash • Python & Tech Enthusiast
@[Link]
🚀

Add New Column


Add Grade Column

students["Grade"] = ["B", "A", "C", "A", "D", "B"]


print(students)

Output
Name Age City Marks Grade
0 Akash 21 Pune 88 B
1 Riya 22 Mumbai 92 A
2 Aman 20 Delhi 76 C
3 Sneha 23 Bangalore 95 A
4 Rahul 21 Pune 67 D
5 Priya 22 Delhi 81 B

Explanation:
sort_values() arranges data in ascending or descending
order.

ALL NOTES ARE UPLOADED ON TELEGRAM


- CHECK THE LINK IN BIO
Akash • Python & Tech Enthusiast
@[Link]
🚀

GroupBy
Average Marks By City

print([Link]("City")["Marks"].mean())

Output
City
Bangalore 95.0
Delhi 78.5
Mumbai 92.0
Pune 77.5

Explanation:
groupby() helps summarize and analyze grouped data.

ALL NOTES ARE UPLOADED ON TELEGRAM


- CHECK THE LINK IN BIO
Akash • Python & Tech Enthusiast
@[Link]
🚀

Missing Values
Check Missing Values

print([Link]().sum())

Output
Name 0
Age 0
City 0
Marks 0
Grade 0

Explanation:
isnull().sum() checks missing values in each column.

ALL NOTES ARE UPLOADED ON TELEGRAM


- CHECK THE LINK IN BIO
Akash • Python & Tech Enthusiast
@[Link]
🚀

Save CSV File


Save Updated Data

students.to_csv("updated_students.csv", index=False)

Explanation:
to_csv() saves the DataFrame into a new CSV file.

updated_students.csv

ALL NOTES ARE UPLOADED ON TELEGRAM


- CHECK THE LINK IN BIO
AKASH • PYTHON & TECH ENTHUSIAST
@[Link]

NUMPY CHEAT SHEET

BASIC TO ADVANCE
by [Link]

ALL NOTES ARE UPLOADED ON TELEGRAM


@[Link] – CHECK THE LINK IN BIO
AKASH • PYTHON & TECH ENTHUSIAST
@[Link]

LEVEL 0: WHAT IS NUMPY & WHY USE IT

1. What is NumPy?
NumPy (Numerical Python) is a Python library used to work
with numbers in the form of arrays and matrices. It helps you
perform fast mathematical and scientific computations.

2. Why not just use Python lists?


Python lists are slow for heavy calculations.
NumPy arrays are Faster, Memory efficient, Built for math, ML,
data, images

3. Where is NumPy used?


Machine Learning
Data Science
Image Processing
Deep Learning
Scientific Computing

ALL NOTES ARE UPLOADED ON TELEGRAM


@[Link] – CHECK THE LINK IN BIO
AKASH • PYTHON & TECH ENTHUSIAST
@[Link]

LEVEL 1: INSTALLATION & IMPORTING NUMPY

1. Install NumPy
If you are using pip:

pip install numpy

If you are using Anaconda:

conda install numpy

This downloads and installs NumPy in your Python


environment.

2. Import NumPy in Python

import numpy as np

Explanation:
numpy is the full library name
np is a short alias (standard practice)
We use np to access all NumPy functions

ALL NOTES ARE UPLOADED ON TELEGRAM


@[Link] – CHECK THE LINK IN BIO
AKASH • PYTHON & TECH ENTHUSIAST
@[Link]

LEVEL 1: INSTALLATION & IMPORTING NUMPY

3. Check NumPy Version

import numpy as np
print(np.__version__)

This shows which version of NumPy is installed on your


system.

4. First Test: Create a Simple Array

import numpy as np
arr = [Link]([1, 2, 3, 4, 5])
print(arr)

Explanation:
[Link]() converts a Python list into a NumPy array
arr now becomes a fast numerical array

ALL NOTES ARE UPLOADED ON TELEGRAM


@[Link] – CHECK THE LINK IN BIO
AKASH • PYTHON & TECH ENTHUSIAST
@[Link]

LEVEL 2: CREATING NUMPY ARRAYS


In this level, we learn different ways to create arrays.
1. Create Array from Python List (1D)
import numpy as np
arr1 = [Link]([10, 20, 30, 40])
print(arr1)

Creates a 1D array
Similar to a list, but faster and math-friendly
2. Create 2D Array (Matrix)
arr2 = [Link]([[1, 2, 3],
[4, 5, 6]])
print(arr2)

Each inner list becomes a row


Used in tables, images, datasets
3. Create 3D Array
arr3 = [Link]([
[[1, 2], [3, 4]],
[[5, 6], [7, 8]]
])
print(arr3)

Used in videos, deep learning, 3D data

ALL NOTES ARE UPLOADED ON TELEGRAM


@[Link] – CHECK THE LINK IN BIO
AKASH • PYTHON & TECH ENTHUSIAST
@[Link]

LEVEL 2: CREATING NUMPY ARRAYS

4. Create Array with Range (arange)


arr4 = [Link](1, 10)
print(arr4)

Creates numbers from 1 to 9


Similar to Python range() but returns array

5. Zeros Array
zeros_arr = [Link]((3, 4))
print(zeros_arr)

Creates a 3x4 matrix filled with 0

6. Ones Array
ones_arr = [Link]((2, 3))
print(ones_arr)

Creates a 2x3 matrix filled with 1

7. Full Array (Custom Value)


full_arr = [Link]((3, 3), 7)
print(full_arr)

Creates a 3x3 matrix filled with 7

ALL NOTES ARE UPLOADED ON TELEGRAM


@[Link] – CHECK THE LINK IN BIO
AKASH • PYTHON & TECH ENTHUSIAST
@[Link]

LEVEL 2: CREATING NUMPY ARRAYS

8. Identity Matrix (eye)


eye_arr = [Link](4)
print(eye_arr)

Diagonal = 1, rest = 0
Used in linear algebra

9. Data Type (dtype)


arr_float = [Link]([1, 2, 3], dtype=float)
print(arr_float)

Forces array type (int, float, etc.)

ALL NOTES ARE UPLOADED ON TELEGRAM


@[Link] – CHECK THE LINK IN BIO
AKASH • PYTHON & TECH ENTHUSIAST
@[Link]

LEVEL 3: ARRAY PROPERTIES (SHAPE, SIZE,


DIMENSION, RESHAPE, TYPE)
In this level, we understand how to inspect an array.

1. Shape (Rows, Columns)


import numpy as np
arr = [Link]([[1, 2, 3],
[4, 5, 6]])
print([Link])

Output: (2, 3)
Means 2 rows and 3 columns

2. Size (Total Elements)


print([Link])

Output: 6
Total number of elements

3. Number of Dimensions
print([Link])

Output: 2
1 = 1D, 2 = 2D, 3 = 3D, etc.

ALL NOTES ARE UPLOADED ON TELEGRAM


@[Link] – CHECK THE LINK IN BIO
AKASH • PYTHON & TECH ENTHUSIAST
@[Link]

LEVEL 3: ARRAY PROPERTIES (SHAPE, SIZE,


DIMENSION, RESHAPE, TYPE)
4. Data Type
print([Link])

Shows the type of elements (int32, float64, etc.)

5. Reshape Array
new_arr = [Link](3, 2)
print(new_arr)

Changes shape without changing data


From (2,3) →
(3,2)

6. Flatten (Convert to 1D)


flat = [Link]()
print(flat)

Converts any array into 1D

ALL NOTES ARE UPLOADED ON TELEGRAM


@[Link] – CHECK THE LINK IN BIO
AKASH • PYTHON & TECH ENTHUSIAST
@[Link]

LEVEL 4: INDEXING & SLICING (ACCESSING


DATA)
This level teaches how to get specific values from arrays.
1. Indexing in 1D Array
import numpy as np
arr = [Link]([10, 20, 30, 40, 50])
print(arr[0])
print(arr[2])
print(arr[-1])

arr[0] → first element


arr[2] → third element
arr[-1] → last element
2. Slicing in 1D Array
print(arr[1:4])

Gets elements from index 1 to 3


3. Indexing in 2D Array (Row, Column)
matrix = [Link]([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
print(matrix[0, 1])

Row 0, Column 1 →2
ALL NOTES ARE UPLOADED ON TELEGRAM
@[Link] – CHECK THE LINK IN BIO
AKASH • PYTHON & TECH ENTHUSIAST
@[Link]

LEVEL 4: INDEXING & SLICING (ACCESSING


DATA)
4. Getting Full Row
print(matrix[1])

Returns second row

5. Getting Full Column


print(matrix[:, 2])

: means all rows


2 means third column

6. Slicing Rows & Columns


print(matrix[0:2, 1:3])

Rows 0 to 1
Columns 1 to 2

7. Boolean Indexing
arr2 = [Link]([5, 10, 15, 20, 25])
print(arr2[arr2 > 15])

Returns values greater than 15

ALL NOTES ARE UPLOADED ON TELEGRAM


@[Link] – CHECK THE LINK IN BIO
AKASH • PYTHON & TECH ENTHUSIAST
@[Link]

LEVEL 5: BASIC OPERATIONS ON NUMPY


ARRAYS
In this level, we perform mathematical operations directly on
arrays.

1. Element-wise Addition
import numpy as np
a = [Link]([1, 2, 3])
b = [Link]([10, 20, 30])
print(a + b)

Adds each element of a with corresponding element of b

2. Subtraction
print(b - a)

3. Multiplication
print(a * b)

Element-wise multiplication (not matrix multiply)

4. Division
print(b / a)

ALL NOTES ARE UPLOADED ON TELEGRAM


@[Link] – CHECK THE LINK IN BIO
AKASH • PYTHON & TECH ENTHUSIAST
@[Link]

LEVEL 5: BASIC OPERATIONS ON NUMPY


ARRAYS
5. Power
print(a ** 2)

Squares each element

6. Comparison Operations
print(a > 2)

Returns boolean array

7. Aggregation Functions
print([Link]())
print([Link]())
print([Link]())
print([Link]())

sum() →
total
mean() →
average
max() →
largest
min() →
smallest

ALL NOTES ARE UPLOADED ON TELEGRAM


@[Link] – CHECK THE LINK IN BIO
AKASH • PYTHON & TECH ENTHUSIAST
@[Link]

LEVEL 5: BASIC OPERATIONS ON NUMPY


ARRAYS
8. Axis-wise Operations (2D)
matrix = [Link]([[1, 2, 3],
[4, 5, 6]])
print([Link](axis=0))
print([Link](axis=1))

axis=0 → column-wise
axis=1 → row-wise

ALL NOTES ARE UPLOADED ON TELEGRAM


@[Link] – CHECK THE LINK IN BIO
AKASH • PYTHON & TECH ENTHUSIAST
@[Link]

LEVEL 6: BROADCASTING (WORKING WITH


DIFFERENT SHAPES)
Broadcasting allows NumPy to perform operations on arrays
of different shapes without using loops.

1. Array + Single Number


import numpy as np
arr = [Link]([1, 2, 3, 4])
result = arr + 10
print(result)

The number 10 is added to every element


NumPy automatically expands it to match array shape

2. 2D Array + 1D Array
matrix = [Link]([[1, 2, 3],
[4, 5, 6]])
add_arr = [Link]([10, 20, 30])
print(matrix + add_arr)

The 1D array is added to each row


Each column gets its matching value

ALL NOTES ARE UPLOADED ON TELEGRAM


@[Link] – CHECK THE LINK IN BIO
AKASH • PYTHON & TECH ENTHUSIAST
@[Link]

LEVEL 6: BROADCASTING (WORKING WITH


DIFFERENT SHAPES)
3. Column-wise Broadcasting
col = [Link]([[1],
[2],
[3]])
row = [Link]([10, 20, 30])
print(col + row)

Column vector + Row vector


Creates a full 2D grid by broadcasting

4. Broadcasting Rules (Simple)


Broadcasting works if:
Dimensions are equal, or
One of them is 1
Then NumPy stretches the smaller one.

ALL NOTES ARE UPLOADED ON TELEGRAM


@[Link] – CHECK THE LINK IN BIO
AKASH • PYTHON & TECH ENTHUSIAST
@[Link]

LEVEL 7: MATHEMATICAL & STATISTICAL


FUNCTIONS
NumPy provides many built-in math and stats functions.
1. Square Root, Log, Exponential
import numpy as np
arr = [Link]([1, 4, 9, 16])
print([Link](arr))
print([Link](arr))
print([Link](arr))

sqrt() → square root


log() → natural log
exp() → e power x
2. Mean, Median, Standard Deviation, Variance
data = [Link]([10, 20, 30, 40, 50])
print([Link](data))
print([Link](data))
print([Link](data))
print([Link](data))

Mean →
average
Median →
middle value
Std →
spread of data
Var →
square of std

ALL NOTES ARE UPLOADED ON TELEGRAM


@[Link] – CHECK THE LINK IN BIO
AKASH • PYTHON & TECH ENTHUSIAST
@[Link]

LEVEL 7: MATHEMATICAL & STATISTICAL


FUNCTIONS
3. Argmax & Argmin (Index of Max/Min)
print([Link](data))
print([Link](data))

Returns index of largest and smallest value

4. Sum Along Axis


matrix = [Link]([[1, 2, 3],
[4, 5, 6]])
print([Link](matrix, axis=0))
print([Link](matrix, axis=1))

Column-wise and row-wise sums

ALL NOTES ARE UPLOADED ON TELEGRAM


@[Link] – CHECK THE LINK IN BIO
AKASH • PYTHON & TECH ENTHUSIAST
@[Link]

LEVEL 8: SORTING & SEARCHING IN NUMPY


This level shows how to arrange data and find values.

1. Sorting an Array
import numpy as np
arr = [Link]([40, 10, 30, 20])
print([Link](arr))

Sorts the array in ascending order

2. Sorting a 2D Array (Row-wise)


matrix = [Link]([[3, 1, 2],
[6, 4, 5]])
print([Link](matrix))

Sorts each row separately

3. Argsort (Get Sorting Indexes)


arr = [Link]([50, 10, 30])
print([Link](arr))

Returns indexes that would sort the array

ALL NOTES ARE UPLOADED ON TELEGRAM


@[Link] – CHECK THE LINK IN BIO
AKASH • PYTHON & TECH ENTHUSIAST
@[Link]

LEVEL 8: SORTING & SEARCHING IN NUMPY


4. Where (Find Positions with Condition)
arr = [Link]([10, 20, 30, 40, 50])
print([Link](arr > 25))

Returns indexes where condition is true

5. Unique Values
arr = [Link]([1, 2, 2, 3, 3, 3, 4])
print([Link](arr))

Removes duplicates and returns unique values

ALL NOTES ARE UPLOADED ON TELEGRAM


@[Link] – CHECK THE LINK IN BIO
AKASH • PYTHON & TECH ENTHUSIAST
@[Link]

LEVEL 9: STACKING & SPLITTING ARRAYS


This level shows how to combine and break arrays.

1. Horizontal Stack (hstack)


import numpy as np
a = [Link]([1, 2, 3])
b = [Link]([4, 5, 6])
print([Link]((a, b)))

Joins arrays side by side

2. Vertical Stack (vstack)


print([Link]((a, b)))

Stacks arrays one above another (creates 2D array)

3. Concatenate
print([Link]((a, b)))

General way to join arrays

4. Split Array
arr = [Link]([10, 20, 30, 40, 50, 60])
print([Link](arr, 3))

Splits into 3 equal parts

ALL NOTES ARE UPLOADED ON TELEGRAM


@[Link] – CHECK THE LINK IN BIO
AKASH • PYTHON & TECH ENTHUSIAST
@[Link]

LEVEL 9: STACKING & SPLITTING ARRAYS


5. Horizontal & Vertical Split (2D)
matrix = [Link]([[1, 2, 3, 4],
[5, 6, 7, 8]])
print([Link](matrix, 2))
print([Link](matrix, 2))

hsplit → split columns


vsplit → split rows

ALL NOTES ARE UPLOADED ON TELEGRAM


@[Link] – CHECK THE LINK IN BIO
AKASH • PYTHON & TECH ENTHUSIAST
@[Link]

LEVEL 10: COPY VS VIEW (DEEP COPY &


SHALLOW COPY)
This level explains how NumPy handles memory.

1. View (Shallow Copy)


import numpy as np
arr = [Link]([10, 20, 30, 40])
view_arr = [Link]()
view_arr[0] = 100
print(arr)
print(view_arr)

Changing view_arr also changes arr


They share the same memory

2. Copy (Deep Copy)


copy_arr = [Link]()
copy_arr[1] = 200
print(arr)
print(copy_arr)

Changing copy_arr does NOT affect arr


New memory is created

ALL NOTES ARE UPLOADED ON TELEGRAM


@[Link] – CHECK THE LINK IN BIO
AKASH • PYTHON & TECH ENTHUSIAST
@[Link]

LEVEL 10: COPY VS VIEW (DEEP COPY &


SHALLOW COPY)
3. Check Memory Sharing
print([Link])
print(view_arr.base)
print(copy_arr.base)

view_arr.base points to original array


copy_arr.base is None (independent)

ALL NOTES ARE UPLOADED ON TELEGRAM


@[Link] – CHECK THE LINK IN BIO
AKASH • PYTHON & TECH ENTHUSIAST
@[Link]

LEVEL 11: NUMPY RANDOM MODULE


This level is about generating random numbers for ML,
testing, simulations.

1. Generate Random Float Numbers


import numpy as np
print([Link](5))

Gives 5 random numbers between 0 and 1

2. Random Integers
print([Link](1, 10, size=5))

Random integers between 1 and 9

3. Random 2D Array
print([Link](3, 3))

Creates a 3×3 matrix with random values

4. Set Seed (Same Random Every Time)


[Link](42)
print([Link](3))

Fixes randomness for reproducible results

ALL NOTES ARE UPLOADED ON TELEGRAM


@[Link] – CHECK THE LINK IN BIO
AKASH • PYTHON & TECH ENTHUSIAST
@[Link]

LEVEL 11: NUMPY RANDOM MODULE


5. Shuffle Array
arr = [Link]([10, 20, 30, 40, 50])
[Link](arr)
print(arr)

Randomly shuffles elements

6. Permutation
arr = [Link]([1, 2, 3, 4])
print([Link](arr))

Returns a shuffled copy (original unchanged)

ALL NOTES ARE UPLOADED ON TELEGRAM


@[Link] – CHECK THE LINK IN BIO
AKASH • PYTHON & TECH ENTHUSIAST
@[Link]

LEVEL 12: LINEAR ALGEBRA (CORE FOR ML &


AI)
This level covers matrix operations used in Machine Learning
and Deep Learning.

1. Dot Product (Vector / Matrix Multiplication)


import numpy as np
a = [Link]([1, 2, 3])
b = [Link]([4, 5, 6])
print([Link](a, b))

Multiplies and sums: 1*4 + 2*5 + 3*6

2. Matrix Multiplication
A = [Link]([[1, 2],
[3, 4]])
B = [Link]([[5, 6],
[7, 8]])
print([Link](A, B))

True matrix multiplication (not element-wise)

ALL NOTES ARE UPLOADED ON TELEGRAM


@[Link] – CHECK THE LINK IN BIO
AKASH • PYTHON & TECH ENTHUSIAST
@[Link]

LEVEL 12: LINEAR ALGEBRA (CORE FOR ML &


AI)

3. Transpose (Rows ↔ Columns)


print(A.T)

Converts rows into columns

4. Inverse of Matrix
inv_A = [Link](A)
print(inv_A)

Used in solving equations and ML formulas

5. Determinant
print([Link](A))

Tells if matrix is invertible (non-zero determinant)

6. Eigen Values (Advanced ML Concept)


values, vectors = [Link](A)
print(values)
print(vectors)

Used in PCA, data compression, ML math

ALL NOTES ARE UPLOADED ON TELEGRAM


@[Link] – CHECK THE LINK IN BIO
AKASH • PYTHON & TECH ENTHUSIAST
@[Link]

LEVEL 13: ADVANCED INDEXING & MASKING


This level shows how to select and modify data using
conditions and index lists.

1. Fancy Indexing (Using Index Lists)


import numpy as np
arr = [Link]([10, 20, 30, 40, 50])
print(arr[[0, 2, 4]])

Gets elements at positions 0, 2, and 4


Output: 10, 30, 50

2. Boolean Masking
data = [Link]([5, 12, 18, 25, 30])
mask = data > 15
print(mask)
print(data[mask])

mask creates True/False array


Only values > 15 are selected

3. Direct Condition Selection


print(data[data < 20])

Returns all values less than 20

ALL NOTES ARE UPLOADED ON TELEGRAM


@[Link] – CHECK THE LINK IN BIO
AKASH • PYTHON & TECH ENTHUSIAST
@[Link]

LEVEL 13: ADVANCED INDEXING & MASKING

4. Conditional Update
arr2 = [Link]([1, 2, 3, 4, 5])
arr2[arr2 % 2 == 0] = 0
print(arr2)

All even numbers are replaced with 0

5. Using [Link] (If-Else Style)


scores = [Link]([40, 60, 75, 30, 90])
result = [Link](scores >= 50, "Pass", "Fail")
print(result)

If score ≥ 50 → Pass
Else → Fail

ALL NOTES ARE UPLOADED ON TELEGRAM


@[Link] – CHECK THE LINK IN BIO
AKASH • PYTHON & TECH ENTHUSIAST
@[Link]

LEVEL 14: PERFORMANCE & VECTORIZATION


(WHY NUMPY IS FAST)
This level explains how to write fast NumPy code by avoiding
loops.

1. Python Loop vs NumPy Vectorization


import numpy as np
import time
arr = [Link](1_000_000)
start = [Link]()
result1 = []
for x in arr:
[Link](x * 2)
end = [Link]()
print("Python loop time:", end - start)
start = [Link]()
result2 = arr * 2
end = [Link]()
print("NumPy vectorized time:", end - start)

NumPy does multiplication in C (very fast)


No Python loop overhead

ALL NOTES ARE UPLOADED ON TELEGRAM


@[Link] – CHECK THE LINK IN BIO
AKASH • PYTHON & TECH ENTHUSIAST
@[Link]

LEVEL 14: PERFORMANCE & VECTORIZATION


(WHY NUMPY IS FAST)
2. Vectorized Functions
arr = [Link]([1, 4, 9, 16])
print([Link](arr))
print(arr ** 2)

Applies function to all elements at once

3. Avoid for-loops in NumPy


Bad (slow):

for i in range(len(arr)):
arr[i] = arr[i] + 5

Good (fast):

arr = arr + 5

4. Memory Efficiency Tip


arr = [Link](10)
arr += 5
print(arr)

Modifies in-place (no new array created)

ALL NOTES ARE UPLOADED ON TELEGRAM


@[Link] – CHECK THE LINK IN BIO
Akash • Python & Tech Enthusiast
@[Link]
🚀

MATPLOTLIB
Cheatsheet

ALL NOTES ARE UPLOADED ON TELEGRAM


- CHECK THE LINK IN BIO
Akash • Python & Tech Enthusiast
@[Link]
🚀

Install & Import


Install Matplotlib

$ pip install matplotlib

Import Matplotlib

import [Link] as plt

Explanation:
Matplotlib helps us create charts and graphs to visualize
data.

ALL NOTES ARE UPLOADED ON TELEGRAM


- CHECK THE LINK IN BIO
Akash • Python & Tech Enthusiast
@[Link]
🚀

Sample Data
Data we'll use for all examples

months = ["Jan", "Feb", "Mar", "Apr", "May"]


sales = [250, 320, 280, 450, 390]

Explanation:
This is the data we will use for all examples.

ALL NOTES ARE UPLOADED ON TELEGRAM


- CHECK THE LINK IN BIO
Akash • Python & Tech Enthusiast
@[Link]
🚀

Line Plot
Basic Line Chart

[Link](months, sales)
[Link]()

Output

Explanation:
plot() draws a line connecting the data points.

ALL NOTES ARE UPLOADED ON TELEGRAM


- CHECK THE LINK IN BIO
Akash • Python & Tech Enthusiast
@[Link]
🚀

Customize Line
Color, Style & Markers

[Link](months, sales, color="green", linestyle="--",


marker="o")
[Link]()

Output

Explanation:
Use color, linestyle, and marker to style the line.

ALL NOTES ARE UPLOADED ON TELEGRAM


- CHECK THE LINK IN BIO
Akash • Python & Tech Enthusiast
@[Link]
🚀

Labels, Title & Legend


Add Labels

[Link](months, sales, label="Sales")


[Link]("Month")
[Link]("Sales")
[Link]("Monthly Sales")
[Link]()
[Link]()

Output

Explanation:
Labels and titles make charts easier to understand.

ALL NOTES ARE UPLOADED ON TELEGRAM


- CHECK THE LINK IN BIO
Akash • Python & Tech Enthusiast
@[Link]
🚀

Bar Chart
Vertical Bars

[Link](months, sales, color="skyblue")


[Link]()

Output

Explanation:
bar() shows values as rectangular bars.

ALL NOTES ARE UPLOADED ON TELEGRAM


- CHECK THE LINK IN BIO
Akash • Python & Tech Enthusiast
@[Link]
🚀

Histogram
Distribution of Data

marks = [88, 92, 76, 95, 67, 81, 70, 85]


[Link](marks, bins=5, color="orange")
[Link]()

Output

Explanation:
hist() shows how data is distributed across ranges
(bins).

ALL NOTES ARE UPLOADED ON TELEGRAM


- CHECK THE LINK IN BIO
Akash • Python & Tech Enthusiast
@[Link]
🚀

Scatter Plot
Relationship Between Values

age = [21, 22, 20, 23, 21, 22]


marks = [88, 92, 76, 95, 67, 81]
[Link](age, marks, color="red")
[Link]()

Output

Explanation:
scatter() shows the relationship between two variables.

ALL NOTES ARE UPLOADED ON TELEGRAM


- CHECK THE LINK IN BIO
Akash • Python & Tech Enthusiast
@[Link]
🚀

Pie Chart
Show Proportions

sizes = [40, 30, 20, 10]


labels = ["Python", "Java", "C++", "Other"]
[Link](sizes, labels=labels, autopct="%1.1f%%")
[Link]()

Output

Explanation:
pie() shows parts of a whole as percentages.

ALL NOTES ARE UPLOADED ON TELEGRAM


- CHECK THE LINK IN BIO
Akash • Python & Tech Enthusiast
@[Link]
🚀

Multiple Lines
Compare Two Lines

sales_2023 = [250, 320, 280, 450, 390]


sales_2024 = [300, 360, 310, 500, 420]
[Link](months, sales_2023, label="2023")
[Link](months, sales_2024, label="2024")
[Link]()
[Link]()

Output

Explanation:
Plot multiple lines to compare different data sets.

ALL NOTES ARE UPLOADED ON TELEGRAM


- CHECK THE LINK IN BIO
Akash • Python & Tech Enthusiast
@[Link]
🚀

Subplots
Multiple Charts Together

[Link](1, 2, 1)
[Link](months, sales)
[Link](1, 2, 2)
[Link](months, sales)
[Link]()

Output

Explanation:
subplot() places several charts in one figure.

ALL NOTES ARE UPLOADED ON TELEGRAM


- CHECK THE LINK IN BIO
Akash • Python & Tech Enthusiast
@[Link]
🚀

Grid & Style


Add Grid & Theme

[Link]("ggplot")
[Link](months, sales)
[Link](True)
[Link]()

Output

Explanation:
Styles and grids make charts cleaner and easier to read.

ALL NOTES ARE UPLOADED ON TELEGRAM


- CHECK THE LINK IN BIO
Akash • Python & Tech Enthusiast
@[Link]
🚀

Figure Size
Resize the Chart

[Link](figsize=(8, 4))
[Link](months, sales)
[Link]()

Output

Explanation:
figsize sets the width and height of the figure.

ALL NOTES ARE UPLOADED ON TELEGRAM


- CHECK THE LINK IN BIO
Akash • Python & Tech Enthusiast
@[Link]
🚀

Save Figure
Save Your Plot

[Link](months, sales)
[Link]("sales_chart.png")

Explanation:
savefig() saves the chart as an image file.

ALL NOTES ARE UPLOADED ON TELEGRAM


- CHECK THE LINK IN BIO

You might also like