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

Data Analysis Using Python

Uploaded by

shloksharmawork
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 views60 pages

Data Analysis Using Python

Uploaded by

shloksharmawork
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

Data Analysis Using Python

(BOB IT Officer Exam Notes)


by 2026
Piyush Wairale

Instructions:
• Kindly go through the lectures/videos on our website [Link]

M
• Read this study material carefully and make your own handwritten short notes. (Short notes must not be

O
more than 5-6 pages)

.C
• Attempt the mock tests available on portal. LE
• Revise this material at least 5 times and once you have prepared your short notes, then revise your short
RA

notes twice a week


• If you are not able to understand any topic or required a detailed explanation and if there are any typos or
AI

mistake in study materials. Mail me at piyushwairale100@[Link]


S HW
YU
PI
Contents
1 Variables in Python 4

2 Data Types in Python 4

3 Lists 6
3.1 Python List Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7

4 Dictionaries 8
4.1 Python Dictionary Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9

5 Tuples 10
5.1 Creating tuples and basic operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
5.2 Python Tuple Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10

6 Sets 11
6.1 Python Set Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12

7 Control Flow 12
7.1 Conditional Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
7.2 Looping Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14

M
O
8 3.1 Loop Control Statements 15

.C
9 Functions Commonly Used with for Loops in Python LE 17

10 Error Handling in Python 21


RA

11 Pandas DataFrame Functions with Examples 26


AI

12 Data Reshaping 29
HW

13 Regular Expressions (Regex) and Slicing in Python 33

14 Slicing in Python 35
S
YU

15 Functions in Python 36
15.1 Recursion in Python Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
PI

16 File Management in Python 39

17 Importing and Exporting Data (Python vs R) 41

18 Charts and Graphs in Python (Matplotlib / Seaborn) 51

19 Charts and Graphs in R 53

20 Matplotlib Functions 55

21 Seaborn Functions (Complete List) 56

22 Seaborn vs Matplotlib (Comparison Table) 57

23 NumPy: Numerical Python 58

2
M
O
.C
LE
LinkedIn
RA
AI

Youtube Channel
HW

Instagram
S
YU

Telegram Group
PI

Facebook

Download Andriod App


Introduction
Python is a high-level, interpreted, and general-purpose programming language. It emphasizes readability, simplic-
ity, and flexibility, making it one of the most widely used languages for beginners and professionals alike.

1 Variables in Python
A variable is a name that refers to a value stored in memory. In Python, variables are created when you assign a
value to them.

Rules for Naming Variables


• Must start with a letter or underscore ( ).
• Can contain letters, numbers, and underscores.
• Case-sensitive (e.g., age and Age are different).
• Should not use Python keywords (e.g., if, for).

Examples

M
x = 10

O
name = "Alice"

.C
pi_value = 3.14159
_is_valid = True
LE
RA

2 Data Types in Python


AI

Python supports multiple built-in data types. Common ones include:


HW

Numeric Types
• int: Integer numbers
S

• float: Decimal (floating point) numbers


YU

• complex: Complex numbers with real and imaginary parts


PI

# Examples
a = 5 # int
b = 3.14 # float
c = 2 + 3j # complex

String (str)
A string is a sequence of characters enclosed in single, double, or triple quotes.

s1 = ’Hello’
s2 = "World"
s3 = ’’’Python allows
multiline strings’’’

Boolean (bool)
Represents truth values: True or False.

is_python_easy = True
is_java_easy = False
Checking Data Types
You can use the type() function to check the type of a variable.

x = 10
print(type(x)) # <class ’int’>

s = "Hello"
print(type(s)) # <class ’str’>

Type Casting
Python allows conversion between data types.

x = 5
y = float(x) # int to float
z = str(x) # int to string
a = int("10") # string to int

M
O
.C
LE
RA
AI
S HW
YU
PI
3 Lists
A list is an ordered, mutable collection of elements. Lists allow duplicates and can hold heterogeneous types.

Creating lists
# empty list
lst1 = []
# list from literals
lst2 = [1, 2, 3]
# heterogeneous list
lst3 = [1, ’two’, 3.0, [4,5]]
# list from iterable
lst4 = list(range(5)) # [0,1,2,3,4]
# list comprehension
squares = [x*x for x in range(6)] # [0,1,4,9,16,25]

Indexing and slicing


a = [’a’,’b’,’c’,’d’]
print(a[0]) # ’a’

M
print(a[2]) # ’c’

O
print(a[-1]) # ’d’

.C
print(a[-2]) # ’c’

letters = [’a’,’b’,’c’,’d’,’e’,’f’]
LE
print(letters[1:4]) # [’b’,’c’,’d’]
RA

print(letters[:3]) # [’a’,’b’,’c’]
print(letters[3:]) # [’d’,’e’,’f’]
AI

print(letters[-3:]) # [’d’,’e’,’f’]
print(letters[::2]) # [’a’,’c’,’e’]
HW

print(letters[::-1]) # reversed: [’f’,’e’,’d’,’c’,’b’,’a’]


# slicing creates a new list (shallow copy)
sub = letters[1:4]
S
YU

Note: Slicing returns a new list — it is a shallow copy. If elements inside the list are mutable, they are shared
between the original and the slice.
PI

Common list operations and examples


# concatenation and repetition
print([1,2] + [3,4]) # [1,2,3,4]
print([0] * 3) # [0,0,0]

# membership
print(3 in [1,2,3]) # True

# iteration
for i, v in enumerate([’x’,’y’]):
print(i, v)

# unpacking
a, b, c = [1,2,3]
print(a, b, c)
3.1 Python List Functions
Function Description Example
append(x) Adds element x at the end. a=[1,2]; [Link](3) ⇒ [1,2,3]
extend(iterable) Adds all elements of iterable to a=[1,2]; [Link]([3,4]) ⇒
list. [1,2,3,4]
insert(i,x) Inserts x at index i. a=[1,3]; [Link](1,2) ⇒ [1,2,3]
remove(x) Removes first occurrence of x. a=[1,2,2,3]; [Link](2) ⇒
[1,2,3]
pop() Removes and returns last ele- a=[1,2,3]; [Link]() ⇒ 3
ment.
pop(i) Removes and returns element at a=[10,20,30]; [Link](1) ⇒ 20
index i.
clear() Removes all elements. a=[1,2]; [Link]() ⇒ []
index(x) Returns index of first occurrence a=[10,20,30]; [Link](20) ⇒ 1
of x.
count(x) Counts occurrences of x. a=[1,2,2,3]; [Link](2) ⇒ 2
sort() Sorts list in ascending order. a=[3,1,2]; [Link]() ⇒ [1,2,3]
sort(reverse=True) Sorts list in descending order. a=[1,3,2]; [Link](reverse=True)
⇒ [3,2,1]

M
reverse() Reverses the list in-place. a=[1,2,3]; [Link]() ⇒ [3,2,1]
copy() Returns a shallow copy. a=[1,2]; b=[Link]() ⇒ [1,2]

O
len(list) Returns number of elements. len([1,2,3]) ⇒ 3

.C
max(list) Returns maximum element. max([1,9,3]) ⇒ 9
min(list) Returns minimum element. LE min([4,2,7]) ⇒ 2
sum(list) Returns sum of elements. sum([1,2,3]) ⇒ 6
any(list) True if any element is True. any([0,0,1]) ⇒ True
RA

all(list) True if all elements are True. all([1,2,3]) ⇒ True


list(iterable) Converts iterable to list. list("ABC") ⇒ [’A’,’B’,’C’]
AI
S HW
YU
PI
4 Dictionaries
A dict is a mutable mapping from keys to values. As of Python 3.7, dictionaries preserve insertion order. Keys
must be hashable (immutable types like strings, numbers, tuples of immutables).

Creating dictionaries
# literals
d1 = {’a’: 1, ’b’: 2}
# empty dict
d2 = {}
# from pairs
d3 = dict([(’x’, 9), (’y’, 10)])
# from keyword args
d4 = dict(x=1, y=2)
# dict from keys with same default
keys = [’a’,’b’,’c’]
d = [Link](keys, 0) # {’a’:0,’b’:0,’c’:0}

Accessing values and safe patterns

M
print(d1[’a’]) # 1 -- raises KeyError if ’a’ missing

O
# safer alternatives

.C
print([Link](’z’)) # None
print([Link](’z’, ’def’)) # ’def’ LE
# nested safe access
data = {’user’: {’name’: ’Priya’, ’age’: 30}}
RA

name = [Link](’user’, {}).get(’name’) # ’Priya’


AI

Merging dictionaries
HW

# Python 3.9+ merge operators


a = {’x’:1}
b = {’y’:2}
S

c = a | b # {’x’:1,’y’:2}
YU

a |= {’z’:3} # a becomes {’x’:1,’z’:3}


# older approach
PI

merged = {**a, **b}


4.1 Python Dictionary Functions
Function Description Example
get(key) Returns value of the given key; d={"a":10}; [Link]("a") ⇒ 10
returns None if key not found.
get(key, default) Returns default value if key not [Link]("x",0) ⇒ 0
found.
keys() Returns all keys as a view object. d={"a":1,"b":2}; [Link]()
values() Returns all values as a view ob- [Link]()
ject.
items() Returns list of (key,value) pairs. [Link]()
pop(key) Removes key and returns its d={"a":1,"b":2}; [Link]("a") ⇒ 1
value.
pop(key, default) Returns default value if key miss- [Link]("x", -1) ⇒ -1
ing.
popitem() Removes and returns last in- [Link]()
serted (key,value) pair.
clear() Removes all elements from dic- [Link]() ⇒ {}
tionary.
update(other dict) Updates dictionary with key/- d={"a":1}; [Link]({"b":2}) ⇒
{"a":1,"b":2}

M
value pairs from another dictio-
nary.

O
setdefault(key) Returns value if key exists; else d={"a":1}; [Link]("b") ⇒

.C
inserts and returns None. None
setdefault(key, If key not found, inserts key with
LE [Link]("c",100) ⇒ 100
default) default value.
copy() Returns a shallow copy of dictio- d={"a":1}; x=[Link]()
RA

nary.
len(dict) Returns number of key-value len({"a":1,"b":2}) ⇒ 2
AI

pairs.
in Checks if key exists in dictionary. "a" in {"a":1,"b":2} ⇒ True
HW

dict() Creates a dictionary. dict(a=1,b=2) ⇒ {"a":1,"b":2}


fromkeys(iterable) Creates new dict with keys from [Link](["a","b","c"]) ⇒
iterable, values None. {”a”:None, ”b”:None, ”c”:None}
S

Creates dict with given value for ⇒


YU

fromkeys(iterable, [Link](["x","y"],0)
value) all keys. {”x”:0,”y”:0}
PI
5 Tuples
A tuple is an immutable, ordered sequence of values. Use tuples for fixed collections and for keys in dictionaries
when appropriate.

5.1 Creating tuples and basic operations


t1 = () # empty tuple
t2 = (1, 2, 3) # tuple of ints
t3 = ("a", 1, 3.14) # heterogeneous tuple
t4 = (1,) # single element requires a trailing comma
t5 = tuple([1,2,3]) # from iterable
# indexing & slicing behave like lists
print(t2[0])
print(t2[-1])
print(t2[1:3])
# concatenation
print((1,2) + (3,4))
# repetition
print((1,) * 3)

M
5.2 Python Tuple Functions

O
.C
Function Description Example
count(x) Returns number of times x ap- LE t=(1,2,2,3); [Link](2) ⇒ 2
pears in the tuple.
index(x) Returns index of first occurrence t=(10,20,30); [Link](20) ⇒ 1
RA

of x.
len(tuple) Returns number of elements. len((1,2,3)) ⇒ 3
AI

max(tuple) Returns maximum value (for max((1,5,3)) ⇒ 5


comparable items).
HW

min(tuple) Returns minimum value. min((4,2,9)) ⇒ 2


sum(tuple) Returns sum of numeric ele- sum((1,2,3)) ⇒ 6
ments.
S

Converts iterable into a tuple. tuple([1,2,3]) ⇒ (1,2,3)


YU

tuple(iterable)
in Checks if an item exists in the 2 in (1,2,3) ⇒ True
tuple.
PI

sorted(tuple) Returns a sorted list (tuple re- sorted((3,1,2)) ⇒ [1,2,3]


mains unchanged).
6 Sets
A set is an unordered collection of unique elements. Use sets for membership tests and elimination of duplicates.
frozenset is the immutable variant.

Creating sets
s1 = {1,2,3}
s2 = set([1,2,2,3]) # duplicates removed -> {1,2,3}
s3 = set() # empty set (note: {} creates empty dict)
s4 = frozenset([1,2]) # immutable set

Set operations and methods


a = {1,2,3}
b = {3,4,5}
print(a | b) # union -> {1,2,3,4,5}
print(a & b) # intersection -> {3}
print(a - b) # difference -> {1,2}
print(a ^ b) # symmetric difference -> {1,2,4,5}

M
# set algebra methods

O
A = {1,2,3}

.C
B = {3,4}
print([Link](B)) # {1,2,3,4} LE
print([Link](B)) # {3}
print([Link](B)) # {1,2}
RA

print([Link]({1,2,3,4})) # True
AI
S HW
YU
PI
6.1 Python Set Functions
Function Description Example
add(x) Adds element x to the set. s={1,2}; [Link](3) ⇒ {1,2,3}
update(iterable) Adds all elements from iterable s={1}; [Link]([2,3]) ⇒ {1,2,3}
to set.
remove(x) Removes element x; error if not s={1,2}; [Link](2) ⇒ {1}
found.
discard(x) Removes element x; no error if s={1,2}; [Link](5)
not found.
pop() Removes and returns a random s={10,20,30}; [Link]()
element (since set is unordered).
clear() Removes all elements. [Link]() ⇒ {}
copy() Returns a shallow copy. s={1,2}; t=[Link]()
union(t) Returns new set with elements {1,2}.union({2,3}) ⇒ {1,2,3}
from both sets.
intersection(t) Returns common elements of {1,2,3}.intersection({2,3,4}) ⇒
sets. {2,3}
difference(t) Returns elements present in set {1,2,3}.difference({3,4}) ⇒ {1,2}
but not in t.

M
sym diff(t) Returns symmetric difference. {1,2,3} ^ {3,4} ⇒ {1,2,4}
intersection update(t) Updates current set to intersec- s={1,2,3};

O
tion with t. [Link] update({2,3})

.C
⇒ {2,3}
difference update(t) Removes elements found in t
LE s={1,2,3};
from the set. [Link] update({3}) ⇒ {1,2}
sym diff update(t) Updates set to symmetric differ- s={1,2};
RA

ence. [Link] difference update({2,3})


⇒ {1,3}
AI

issubset(t) Checks if set is a subset of t. {1,2}.issubset({1,2,3}) ⇒ True


issuperset(t) Checks if set is a superset of t. {1,2,3}.issuperset({2}) ⇒ True
HW

isdisjoint(t) True if sets have no common el- {1,2}.isdisjoint({3,4}) ⇒ True


ements.
len(set) Returns number of elements in len({1,2,3}) ⇒ 3
S

the set.
YU

set(iterable) Converts iterable to a set (re- set([1,2,2,3]) ⇒ {1,2,3}


moves duplicates).
PI

7 Control Flow
Control Flow Statements in Python
Control flow statements determine the order in which instructions are executed in a program. They allow decision-
making, looping, branching, and repeating actions based on conditions.
Python provides three major categories:
• Conditional statements (if, elif, else)
• Looping statements (for, while)

• Loop control statements (break, continue, pass)

7.1 Conditional Statements


Conditional statements allow Python to choose different execution paths based on logical conditions.
if Statement
Syntax:
if condition:
statement
Example:
x = 10
if x > 5:
print("x is greater than 5")
Output:
x is greater than 5

if-else Statement
If the condition is false, the else block executes.
x = 3
if x % 2 == 0:
print("Even")

M
else:
print("Odd")

O
Output:

.C
Odd LE
if-elif-else
RA

Used when multiple conditions need evaluation.


AI

marks = 85
HW

if marks >= 90:


print("Grade A")
S

elif marks >= 75:


YU

print("Grade B")
elif marks >= 60:
print("Grade C")
PI

else:
print("Grade D")
Output:
Grade B

Nested if
if statements inside another if block.
x = 10

if x > 0:
print("Positive")
if x % 2 == 0:
print("Even Number")
Output:
Positive
Even Number
7.2 Looping Statements
Loops are used to execute statements repeatedly.
Python supports:
• for loop
• while loop

1. for Loop
Used to iterate over sequences (list, tuple, string, range).
Syntax:

for variable in sequence:


statement

Example:

for i in range(1, 6):


print(i)

M
Output:

O
.C
1
2 LE
3
4
RA

5
AI

for Loop over List


HW

fruits = ["apple", "banana", "mango"]


for f in fruits:
print(f)
S
YU

Using break in for Loop


for i in range(1, 10):
PI

if i == 5:
break
print(i)

Output stops at 4.

Using continue in for Loop


for i in range(1, 6):
if i == 3:
continue
print(i)

Output skips 3.
2. while Loop
Executes as long as the condition is true.
Syntax:
while condition:
statements
Example:
i = 1
while i <= 5:
print(i)
i += 1
Output:
1
2
3
4
5

M
Infinite Loop Example

O
while True:

.C
print("Press Ctrl+C to stop")
LE
8 3.1 Loop Control Statements
RA

These modify the behavior of loops.


AI

1 break Statement
HW

Stops the loop immediately.


for i in range(10):
S

if i == 4:
YU

break
print(i)
PI

Output:
0
1
2
3

3.2 continue Statement


Skips the current iteration and continues.
for i in range(5):
if i == 2:
continue
print(i)
Output:
0
1
3
4
3.3 pass Statement
Does nothing. Placeholder for future code.

for i in range(5):
pass

4. else Block with Loops


Python allows else with loops. The else block executes only if the loop completes normally (without break).
Example:

for i in range(3):
print(i)
else:
print("Loop Completed")

Output:

0
1
2

OM
Loop Completed

.C
Example with break: LE
for i in range(3):
if i == 1:
RA

break
print(i)
I

else:
WA

print("Completed")
SH

Output:
YU

0
PI

Else block does not execute because loop broke.

5. Nested Loops
Loops inside another loop.

for i in range(1, 4):


for j in range(1, 3):
print(i, j)

Output:

1 1
1 2
2 1
2 2
3 1
3 2
6. Match-Case (Python 3.10+)
Similar to switch-case in other languages.

choice = 2

match choice:
case 1:
print("One")
case 2:
print("Two")
case _:
print("Default Case")

Output:

Two

9 Functions Commonly Used with for Loops in Python


Python provides several built-in functions that enhance the ability of for loops to iterate efficiently over sequences,
collections, and iterables.

OM
1. range() Function
.C
LE
range() generates a sequence of numbers and is the most commonly used function in for loops.
RA

Syntax:
I

range(start, stop, step)


WA

Examples:
SH

for i in range(5):
YU

print(i)
PI

Output:
0
1
2
3
4
Using start and step:

for i in range(1, 10, 2):


print(i)
Output:
1
3
5
7
9
2. enumerate() Function
enumerate() adds an index to each element during iteration.

fruits = ["apple", "banana", "mango"]

for index, value in enumerate(fruits):


print(index, value)

Output:

0 apple
1 banana
2 mango
Useful when you need both index and value.

3. zip() Function
zip() combines multiple sequences and iterates over them simultaneously.

M
names = ["Amit", "Riya", "Sam"]

O
scores = [85, 90, 78]

.C
for n, s in zip(names, scores): LE
print(n, s)
RA

Output:
Amit 85
AI

Riya 90
Sam 78
HW

4. sorted() Function
S
YU

sorted() sorts any iterable and returns a new sorted list.


PI

numbers = [5, 3, 9, 1]

for n in sorted(numbers):
print(n)

Output:
1
3
5
9
Sorting in reverse:
for n in sorted(numbers, reverse=True):
print(n)
5. reversed() Function
reversed() iterates over the sequence in reverse order.

items = [10, 20, 30]

for n in reversed(items):
print(n)

Output:

30
20
10

6. len() Function
len() returns the length of a sequence. often used with range()

names = ["A", "B", "C"]

for i in range(len(names)):

OM
print(i, names[i])

7. Iteration over Strings .C


LE
Strings are iterable, so each character can be accessed.
RA

for ch in "DATA":
I
WA

print(ch)
SH

Output:
D
YU

A
PI

T
A

8. Iteration over Dictionaries


Keys:

d = {"A":10, "B":20}

for k in d:
print(k)
Values:

for v in [Link]():
print(v)
Key-value pairs:
for k, v in [Link]():
print(k, v)
9. Iteration over Sets
Sets produce unique, unordered values.

s = {1, 2, 3}

for x in s:
print(x)

(No guaranteed order)

10. List Comprehension (For Loop Shortcut)


List comprehension is a compact form of a for loop.

squares = [x*x for x in range(5)]


print(squares)

Output:
[0, 1, 4, 9, 16]

M
O
.C
LE
RA
AI
S HW
YU
PI
10 Error Handling in Python
Error handling allows a program to respond gracefully when something goes wrong. Instead of terminating abruptly,
Python provides mechanisms to catch and manage errors using exception handling.
The main construct is the try-except block.

1. try-except Block
Basic structure:

try:
code that may cause an error
except:
code that runs if an error occurs

Example:

try:
x = 10 / 0
except:
print("An error occurred")

OM
Output:

An error occurred
.C
LE
2. Catching Specific Exceptions
RA

Python allows catching particular exception types.


I
WA

try:
SH

num = int("abc")
except ValueError:
YU

print("ValueError: Invalid conversion")


PI

Output:

ValueError: Invalid conversion

Common exception classes:


• ValueError
• ZeroDivisionError

• TypeError
• FileNotFoundError
• IndexError
• KeyError
3. Multiple except Blocks
Different errors can be handled separately.

try:
a = [1, 2, 3]
print(a[5])
except IndexError:
print("Index out of range")
except Exception:
print("General error")

4. else Block
The else block executes only if no exception occurs.

try:
x = 10 / 2
except ZeroDivisionError:
print("Cannot divide by zero")
else:
print("Division successful:", x)

OM
Output:

Division successful: 5.0 .C


LE
RA

5. finally Block
I

The finally block always executes, whether or not an exception occurs. Often used for cleanup (closing files, releasing
WA

resources).
SH

try:
YU

f = open("[Link]", "r")
print([Link]())
PI

except FileNotFoundError:
print("File not found")
finally:
print("Execution Completed")

Output:

File not found


Execution Completed

6. Raising Exceptions Using raise


Use raise to generate an exception manually.

age = -4

if age < 0:
raise ValueError("Age cannot be negative")

Output:

ValueError: Age cannot be negative


7. try-except-finally Combined Example
try:
a = int(input("Enter a number: "))
result = 10 / a
except ValueError:
print("Please enter a valid integer")
except ZeroDivisionError:
print("Cannot divide by zero")
else:
print("Result:", result)
finally:
print("Program ended")

8. Creating Custom Exceptions


User-defined exception classes must inherit from Exception.

class AgeError(Exception):
pass

M
O
age = -1

.C
try: LE
if age < 0:
raise AgeError("Age cannot be negative")
RA

except AgeError as e:
print(e)
AI

Output:
HW

Age cannot be negative


S

9. Why Error Handling is Important


YU

• Prevents sudden program crashes


PI

• Allows controlled handling of unexpected situations


• Improves debugging and stability
• Ensures resource cleanup
DataFrames in Python (Pandas)
A DataFrame is a two-dimensional, tabular, labeled data structure provided by the Pandas library in Python.
It is similar to an Excel sheet, SQL table, or R DataFrame. A DataFrame consists of rows (records), columns
(attributes), and an index.

1. Definition
A DataFrame is:
• Two-dimensional (rows × columns)
• Labeled (column names + row index)
• Heterogeneous (each column may have different data types)
• Mutable (data can be added, removed, modified)

2. Creating a DataFrame
(a) From a Python Dictionary
import pandas as pd
data = {"Name":["Amit","Riya","Piyush"], "Marks":[85,92,78], "City":["Mumbai","Delhi","Pune"]}

M
df = [Link](data)

O
(b) From a List of Lists

.C
df = [Link]([[1,"Amit"], [2,"Riya"]], columns=["Roll","Name"])
(c) From a CSV File LE
df = [Link] csv("[Link]")
(d) From an SQL Query
RA

df = [Link] sql("SELECT * FROM employees", connection)


AI

3. DataFrame Components
HW

A DataFrame contains:
• Index: Row labels (default: 0,1,2,. . . )
• Columns: Names of attributes
S
YU

• Values: Data stored in a 2D array


PI

4. Accessing Data
(a) Select a single column
df["Name"]
(b) Select multiple columns
df[["Name","City"]]
(c) Select row by label (loc)
[Link][1]
(d) Select row by position (iloc)
[Link][2]
(e) Select a specific cell
[Link][0,"Marks"]
[Link][1,2]

5. Filtering Data
(a) Conditional filtering
df[df["Marks"] > 80]
(b) Multiple conditions
df[(df["Marks"] > 80) & (df["City"] == "Mumbai")]
6. Adding New Columns
df["Status"] = ["Pass","Pass","Fail"]
Using a function:
df["Marks2"] = df["Marks"].apply(lambda x: x+5)

7. Removing Data
Drop a column:
[Link]("City", axis=1)
Drop multiple columns:
[Link](["Marks","City"], axis=1)
Drop a row:
[Link](1)

8. Descriptive Statistics
[Link]()
df["Marks"].mean()
df["Marks"].median()
df["Marks"].max()

M
df["Marks"].min()
df["Marks"].std()

O
.C
9. Handling Missing Values LE
Check missing values:
[Link]().sum()
RA

Drop missing values:


[Link]()
AI

Fill missing values:


[Link](0)
HW

10. Sorting Data


S

Sort by a column:
YU

[Link] values("Marks")
Sort descending:
PI

[Link] values("Marks", ascending=False)

11. Grouping Data (GroupBy)


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

12. Merging and Joining DataFrames


Merge (SQL JOIN):
[Link](df1, df2, on="Roll")
Join on index:
[Link](df2)

13. Concatenation
Row-wise:
[Link]([df1, df2], axis=0)
Column-wise:
[Link]([df1, df2], axis=1)
14. Exporting Data
To CSV:
[Link] csv("[Link]")
To Excel:
[Link] excel("[Link]")
To SQL Table:
[Link] sql("students", connection)

11 Pandas DataFrame Functions with Examples


Function Description Example
head(n) Returns first n rows. [Link](3)
tail(n) Returns last n rows. [Link](2)
info() Shows summary: columns, data [Link]()
types, memory use.
describe() Statistical summary of numeric [Link]()
columns.
shape Returns (rows, columns). [Link] ⇒ (5,3)
columns Returns list of column names. [Link]

M
dtypes Returns data type of each col- [Link]

O
umn.
rename() Renames columns. [Link](columns={"Name":"FullName"})

.C
drop() Drops rows or columns. LE [Link]("City", axis=1)
sort values() Sorts by column values. [Link] values("Marks")
sort index() Sorts DataFrame by index. [Link] index()
RA

loc[] Selects rows/columns by labels. [Link][0, "Marks"]


iloc[] Selects rows/columns by index [Link][1, 2]
positions.
AI

filter() Filters by column names using [Link](like="Name")


HW

patterns.
assign() Adds new columns. [Link](Age2=df["Age"]+1)
apply() Applies a function to columns or df["Marks"].apply(lambda x: x+5)
S

rows.
YU

astype() Converts data type of column. df["Age"] = df["Age"].astype(int)


isnull() Returns True for missing values. [Link]()
PI

notnull() Returns True for non-missing [Link]()


values.
fillna() Fills missing values. [Link](0)
dropna() Drops rows with missing values. [Link]()
groupby() Groups rows for aggregation. [Link]("City")["Marks"].mean()
agg() Multiple aggregations. [Link]({"Marks":["min","max"]})
merge() SQL-style join on columns. [Link](df1, df2, on="ID")
join() Join on index. [Link](df2)
concat() Appends rows or columns. [Link]([df1, df2], axis=0)
pivot() Reshapes data using columns. [Link](index="ID", columns="Year",
values="Sales")
pivot table() Pivot table with aggregation. [Link] table(values="Sales",
index="City", aggfunc="mean")
melt() Unpivots DataFrame into long [Link](id vars="Name")
format.
to csv() Exports to CSV file. [Link] csv("[Link]")
to excel() Exports to Excel file. [Link] excel("[Link]")
to sql() Writes DataFrame to SQL table. [Link] sql("students", connection)
value counts() Frequency count of a column. df["City"].value counts()
unique() Returns unique values of a col- df["City"].unique()
umn.
nunique() Count of unique values. df["City"].nunique()

M
O
.C
LE
RA
AI
S HW
YU
PI
R DataFrame Functions with Examples
Function Description Example
[Link]() Creates a new DataFrame. df <- [Link](Name, Marks,
City)
head(df) Displays first 6 rows. head(df)
tail(df) Displays last 6 rows. tail(df)
str(df) Shows structure, types, and pre- str(df)
view.
summary(df) Summary statistics of columns. summary(df)
names(df) Returns column names. names(df)
rownames(df) Returns row names. rownames(df)
nrow(df) Number of rows. nrow(df)
ncol(df) Number of columns. ncol(df)
dim(df) Returns (rows, columns). dim(df)
df$Column Accesses a column. df$Marks
df[, "col"] Column selection using name. df[, "City"]
df[i, ] Access row i. df[2, ]
df[i, j] Access cell at row i, column j. df[1, 2]
subset() Filter rows / select columns. subset(df, Marks > 80)

M
order() Sort DataFrame. df[order(df$Marks), ]

O
rbind() Combine row-wise. rbind(df1, df2)

.C
cbind() Combine column-wise. cbind(df1, df2)
merge() SQL-style join. LE merge(df1, df2, by="ID")
unique(df) Returns unique rows. unique(df)
duplicated(df) Checks for duplicate rows. duplicated(df)
RA

[Link](df) Finds missing values. [Link](df)


[Link](df) Removes rows with NA values. [Link](df)
AI

aggregate() Aggregates using group-by oper- aggregate(Marks ~ City, df,


ations. mean)
HW

transform() Adds or modifies columns. transform(df, Marks2 = Marks +


10)
within() Modify multiple columns. df <- within(df, {Marks2 <-
S
YU

Marks+5})
apply() Apply function on rows/- apply(df[,2:3], 2, mean)
columns.
PI

[Link]() Export DataFrame to CSV. [Link](df, "[Link]")


[Link]() Read CSV into DataFrame. df <- [Link]("[Link]")
12 Data Reshaping
Data reshaping refers to changing the structure, orientation, or layout of data without altering its underlying values.
It is widely used in data preprocessing, reporting, and analytics.
Common reshaping operations include:
• Converting wide data to long format

• Converting long data to wide format


• Pivoting
• Unstacking / stacking
• Merging and concatenation

Below are detailed reshaping concepts in Python (Pandas) and R, with examples and outputs.

1. Data Reshaping in Python (Pandas)


Example Dataset
Name Math Science

M
Amit 80 90

O
Riya 85 88

.C
a) melt(): Wide to Long LE
Code:
RA

df long = [Link](id vars="Name", var name="Subject", value name="Score")


Output:
Name Subject Score
AI

Amit Math 80
HW

Amit Science 90
Riya Math 85
Riya Science 88
S
YU

b) pivot(): Long to Wide


Code:
PI

df wide = df [Link](index="Name", columns="Subject", values="Score")


Output:
Name Math Science
Amit 80 90
Riya 85 88

c) pivot table(): Aggregated Pivot


Code:
[Link] table(values="Score", index="Name", columns="Subject", aggfunc="mean")
Output:
(Same as pivot – aggregation performed)
d) stack(): Column to Row Transformation
Code:
df stacked = [Link] index("Name").stack()
Output:
[Link] 80
[Link] 90
[Link] 85
[Link] 88

e) unstack(): Row to Column Transformation


Code:
df [Link]()
Output:
(Same as pivot)

f ) concat(): Joining DataFrames


Code:
[Link]([df1, df2], axis=0)

M
Output:
Rows of df1 and df2 are appended.

O
.C
g) merge(): SQL Style Join
Code:
LE
[Link](df1, df2, on="ID")
RA

Output:
ID Name Score
1 Amit 80
AI

2 Riya 90
S HW
YU
PI
2. Data Reshaping in R
Example Dataset
Name Math Science
Amit 80 90
Riya 85 88

a) melt(): Wide to Long


Code:
library(reshape2)
df long <- melt(df, [Link]="Name", [Link]="Subject", [Link]="Score")
Output:
Name Subject Score
Amit Math 80
Amit Science 90
Riya Math 85
Riya Science 88

M
b) dcast(): Long to Wide

O
Code:

.C
df wide <- dcast(df long, Name ~ Subject, [Link]="Score")
Output: LE
Name Math Science
Amit 80 90
RA

Riya 85 88
AI

c) pivot longer() : Wide to Long (tidyr)


HW

Code:
library(tidyr)
df long <- df %>% pivot longer(cols=Math:Science, names to="Subject", values to="Score")
S

Output:
YU

(Same as melt)
PI

d) pivot wider(): Long to Wide (tidyr)


Code:
df wide <- df long %>% pivot wider(names from=Subject, values from=Score)
Output:
(Same as dcast)

e) merge(): SQL Join


Code:
merge(df1, df2, by="ID")
Output:
ID Name Score
1 Amit 80
2 Riya 90
f ) rbind() and cbind()
Row Bind:
rbind(df1, df2)
Column Bind:
cbind(df1, df2)

M
O
.C
LE
RA
AI
HW
S
YU
PI
13 Regular Expressions (Regex) and Slicing in Python
1. Regular Expressions (Regex)
Regular Expressions (Regex) are patterns used to search, match, and manipulate text. Python provides the re
module for regex operations.

Key Regex Functions


Function Meaning Example Output
[Link]() Searches for first match [Link]("ai", "India") ⇒
match(”ai”)
[Link]() Returns all matches as list [Link]("a", "banana")
⇒ [”a”,”a”,”a”]
[Link]() Matches only at start of string [Link]("In", "India") ⇒
match(”In”)
[Link]() Replaces pattern with text [Link]("a","@", "banana")
⇒ b@n@n@
[Link]() Splits string by regex [Link]("\s+", "A B C") ⇒
[”A”,”B”,”C”]

M
O
Common Regex Patterns

.C
LE
RA
AI
S HW
YU
PI
Regex Example with Output
Code:
import re
text = "Contact: 9876543210"
[Link]("\d10", text)
Output:
["9876543210"]

Email validation example:


[Link]("[A-Za-z0-9. ]+@[A-Za-z]+\.[A-Za-z]2,3", "abc@[Link]")
Output:
match("abc@[Link]")

M
O
.C
LE
RA
AI
S HW
YU
PI
14 Slicing in Python
Slicing is used to extract a portion of a sequence (string, list, tuple) using the format:

sequence[start : end : step]

Rules
• start = index to begin slice (inclusive)

• end = stop slice (exclusive)


• step = jump value (default = 1)

Basic Slicing Examples


Code Output
s = "PYTHON" –
s[0:3] ”PYT”
s[:4] ”PYTH”
s[2:] ”THON”

M
s[-3:] ”HON”

O
s[::2] ”PTO”
s[::-1] ”NOHTYP” (reverse string)

.C
List Slicing Example
LE
RA

Code Output
lst = [10,20,30,40,50] –
lst[1:4] [20,30,40]
AI

lst[::-1] [50,40,30,20,10]
HW

lst[::2] [10,30,50]

Advanced Slicing
S
YU

Extract every 3rd character:


"ABCDEFGHI"[::3] ⇒ "ADG"
PI

Reverse from middle:


"PYTHON"[4::-1] ⇒ "HTYP"
15 Functions in Python
A function is a reusable block of code designed to perform a specific task.

Function Syntax
def function name(parameters):
statements
return value

Example 1: Simple Function


Code:
def add(a, b):
return a + b
print(add(5, 3))
Output:
8

Example 2: Function with Default Argument

M
Code:

O
def greet(name="Guest"):

.C
return "Hello, " + name
greet() LE
Output:
"Hello, Guest"
RA

Example 3: Function with Variable Arguments


AI

Code:
HW

def total(*nums):
return sum(nums)
total(1,2,3,4)
S

Output:
YU

10
PI

Example 4: Lambda Function


Code:
square = lambda x : x * x
square(6)
Output:
36

15.1 Recursion in Python Functions


Recursion is a programming technique where a function calls itself to solve a smaller instance of the same problem.
A recursive function must contain:

• Base Case – The stopping condition.


• Recursive Case – Function calling itself with reduced input.
General Syntax of a Recursive Function
def function name(parameters):
if base condition:
return value
else:
return function name(smaller input)

Example 1: Factorial Using Recursion


Code:
def factorial(n):
if n == 0:
return 1
return n * factorial(n-1)

print(factorial(5))
Output:
120

M
Explanation

O
5! = 5 × 4 × 3 × 2 × 1

.C
Base case: when n == 0, return 1.
LE
Example 2: Fibonacci Using Recursion
RA

Code:
AI

def fib(n):
if n <= 1:
HW

return n
return fib(n-1) + fib(n-2)
S

print(fib(6))
YU

Output:
8
PI

Explanation
Fibonacci series:
0, 1, 1, 2, 3, 5, 8, 13, . . .

Example 3: Sum of Digits Using Recursion


Code:
def sum digits(n):
if n == 0:
return 0
return (n % 10) + sum digits(n // 10)

print(sum digits(1234))
Output:
10
1 + 2 + 3 + 4 = 10
Example 4: Reverse a String Recursively
Code:
def reverse(s):
if len(s) == 0:
return s
return reverse(s[1:]) + s[0]

print(reverse("PYTHON"))
Output:
"NOHTYP"

M
O
.C
LE
RA
AI
S HW
YU
PI
16 File Management in Python
File management refers to reading from and writing to files stored on disk. Python provides built-in functions for
handling files using the open() function.

1. Opening a File
The syntax for opening a file is:
file = open("filename", "mode")

File Modes
Mode Description
"r" Read mode (file must exist)
"w" Write mode (overwrites file or creates new one)
"a" Append mode (adds data to end of file)
"x" Creates new file, error if file exists
"b" Binary mode (images, videos, pdf)
"t" Text mode (default)
"r+" Read + Write

M
"w+" Write + Read

O
.C
2. Reading Files LE
a) read() – Reads entire file
RA

Code:
file = open("[Link]", "r")
AI

content = [Link]()
HW

print(content)
[Link]()
Output (example):
S

Hello World!
YU

Welcome to Python.
PI

b) readline() – Reads one line


Code:
file = open("[Link]", "r")
line = [Link]()
print(line)
[Link]()
Output:
Hello World!

c) readlines() – Reads all lines into a list


Code:
lines = open("[Link]").readlines()
print(lines)
Output:
["Hello World!\n", "Welcome to Python."]
3. Writing to Files
a) write() – Writes text to file
Code:
file = open("[Link]", "w")
[Link]("Python File Writing")
[Link]()
Output:
(File Created with text "Python File Writing")

b) append data using ”a”


Code:
file = open("[Link]", "a")
[Link]("\nAppending new line")
[Link]()
Output (inside file):
Python File Writing
Appending new line

M
O
4. Using with Statement (Recommended)

.C
The with keyword automatically closes the file.
LE
Code:
RA

with open("[Link]", "r") as f:


print([Link]())
Output:
AI

(Contents of file printed)


HW

Advantages
• No need to call close()
S
YU

• Prevents file corruption


• Cleaner and safer code
PI

5. File Pointer Methods


Method Description Example Output
tell() Returns current cursor position. e.g., 0
seek(offset) Moves cursor to specified location. seek(0) resets pointer

Example: seek() and tell()


Code:
f = open("[Link]", "r")
print([Link]())
[Link](5)
print([Link]())
[Link](0)
print([Link]())
Output:
0
5
0

6. Checking if File Exists


Code:
import os
[Link]("[Link]")
Output:
True

7. Deleting Files
Code:
import os
[Link]("[Link]")
Output:

M
(File deleted successfully)

O
.C
8. Handling Exceptions in File Operations LE
Code:
RA

try:
file = open("[Link]", "r")
AI

print([Link]())
except FileNotFoundError:
HW

print("File not found")

Output:
S

"File not found"


YU
PI

17 Importing and Exporting Data (Python vs R)


Operation Python (Pandas) R
Read CSV df = [Link] csv("[Link]") df <- [Link]("[Link]")
Write CSV [Link] csv("[Link]", [Link](df, "[Link]",
index=False) [Link]=FALSE)
Read Excel df = [Link] excel("[Link]") df <- read excel("[Link]")
Write Excel [Link] excel("[Link]", write xlsx(df, "[Link]")
index=False)
Read JSON df = [Link] json("[Link]") df <- fromJSON("[Link]")
Write JSON [Link] json("[Link]") writeJSON(df, "[Link]")
Read Text df = [Link] csv("[Link]", df <- [Link]("[Link]",
delimiter="\t") sep="\t", header=TRUE)
Read SQL df = [Link] sql("SELECT * df <- dbGetQuery(con, "SELECT
FROM t", conn) * FROM t")
Write SQL [Link] sql("t", conn, dbWriteTable(con, "t", df,
if exists="replace") overwrite=TRUE)
Check File Exists [Link]("[Link]") [Link]("[Link]")
Delete File [Link]("[Link]") [Link]("[Link]")
Different Types of Charts and Graphs
With multiple types of data visualizations available, it is important to understand the purpose and best use of each
chart. This helps in selecting the right visualization for your dataset and improves the clarity of your data story.

M
O
.C
Figure 1: Source:[Link]
LE
RA
AI
S HW
YU
PI

Figure 2: Source:[Link]
These are classic visualizations suitable for most use cases and provide quick insights.

1. Bar Chart
A bar chart uses rectangular bars whose lengths represent values. Bars may be vertical or horizontal.
When to use: Comparing values across categories. Useful for distributions of categorical data.
Best practices:

• Clearly label bars and axes.


• Limit number of categories.
• Use colors purposefully to highlight insights.
2. Histogram

M
O
.C
LE
RA

Figure 3: Histogram
AI

A histogram visualizes the distribution of a continuous variable by grouping values into bins.
HW

When to use: Understanding spread, variation, and outliers.


Best practices:
• Choose appropriate number of bins.
S
YU

• Keep bin width consistent.


• Avoid histograms for very small datasets.
PI

3. Column Chart
A column chart is a vertical bar chart used to compare category values.
When to use: Comparing categories, ranking values.
Best practices:
• Avoid distracting 3D effects.
• Highlight key columns with contrasting colors.
M
O
Figure 4: Column Chart

.C
4. Line Chart
LE
A line chart connects data points with straight lines to show changes over an ordered axis.
When to use: Visualizing trends over time (growth, cycles).
RA

Best practices:
AI
S HW
YU
PI

Figure 5: Line Chart

• Ensure data is ordered logically.


• Add annotations for significant events.
• Use spacing or transparency for large datasets.
5. Pie Chart
A circular chart divided into slices representing proportions of a whole.
When to use: Showing part-to-whole relationships with limited categories (less than seven).
Best practices:
Figure 6: Pie Chart

• Limit slices for readability.


• Label slices clearly.
• Maintain consistent color usage.

M
6. KPI Chart

O
A minimal visual showing key performance metrics, often with a small trend indicator.

.C
When to use: Quick decision-making for metrics such as revenue, churn, or NPS.
Best practices: LE
RA
AI
S HW
YU
PI

Figure 7: KPI chart

• Use real-time capable dashboards.


• Focus on a few essential KPIs.
• Allow drill-down for exploring causes.
7. Donut Chart
A variant of the pie chart with a central hole, improving readability for multiple series.
When to use: Showing contributions of categories to a whole with clearer labeling space.
Best practices:
• Use clear labels and consistent colors.
• Avoid 3D effects or excessive decorations.
Figure 8: Donut Chart

M
O
Charts for Comparing Values

.C
8. Treemap
LE
A hierarchical chart using nested rectangles sized by value.
RA

When to use: Showing part-to-whole relationships with many categories.


Best practices:
AI

• Keep rectangle size proportional.


• Label rectangles concisely.
HW

• Use shade variations to encode secondary values.


S

9. Pareto Chart
YU

A bar chart sorted descending combined with a cumulative percentage line.


When to use: Identifying the most significant factors (80/20 rule).
Best practices:
PI

• Sort categories by impact.


• Show cumulative curve clearly.
10. Radar Chart
A radial chart for comparing multiple variables across categories.
When to use: Comparing multiple dimensions at once.
Best practices:
• Maintain consistent axis scaling.
• Use transparency for overlapping shapes.
11. Funnel Chart
Visualizes reduction across stages, widest at the top and narrowing downward.
When to use: Sales funnel, conversion stages, drop-off points.
Best practices:
• Include at least three stages.
• Use annotations to show major drop-offs.
12. Waterfall Chart
Shows how an initial value changes through sequential positive or negative values.
When to use: Explaining value buildup or breakdown (profit analysis).
Best practices:
• Color-code increases and decreases.
• Label intermediate steps clearly.
13. Stacked Bar Chart
Each bar represents a total, divided into colored segments.
When to use: Comparing composition of totals across categories.
Best practices:
• Limit number of segments.
• Use consistent colors across bars.

Charts for Relationships and Correlations

M
O
.C
LE
RA
AI
HW

14. Scatter Plot


Displays points on an X-Y plane to examine relationships.
S
YU
PI

When to use: Identifying trends, correlations, or clusters.


Best practices:
• Highlight outliers.
• Add trendlines to show direction.
• Use transparency for overlapping points.
15. Bubble Chart
A scatter plot where marker size encodes a third numeric variable.
When to use: Showing relationships between three variables.
Best practices:
• Scale bubble sizes proportionally.
• Add legend showing bubble size meaning.
16. Sankey Diagram
Flow chart showing movement between nodes using weighted arrows.
When to use: Illustrating flows between categories or stages.
Best practices:
• Ensure arrow widths match quantities.
• Highlight key flows with strong colors.

17. Area Chart


A filled line chart emphasizing magnitude of change over time.
When to use: Showing cumulative totals or comparisons over time.
Best practices:
• Use a zero baseline.

M
• Limit series to avoid clutter.

O
.C
18. Step Chart
A line chart variant showing discrete changes with horizontal and vertical segments.
LE
When to use: Displaying values that change at specific intervals.
Best practices:
RA

• Label events triggering the steps.


• Avoid too many steps in one chart.
AI
HW

Specialized and Financial Charts


S

19. Candlestick Chart


YU

Financial chart showing open, high, low, close for each period.
When to use: Analyzing price movements of stocks or currencies.
Best practices:
PI

• Use distinct colors for bullish and bearish candles.


• Avoid excessive overlays or indicators.
20. Sparkline
Small, minimal line chart without axes, used for quick trend insights.
When to use: Displaying compact trends alongside KPIs.
Best practices:
• Keep design minimal.
• Use color cues for direction.
Geographic Visualizations

21. Geo Chart


A map where regions are colored based on values.
When to use: Comparing metrics across geographic areas.
Best practices:
• Use consistent color scales.
• Avoid cluttering with excessive markers.
22. Scatter Map

M
Plots points on geographic coordinates.
When to use: Visualizing location-based events (stores, deliveries).

O
Best practices:

.C
• Use clustering in dense locations. LE
• Use color or size to encode value.
RA

23. Geographic Bubble Chart


Bubble map where bubble size represents numeric values at locations.
When to use: Comparing values across locations.
AI

Best practices:
HW

• Size bubbles proportionally.


• Include tooltips or labels for clarity.
S
YU

24. Heatmap
Grid or map where color intensity encodes values.
When to use: Identifying patterns across matrices or geographic regions.
PI

Best practices:
• Use intuitive color scales.
• Avoid overcrowding.

Other Notable Charts


25. Box Plot (Whisker Plot) A box plot uses boxes and whiskers to summarize the distribution of values
within measured groups. The positions of the box and whisker ends show the regions where the majority of
the data lies. We most commonly see box plots when we have multiple groups to compare to one another;
other charts with more detail are preferred when we have only one group to plot.
26. Pictograph
Uses icons or images to represent quantities; best for storytelling.
27. Gantt Chart
Timeline chart showing tasks, durations, and dependencies.
28. Dot Plot
Uses dots to represent values or frequencies, reducing clutter.
M
O
General Best Practices for Effective Visualization

.C
• Label axes, legends, and annotations clearly.
LE
• Avoid unnecessary visual decorations or 3D effects.
RA

• Use consistent scales and baselines.


AI

• Use colors intentionally to highlight insights.


HW

• Provide context such as units and data sources.


S
YU
PI
Charts and Graphs in Python and R
Charts and graphs help visualize data patterns, trends, and distributions. Python uses libraries like Matplotlib
and Seaborn, while R uses Base Graphics and ggplot2.

18 Charts and Graphs in Python (Matplotlib / Seaborn)


1.1 Line Chart
Purpose: Show trends over time.
Code (Matplotlib):
import [Link] as plt
x = [1,2,3,4]
y = [10,20,15,25]
[Link](x, y)
[Link]("Time")
[Link]("Sales")
[Link]("Sales Trend")
[Link]()
Description of Output: A line going through points (1,10), (2,20), (3,15), (4,25).

M
O
.C
1.2 Bar Chart
Code:
LE
cities = ["Mumbai","Delhi","Pune"]
RA

values = [25,30,18]
[Link](cities, values)
[Link]("City Counts")
AI

[Link]()
Output: Vertical bars for Mumbai(25), Delhi(30), Pune(18).
S HW

1.3 Pie Chart


YU

Code:
sizes = [40,30,20,10]
PI

labels = ["A","B","C","D"]
[Link](sizes, labels=labels, autopct="%1.1f%")
[Link]()
Output: A circular pie chart with percentage labels.

1.4 Histogram
Purpose: Visualize distribution of data.
Code:
data = [10,20,20,30,40,40,40,50]
[Link](data, bins=5)
[Link]("Value")
[Link]("Frequency")
[Link]()
Output: Bars showing frequency distribution of the data.
1.5 Scatter Plot
Code:
x = [1,2,3,4,5]
y = [5,4,6,5,7]
[Link](x,y)
[Link]("X")
[Link]("Y")
[Link]()
Output: Dots plotted showing correlation between X and Y.

M
O
.C
LE
RA
AI
S HW
YU
PI
19 Charts and Graphs in R
R provides powerful visualization through Base Plotting and ggplot2.

2.1 Line Chart (Base R)


Code:
x <- c(1,2,3,4)
y <- c(10,20,15,25)
plot(x, y, type="l", col="blue", main="Sales Trend")
Output: A blue line connecting the data points.

2.2 Bar Chart (Base R)


Code:
values <- c(25,30,18)
names <- c("Mumbai","Delhi","Pune")
barplot(values, [Link]=names, col="green")
Output: Green bars representing the values.

M
O
2.3 Pie Chart (Base R)

.C
Code: LE
sizes <- c(40,30,20,10)
labels <- c("A","B","C","D")
RA

pie(sizes, labels)
Output: A pie chart with 4 slices labeled A, B, C, D.
AI
HW

2.4 ggplot2 Visualizations (Advanced R)


S

Load library: library(ggplot2)


YU

Line Plot
PI

Code:
df <- [Link](x=c(1,2,3,4), y=c(10,20,15,25))
ggplot(df, aes(x,y)) + geom line(color="blue")
Output: A smooth blue line chart.

Bar Plot
Code:
df <- [Link](city=c("Mumbai","Delhi","Pune"), val=c(25,30,18))
ggplot(df, aes(city, val)) + geom bar(stat="identity", fill="orange")
Output: Orange bars for the values.

Histogram
Code:
ggplot(df, aes(values)) + geom histogram(bins=5, fill="purple")
Scatter Plot
Code:
df <- [Link](x=c(1,2,3,4), y=c(5,4,6,5))
ggplot(df, aes(x,y)) + geom point(color="red")
Output: Red dots showing the scatter pattern.

Summary of Charts
Chart Use Case Python / R Function
Line Chart Trends over time plot(), geom line()
Bar Chart Category comparison bar(), geom bar()
Pie Chart Part-to-whole pie(), [Link]()
Histogram Distribution hist(), geom histogram()
Scatter Plot Correlation scatter(), geom point()

M
O
.C
LE
RA
AI
S HW
YU
PI
20 Matplotlib Functions
Function Description
1. Basic Plotting
plot() Draws line plot.
scatter() Creates scatter plot.
bar() Vertical bar chart.
barh() Horizontal bar chart.
pie() Pie chart.
hist() Histogram.
boxplot() Box-and-whisker plot.
stem() Stem plot.
stackplot() Stacked area plot.
fill between() Fill region between curves.
2. Labels, Titles, Legends
title() Set plot title.
xlabel() Set x-axis label.
ylabel() Set y-axis label.
legend() Show legend.
text() Add text inside plot.

M
annotate() Add annotations.
Toggle grid lines.

O
grid()
3. Figure and Subplots

.C
figure() Create a figure object.
LE
subplot() Create subplots in a grid.
subplots() Create figure + axes.
RA

add subplot() Add subplot to figure.


tight layout() Adjust spacing.
savefig() Save figure to a file.
AI

Function Description
HW

4. Axis & Tick Control


xticks() Customize x-axis ticks.
yticks() Customize y-axis ticks.
S

xlim() Set x-axis limits.


YU

ylim() Set y-axis limits.


axis() Control axis behavior.
5. Styling Functions
PI

[Link]() Apply predefined style.


rcParams Modify default style settings.
colormap (cmap) Set colormap.
6. Image Functions
imshow() Display an image.
imsave() Save image to file.
colorbar() Add color legend.
7. 3D & Advanced Plots
plot surface() 3D surface plot.
plot wireframe() 3D wireframe plot.
contour() 2D contour lines.
contourf() Filled contour plot.
hexbin() Hexagonal binning plot.
21 Seaborn Functions (Complete List)
Function Description
1. Categorical Plots
catplot() High-level interface for categorical plots
(kind=”bar”,”box”,”violin”...).
barplot() Shows mean of a numerical variable with confidence intervals.
countplot() Displays count of each category.
boxplot() Draws a box-and-whisker plot for distributions.
violinplot() Shows distribution + kernel density of data.
stripplot() Scatter plot for categorical data (points).
swarmplot() Non-overlapping categorical scatter plot.
pointplot() Point plot with estimates and confidence intervals.
2. Distribution Plots
distplot() (Deprecated) Combined histogram + KDE.
displot() High-level distribution plot (hist, kde, ecdf, rug).
histplot() Histogram of numerical data.
kdeplot() Kernel Density Estimation curve.
ecdfplot() Empirical cumulative distribution function.
rugplot() Small dashes along axis showing distribution.

M
jointplot() Joint distribution + marginal distributions.

O
pairplot() Pairwise relationships between all numerical variables.

.C
3. Relational Plots
relplot() High-level interface for relational plots (scatter, line).
LE
scatterplot() Scatter plot of numeric variables.
lineplot() Line plot with aggregation over observations.
RA

4. Regression / Statistical Plots


lmplot() Linear regression with multiple facets.
AI

regplot() Scatter + linear regression fit.


residplot() Residuals of a regression model.
HW

5. Matrix / Heatmap Plots


heatmap() Heatmap of matrix-like data.
Hierarchical clustering + heatmap.
S

clustermap()
YU

6. Multi-Plot Grids
FacetGrid() Multi-plot grid for categorical values.
PairGrid() Grids of pairwise plots for variables.
PI

JointGrid() Grid layout for joint and marginal plots.


7. Seaborn Styling Functions
set style() Set background style (white, dark, ticks).
set context() Set context (paper, talk, poster).
set palette() Set color palette for plots.
color palette() Return a list of colors from palette.
despine() Remove top/right spines from plot.
22 Seaborn vs Matplotlib (Comparison Table)
Matplotlib Seaborn
Low-level plotting library; re- High-level library built on top of Matplotlib; provides
quires more manual customiza- cleaner and attractive plots.
tion.
Default plots are basic and plain. Comes with attractive themes and color palettes by default.
User must set labels, titles, and Automatically handles labels, style, legends, and aesthetics.
legends manually.
Ideal for full control of every plot Ideal for quick statistical and analytical visualizations.
element.
Syntax can be longer for complex Shorter and cleaner syntax for relational, categorical, and
plots. distribution plots.
Mostly used for: line, scatter, Mostly used for: pairplot, jointplot, heatmap, violinplot,
bar, pie, hist, images, 3D plots. swarmplot, kdeplot.
Does not handle DataFrames di- Works seamlessly with Pandas DataFrames and column
rectly (better with arrays). names.
Manual color selection needed for Automatic color palette selection (deep, pastel, coolwarm).
multi-series plots.
Subplots created using Grid layouts available using FacetGrid, PairGrid,

M
subplots(), figure(), JointGrid.

O
add subplot().
Better for detailed, production- Better for exploratory data analysis (EDA).

.C
ready customization. LE
Performance is very fast because Slightly slower but provides better statistical visualizations.
it’s low-level.
RA

Supports 3D plots. No native 3D plot support.


AI
S HW
YU
PI
23 NumPy: Numerical Python
NumPy (Numerical Python) is a core Python library used for scientific computing. It provides the powerful
ndarray object for storing and manipulating large, multi–dimensional numerical data efficiently.
NumPy is highly optimized (C backend), making array operations much faster than regular Python lists.

1. Creating NumPy Arrays


1.1 From Python List
import numpy as np
a = [Link]([1,2,3,4])

Output:
array([1, 2, 3, 4])

1.2 Multi–dimensional Array


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

1.3 Array Creation Functions

OM
Function Description
[Link]((m,n)) Matrix filled with 0s.
.C
LE
[Link]((m,n)) Matrix filled with 1s.
[Link]((m,n), value) Matrix filled with a constant.
RA

[Link](start, stop, Creates array like Python range.


step)
I

[Link](a,b,n) n numbers between a and b.


WA

[Link](n) Identity matrix.


[Link](m,n) Uniform random numbers.
SH

[Link](m,n) Standard normal distribution.


YU

2. Array Attributes
PI

Attribute Description
[Link] Number of dimensions.
[Link] Tuple of array dimensions.
[Link] Total number of elements.
[Link] Data type of array elements.
[Link] Size of each element (bytes).
Example:
a = [Link]([[1,2,3],[4,5,6]])
[Link] ⇒ (2, 3)

3. Indexing & Slicing


NumPy slicing is similar to Python slicing but more powerful.

3.1 Basic Slicing


a = [Link]([10,20,30,40,50])
a[1:4] ⇒ [20, 30, 40]
3.2 2D Slicing
b = [Link]([[1,2,3],[4,5,6],[7,8,9]])
b[0:2, 1:3] ⇒ [[2, 3], [5, 6]]

3.3 Boolean Indexing


a[a > 15] ⇒ elementsgreaterthan15

4. Mathematical Operations
NumPy performs vectorized operations (fast and automatic).
Operation Example
Element-wise add a + b
Element-wise multiply a * b
Dot product [Link](a, b)
Power a ** 2
Log [Link](a)
Sine, Cosine [Link](a), [Link](a)
Example:
a = [Link]([1,2,3])
a * 2 ⇒ [2, 4, 6]

OM
.C
5. Aggregate Functions LE
a = [Link]([1,2,3,4])
Function Output
RA

[Link](a) 10
I

[Link](a) 1
WA

[Link](a) 4
[Link](a) 2.5
SH

[Link](a) 2.5
[Link](a) Standard deviation
YU

[Link](a) Variance
PI

6. Reshaping Arrays
6.1 Reshape
a = [Link](6)
[Link](2,3) ⇒ 2x3matrix

6.2 Flatten
[Link]() ⇒ 1Darray

6.3 Transpose
b.T ⇒ transposeof b
7. Stacking Arrays
Function Description
[Link]((a,b)) Horizontal stacking.
[Link]((a,b)) Vertical stacking.
[Link]((a,b), General concatenation.
axis=0)

8. Copies vs Views
• View shares data with original array.
• Copy creates new data.

Example:
a = [Link]([1,2,3])
b = [Link]()
c = [Link]()
Modifying b affects a, but modifying c does not.

9. NumPy Random Module

OM
.C
Function Usage
[Link](m,n) Uniform distribution.
LE
[Link](m,n) Normal distribution.
[Link](a,b,n) Random integers.
RA

[Link]() Random sampling.


[Link]() Set random seed.
I

Example:
WA

[Link](1,10,5) ⇒ [3, 7, 2, 9, 1]
SH

10. File Handling in NumPy


YU
PI

Function Description
[Link]("[Link]", a) Save array in binary format.
[Link]("[Link]") Load array.
[Link]("[Link]", a) Save as text.
[Link]("[Link]") Load text file.

11. Broadcasting
Broadcasting allows operations between arrays of different shapes.
Example:
 
a = 1 2 3
b = 5
a + b ⇒ [6, 7, 8]
Rules:

• Trailing dimensions are compared.


• Dimensions must match or be 1.
• Arrays expand temporarily without copying memory.

You might also like