Python Workshop
Python Workshop
🏷️
Python is an interpreter language.
🏷️
Python is a high-level, general-purpose programming language.
Its design philosophy emphasizes code readability with the use of significant
🏷️
indentation.
🏷️
Python is dynamically typed and garbage-collected.
It supports multiple programming paradigms, including structured, object-oriented
and functional programming.
compiler vs interpreter
compiler interpreter
1st compile fully then execute the code line by line compile and execute.
10.Special Constants
Constan Value
t
math.pi 3.1415926535897
93
math.e 2.7182818284590
45
math.tau 6.2831853071795
86
Example:
a = (1, 2, 3) # Tuple
b = [1, 2, 3] # List
c = {1, 2, 3} # Set
d = {"name": "Alice", "age": 25} # Dictionary
print(a, b, c, d)
Example:
weird_dict = {True: "Yes", 1: "One", 1.0: "Float One", False: "No", 0: "Zero"}
print(weird_dict)
# Output: {True: 'Float One', False: 'Zero'}
# Why? Because True, 1, and 1.0 are considered the same key in a dictionary!
Example:
lst = [10, 20, 30, 40]
print(lst[2]) # 30
print(lst[1:3]) # [20, 30]
Example:
def fun():
return (lambda x: x * 2)
double = fun()
print(double(5)) # 10
Example:
import asyncio
async def my_coroutine():
print("Start")
await asyncio.sleep(1)
print("End")
asyncio.run(my_coroutine())
Example:
import asyncio
async def infinite():
while True:
print("Still running...")
await asyncio.sleep(2)
asyncio.run(infinite())
4. Exponentiation (**)
Example:
print(2 ** 3) # 8
Example:
print(2 ** 3 ** 2) # 512
# Because it evaluates as 2 ** (3 ** 2) = 2 ** 9 = 512!
Example:
print(~0) # -1
print(~-1) # 0
# Why? Because ~x is equivalent to -(x+1)!
Example:
print(3 * "Ha!") # Ha!Ha!Ha!
print("Python" * 2) # PythonPython
Example:
print("5" + "3") # 53 (String Concatenation!)
print([1, 2] + [3, 4]) # [1, 2, 3, 4] (List Concatenation!)
Example:
print(1024 >> 10) # 1 (Dividing by 1024 using bit shifting!)
9. Bitwise Operators (&, ^, |)
Example:
print(5 & 3) # 1 (Bitwise AND)
print(5 | 3) # 7 (Bitwise OR)
print(5 ^ 3) # 6 (Bitwise XOR)
Example:
print(bin(5 & 3), bin(5 | 3), bin(5 ^ 3))
# 0b1 0b111 0b110 (Binary Representation!)
10. Comparisons and Membership Tests (in, not in, is, is not, ==, !=, >, <, etc.)
Example:
print(5 > 3) # True
print("a" in "apple") # True
Example:
x = [1, 2, 3]
y=x
print(x is y) # True (Same reference)
print(x == y) # True (Same value)
Example:
print([] or "Python") # Python (Because empty list is False!)
print([] and "Python") # [] (Because False and anything is False)
Example:
print("Python"[::2] if len("Python") > 5 else "Short!")
# Output: Pto (Skipping every 2nd character!)
Example:
print((lambda x, y: x + y)("Hello, ", "World!"))
# Hello, World!
Example:
n=5
print((m := n * 2) + m)
# Output: 20 (Assigns and uses `m` in the same expression!)
Input
Formats that can be asked in problem solving
basic single values
input: 1
code: int(input())
input: 12.12
code: float(input())
example 1: 1 2 3 4 5
example 2: 12 212 23
example 3: 1.23 2.12 43.3 43.3
example 4: hari jai krish mani
# example 1:
# input: 1 2 3 4 5
li = list(map(int,input().split()))
# example 2:
# input: 12 212 23
li = list(map(int,input().split()))
# example 3:
# input: 1.23 2.12 43.3 43.3
li = list(map(float,input().split()))
# example 4:
# input: hari jai krish mani
li = input().split()
# input: 12 23 34 53 53
# code example 1
li = list(map(int,input().split()))
# code example 2
li = []
temp = input().split()
for item in temp:
li.append(int(item))
print(li) # [12, 23, 34, 53, 53]
# code example 3
li = [int(x) for x in input().split()]
print(li) # [12, 23, 34, 53, 53]
input:
5
11 2 23 4 56 6
# code example
n = int(input())
li = list(map(int,input().split()))
print(n) # 5
print(li) # [11, 2, 23, 4, 56, 6]
input:
34
11 22 33 44
10 20 30 40
17 17 17 17
# code example
temp = list(map(int,input().split()))
rc = temp[0]
cc = temp[1]
matrix = []
for i in range(rc):
row = list(map(int,input().split()))
matrix.append(row)
example 1
li = ['samsung', 'iphone','nokia','oppo','vivo']
print(*li,sep=" and ")
# samsung and iphone and nokia and oppo and vivo
example 2
li = [1,2,3,4]
n = len(li)
for i in range(n):
print(li[i],end=",")
# 1,2,3,4,
Data types
Data types
/ \
Numeric string datatype
/ \
int str
float
bool
complex
int
+ve, =ve, 0 are called int
size/range is dependent on machine
# some times you want to store money information in code
amount = 1,00,000 # 1 lakh
print(amount) # (1,0,0)
>>> 00001
# above code is invalid,
# because, in int, prefix 0 is already used for number representation.
# we can't use like above code. it will through error.
REPL
REPL stands for Read-Eval-Print Loop. It is an interactive programming environment
that takes user inputs (one at a time), evaluates them, and returns the result to the
user. This process is repeated in a loop, hence the name "Read-Eval-Print Loop."
Equality == !=
Relational > < >= <=
Membership in not in
Identity is is not
always above operators will return result in True/False
== Equal To
!= Not Equal To
> Greater Than
< Less Than
>= Greater Than or Equal To
<= Lesser Than or Equal To
Lexicographical comparison
# this means, you can compare strings in python
# How this is working ?
# - It will compare each characters ASCII value
print( 'abc' == 'abc' ) # True
print( 'abc' == 'bbc' ) # False
print( 'abc' <= 'bbc' ) # True
Mutable Objects are of type Python list, Python dict, or Python set. Custom classes
are generally mutable.
if we are trying to change, then a new object will be created with that contents.
note:
creating n no of objects with the same content is a stupid activity. hence to save
memory, we create only one object and all references all pointing to same object.
if one reference is trying to change the content.in the object, other objects will have
impact.
To overcome this problem, content in the object should be modified. This concept is
called immutable.
If any person is trying to change the content in the immutable object, then a new
object is created with new content.
>>> a = "hi"
>>> a*2
hihi
>>> a = "hi"
>>> a * 2 * 2
"hihi"*2
"hihihihi"
>>> 2 * a * 2
"hihihihi"
>>> 2 * 2 * a
"hihihihi"
# edge cases
>>> a = 'hello'
>>> a * -1
''
>>> a * 0
''
>>> a * True
'hello'
>>> a * False
''
>>> a * 3.5
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: can't multiply sequence by non-int of type 'float'
logical operators
and
or
not
and
arg1 or arg2
* arg1 is non-zero then result is arg1
* arg1 is 0 then result is arg2
>>> 10 or 5
10
>>> 7 or 0
7
>>> 6.5 or -5
6.5
>>> False or 100
100
>>> False or False
False
>>> 0 or 2.5
2.5
In case of and 1st is 0 don't go next.
In case of or 1st is non-zero then don't go for next.
not
# in below code,
# == has high priority
# but 10==not is invalid.
>>> a = 10 == not 100
File "<stdin>", line 1
a = 10 == not 100
^^^
SyntaxError: invalid syntax
# code 01
print(10)
if (3>2):
print(20)
print(30) # this will not comes under either main/if block.
print(40)
# code 02
print(10)
if (3>2):
print(20)
print(30) # this will not comes under either main/if block.
print(40)
# code 03
print(10)
if (3>2): # expected an indented block after 'if' statement
print(20)
print(30)
print(40)
some if code scenario's
# code 01
print(10)
if (3>2):
pass # this is used, when you don't want to write a code now.
# but you will write in future.
# pass will do nothing.
print(20)
print(30)
print(40)
# code 02
print(10)
if (): # empty brackets are always valid,
# is consider False,
# Because it means, empty tuple.
print(20)
print(30)
# code 03
print(10)
if :# End up with syntax error.
# Because, bracket is mandatory
print(20)
print(30)
else
# WAP to find max of 2 numbers
a = int(input())
b = int(input())
if a > b:
res = a
else:
res = b
print(res)
# alternate solution
a = int(input())
b = int(input())
res = (a>b)*a + (b>a)*b
print(res)
# some valid indentation codes.
# code 01
print(10)
if (3>2):
print(20) # this will comes under if block
print(25)
else:
print(26)
print(30) # this will not comes under either else block.
print(40)
col = 5
row = 5
c=1
while col <= 5: # outer while
r=1
while r <= r-c: # inner while
print(b, end=" ")
b=b+1
print()
a=a+1
for
In Python, a for loop allows you to repeat a block of code a specific number of times.
When using range(), you can control how many times the loop will run.
Syntax
for variable in range(start, stop, step):
# code to repeat
start (optional): The number where the loop starts (default is 0).
stop (required): The number where the loop stops. This is not included in the loop.
step (optional): The amount the loop counter increases by after each iteration
(default is 1).
Example 1: Basic Loop with range(stop)
for i in range(5):
print(i)
Explanation:
Output:
0
1
2
3
4
Example 2: Loop with range(start, stop)
for i in range(2, 6):
print(i)
Explanation:
The loop starts at 2 and ends at 5 (because 6 is not included).
Output:
2
3
4
5
Example 3: Loop with range(start, stop, step)
for i in range(0, 10, 2):
print(i)
Explanation:
The loop starts at 0, ends at 8 (since 10 is not included), and increments by 2 each
time.
Output:
0
2
4
6
8
Example 4: Loop in Reverse with range()
for i in range(5, 0, -1):
print(i)
Explanation:
Output:
5
4
3
2
1
break
break keyword will only work inside
the for, while ( loops )
# some other examples
a=1
while a <=10:
print(a)
if a == 4:
break
a=a+1
# example - 02
a=1
while a <=10:
print(a)
a=a+1
else:
print("loop completed")
# find prime no
n = int(input())
i=2
isPrime = True
while i<(n**0.5):
if n%i == 0:
isPrime = False
break
i=i+1
if isPrime:
print("is Prime")
else:
print("is not a Prime")
# find prime no
n = int(input())
i=2
while i<(n**0.5):
if n%i == 0:
print("Not a prime")
break
i=i+1
else:
print("is a Prime")
0
1
2
4
5
6
7
pass
In Python, the `pass` statement is a placeholder that does nothing. It’s used when a
statement is syntactically required but you don’t want to execute any code. Typically,
you use `pass` in situations where you plan to add code later or you want to skip a
block without causing errors.
Common Uses of pass
Empty Function or Class: You can use pass when you want to define a function or
class but aren’t ready to implement the logic yet.
Placeholders in Conditional Statements: Sometimes in if, for, or while statements,
you may want to skip certain conditions temporarily.
Example 1: pass in Conditional Statements
for i in range(5):
if i == 3:
pass # Do nothing when i is 3
else:
print(i)
Output:
0
1
2
4
In this example, when i is 3, the pass statement is executed, meaning the loop does
nothing for that iteration, but continues with the next.
conditional operator
is a concept used, literally a short form of if else
Technically we can call it as "conditional expression"
But it act as an expression
a = int(input())
b = int(input())
res = a if a > b else b
print(res)
# syntax
# a if a>b else b
# [True res] [if keyword] [ condition] [else keyword] [False res]
# to put in simple terms.
# res1 if exp else res2
🔹
1️⃣ Magic Auto-Filled Excel (Pandas + ExcelWriter)
🔹
Concept: Automatically generate and save an Excel sheet with random data!
Magic Trick: Use Pandas to create and fill Excel sheets instantly.
Code:
import pandas as pd
import numpy as np
df = pd.DataFrame(data)
# Save to Excel
df.to_excel("magic_data.xlsx", index=False)
print("Excel file 'magic_data.xlsx' created successfully!")
🔹
2️⃣ Predict Future Sales (Pandas + Excel)
🔹
Concept: Predict future sales using moving averages in Excel!
Magic Trick: Automate calculations and save results to Excel.
Code:
import pandas as pd
import numpy as np
# Save to Excel
df.to_excel("sales_forecast.xlsx", index=False)
print("Excel file 'sales_forecast.xlsx' created with predictions!")
🔹
3️⃣ Excel Password Cracker (SHA-256 Decryption)
🔹
Concept: Decode a hashed password from Excel!
Magic Trick: Hashes passwords and verifies matches automatically.
Code:
import pandas as pd
import hashlib
# Hash function
def hash_password(password):
return hashlib.sha256(password.encode()).hexdigest()
# Check passwords
df["Hashed_Password"] = df["Password"].apply(hash_password)
🔹
4️⃣ Excel-Based Lottery Number Picker
🔹
Concept: Generate random lottery numbers and save them to Excel!
Magic Trick: NumPy picks lucky numbers for you.
Code:
import pandas as pd
import numpy as np
df = pd.DataFrame(lottery_data)
# Save to Excel
df.to_excel("lottery_numbers.xlsx", index=False)
print("Lucky numbers saved in 'lottery_numbers.xlsx'!")
🔹
5️⃣ Auto-Clean Messy Excel Data
🔹
Concept: Remove duplicates, missing values, and outliers from Excel data.
Magic Trick: Pandas auto-cleans your spreadsheet in seconds!
Code:
import pandas as pd
# Remove duplicates
df_cleaned = df_cleaned.drop_duplicates()
🔹
6️⃣ Create an Animated Excel Graph
🔹
Concept: Make a real-time animated graph from Excel data.
Magic Trick: Update and refresh charts automatically.
Code:
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
# Create plot
fig, ax = plt.subplots()
ax.set_xlabel("Date")
ax.set_ylabel("Sales")
def update(i):
ax.clear()
ax.plot(df["Date"][:i], df["Sales"][:i], marker="o", linestyle="-")
ax.set_title(f"Sales Over Time (Frame {i})")
🔹
Concept: Hide and reveal secret messages inside Excel.
Magic Trick: Convert text to ASCII numbers and back!
Code:
import pandas as pd
# Save results
df.to_excel("decoded_messages.xlsx", index=False)
print("Secret messages encoded & decoded!")
🌍
🔹
8️⃣ Auto-Translate Excel Data
🔹
Concept: Translate entire Excel columns to another language!
Magic Trick: Use Google Translate inside Excel.
Code:
import pandas as pd
from deep_translator import GoogleTranslator
# Translate text
df["Translated"] = df["Text"].apply(lambda x: GoogleTranslator(source='auto',
target='fr').translate(x))
# Save to Excel
🌍
df.to_excel("translated.xlsx", index=False)
print("Excel data translated successfully! ")
🔹
1️⃣ Magic Excel Data Filler (NumPy)
🔹
Concept: Automatically fill Excel with random structured data.
NumPy Trick: Generate random numbers and choices instantly!
Code:
import numpy as np
import pandas as pd
df = pd.DataFrame(data)
# Save to Excel
df.to_excel("magic_filled_data.xlsx", index=False)
print("Excel file 'magic_filled_data.xlsx' created with random data! ")
🔹
2️⃣ Auto-Detect Outliers in Excel (NumPy)
Concept: Find suspicious values in Excel using NumPy’s mean & standard
🔹
deviation.
NumPy Trick: Auto-detects outliers in a column.
Code:
import numpy as np
import pandas as pd
🔹
3️⃣ Excel-Based Lucky Number Generator (NumPy)
🔹
Concept: Generate random lottery numbers in an Excel file.
NumPy Trick: No loops needed!
Code:
import numpy as np
import pandas as pd
# Save to Excel
df.to_excel("lottery_numbers.xlsx", index=False)
print("Lucky numbers saved in 'lottery_numbers.xlsx'! ")
🔹
4️⃣ Excel-Based Realistic Date Generator (NumPy)
🔹
Concept: Create random but realistic dates in Excel.
NumPy Trick: Use np.datetime64 for instant date calculations.
Code:
import numpy as np
import pandas as pd
# Save to Excel
df.to_excel("random_dates.xlsx", index=False)
print("Random dates saved in 'random_dates.xlsx'! ")
🔹
5️⃣ Auto-Categorize Excel Data (NumPy)
🔹
Concept: Categorize Excel data automatically based on conditions.
NumPy Trick: Use vectorized operations for speed!
Code:
import numpy as np
import pandas as pd
🏷️")
df.to_excel("categorized_employees.xlsx", index=False)
print("Employees categorized in 'categorized_employees.xlsx'!
🔹
6️⃣ Auto-Generate Fake People for Excel (NumPy)
🔹
Concept: Fill Excel with realistic fake names, emails, and salaries.
NumPy Trick: Instant data generation!
Code:
import numpy as np
import pandas as pd
df = pd.DataFrame({
"Name": np.random.choice(names, num_rows),
"Email": [f"{name.lower()}@{np.random.choice(domains)}" for name in
np.random.choice(names, num_rows)],
"Salary ($)": np.random.randint(35000, 100000, num_rows)
})
# Save to Excel
👥")
df.to_excel("fake_people.xlsx", index=False)
print("Fake people saved in 'fake_people.xlsx'!
🏆
🔹
7️⃣ Automatic Excel Performance Ranker (NumPy)
🔹
Concept: Rank employees based on scores instantly in Excel.
NumPy Trick: Use argsort() for super-fast ranking!
Code:
import numpy as np
import pandas as pd
# Save to Excel
🏆")
df.to_excel("ranked_employees.xlsx", index=False)
✅ 🏅
print("Employees ranked in 'ranked_employees.xlsx'!
Magic: Instantly sort & rank thousands of rows!
🔹
8️⃣ Magic Image Generator in Excel (NumPy)
🔹
Concept: Generate random pixelated images and save the color values to Excel.
NumPy Trick: Use 3D arrays to store RGB values!
Code:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
# Convert to DataFrame
df = pd.DataFrame([["({}, {}, {})".format(*image[i, j]) for j in range(10)] for i in
range(10)])
# Save pixel values to Excel
df.to_excel("random_image.xlsx", index=False, header=False)
NumPy
It provides support for large multi-dimensional arrays and matrices, along with a large
collection of high-level mathematical functions to operate on these arrays.
1. Powerful N-Dimensional Array Object: NumPy provides the ndarray object,
which is a fast, memory-efficient array of fixed-size items.
2. Broadcasting: It allows for operations on arrays of different shapes, making it
possible to perform element-wise operations even on arrays of differing
dimensions.
3. Mathematical Functions: NumPy contains a wide range of mathematical
operations such as linear algebra, Fourier transforms, random sampling, and
more.
4. Memory Efficiency: NumPy arrays consume less memory compared to Python
lists.
5. Integration with Other Libraries: NumPy seamlessly integrates with libraries
like Pandas, SciPy, and Matplotlib.
The primary object in NumPy is the ndarray (N-dimensional array), which is a grid of
values, all of the same type. An ndarray can be of any dimension.
import numpy as np
print(arr)
Output:
[1 2 3 4]
python
print(arr_2d)
Output:
[[1 2]
[3 4]
[5 6]]
python
print(arr.shape) # (4,)
print(arr_2d.shape) # (3, 2)
print(arr.ndim) # 1
print(arr_2d.ndim) # 2
Data Type of Array
print(arr.dtype) # int64
print(zeros_arr)
●
print(ones_arr)
●
print(eye_arr)
●
print(range_arr)
●
●
print(rand_arr)
●
NumPy arrays can be indexed and sliced just like Python lists, but with additional
capabilities like multi-dimensional slicing.
1D Array Indexing
2D Array Indexing
Slicing Arrays
# Slicing 1D array
# Slicing 2D array
You can use Boolean indexing or fancy indexing (using an array of indices).
# Boolean indexing
# Fancy indexing
indices = [0, 3, 4]
4. Array Operations
Mathematical Operations
# Element-wise addition
# Element-wise subtraction
# Element-wise multiplication
NumPy provides ufuncs (universal functions), which are vectorized functions that
operate on arrays element-wise.
# Square root
Aggregating Operations
# Sum of elements
print(np.sum(arr)) # Output: 15
# Mean of elements
# Standard deviation
print(np.min(arr)) # Output: 1
print(np.max(arr)) # Output: 5
5. Reshaping Arrays
You can reshape arrays without changing their data using reshape(), flatten(),
and similar methods.
Reshape
reshaped_arr = arr.reshape(2, 3)
print(reshaped_arr)
Output:
[[1 2 3]
[4 5 6]]
Flatten
flattened = arr_2d.flatten()
print(flattened)
Output:
[1 2 3 4]
Transpose
You can transpose an array (swap rows and columns) using transpose() or .T.
transposed = arr_2d.T
print(transposed)
Output:
[[1 3]
[2 4]]
6. Random Sampling
NumPy has a comprehensive suite of functions for random number generation, from
uniform distribution to normal distribution.
rand = np.random.random(5)
print(rand)
Random Integers
Normal Distribution
rand_norm = np.random.randn(5)
print(rand_norm)
NumPy has a set of linear algebra functions that are essential for scientific
computing.
Dot Product
print(dot_product) # Output: 11
Matrix Multiplication
Output:
[[19 22]
[43 50]]
Inverse of a Matrix
# Inverse of a matrix
inv_arr = np.linalg.inv(arr)
print(inv_arr)
Output:
[[-2. 1. ]
[ 1.5 -0.5]]
In this guide, we'll cover the core features of Pandas in detail, including its data structures,
operations, and key methods.
● Data Structures: Pandas provides two powerful data structures: Series and
DataFrame, which are designed to handle a variety of data formats and types.
● Handling Missing Data: Pandas has built-in support for handling missing data, such
as NaN values, with options for imputation or deletion.
● Label-based Indexing: Unlike traditional arrays, Pandas allows you to use labels
(like strings) to index and access data, which enhances flexibility and clarity.
● Data Alignment: It automatically aligns data when performing operations on multiple
datasets.
● Time Series Support: Pandas offers comprehensive tools for working with time
series data, such as date parsing, resampling, and shifting.
● Flexible I/O: Pandas can read from and write to various data formats, such as CSV,
Excel, HDF5, SQL, and more.
a. Series
A Series is a one-dimensional labeled array capable of holding any data type. It is similar to
a Python list, but with additional features like the ability to label its elements (using indices).
Creating a Series:
import pandas as pd
Output:
0 10
1 20
2 30
3 40
dtype: int64
By default, the index is the sequence of integers starting from 0. You can also assign custom
indices:
Output:
a 10
b 20
c 30
d 40
dtype: int64
b. DataFrame
Creating a DataFrame:
df = pd.DataFrame(data)
print(df)
Output:
● Columns: Name, Age, and City are the columns of the DataFrame.
● Index: By default, the index is a sequence of integers (0, 1, 2, 3, etc.).
Pandas offers powerful ways to index and select data in a Series or DataFrame.
a. Accessing Columns
You can access a column by using its name as a key (just like dictionary access):
Output:
0 Alice
1 Bob
2 Charlie
3 David
Name: Name, dtype: object
b. Accessing Rows
You can access rows using the iloc[] method (integer location-based indexing) or the
loc[] method (label-based indexing).
# Accessing the first row using iloc
print(df.iloc[0])
vbnet
Copy code
Name Alice
Age 24
City New York
Name: 0, dtype: object
c. Slicing DataFrames
Output:
Name Age
1 Bob 27
2 Charlie 22
d. Conditional Selection
You can use conditional expressions to filter data based on specific conditions.
Output:
You can drop rows or columns containing missing values using dropna().
Pandas supports a wide range of operations for transforming and manipulating data.
a. Adding Columns
Output:
Name Age City Country
0 Alice 24 New York USA
1 Bob 27 Los Angeles USA
2 Charlie 22 Chicago USA
3 David 32 Miami USA
b. Dropping Columns
Output:
c. Renaming Columns
print(df_renamed)
Output:
Pandas provides powerful methods for grouping and aggregating data, similar to SQL
group-by operations.
a. GroupBy
# Group by 'City' and calculate the mean age for each city
grouped = df.groupby('City')['Age'].mean()
print(grouped)
Output:
City
Chicago 22.0
Los Angeles 27.0
Miami 32.0
New York 24.0
Name: Age, dtype: float64
Output:
Age
mean max min
City
Chicago 22.0 22 22
Los Angeles 27.0 27 27
Miami 32.0 32 32
New York 24.0 24 24
a. Merging DataFrames
The merge() method allows you to merge two DataFrames based on common columns
Output:
b. Concatenating DataFrames
You can concatenate multiple DataFrames along rows or columns using concat().
Output:
Key Value
0 A 1
1 B 2
2 C 3
0 B 4
1 C 5
2 D 6
a. Date Parsing
Output:
Pandas is an extremely powerful and versatile library for data manipulation and analysis. Its
ability to handle various types of data structures, ease of use for time series data, and
support for a wide range of operations (including merging, grouping, and transforming data)
makes it indispensable in the data science ecosystem. Whether you're performing simple
data cleaning or more complex analysis, Pandas provides the tools needed to perform these
tasks efficiently and with high performance.
EXTRA -
matplotlib.pyplot is a module in the Matplotlib library, which is widely used in Python for
creating static, interactive, and animated visualizations. It provides a MATLAB-like interface
for plotting by encapsulating various plotting functionalities and offering a high-level API for
creating plots, graphs, and charts.
Works like a state machine, where pyplot maintains the state of the current figure and axes
(subplots).
Each function operates on the current figure or axes by default, unless specified otherwise.
Wide Range of Plot Types:
Line plots, scatter plots, bar plots, histograms, pie charts, error bars, box plots, and more.
Customization:
Supports a wide range of customization options for figure size, colors, markers, line styles,
labels, legends, and more.
Interactive Features:
1. Figure Management
plt.figure(figsize=(width, height)): Creates a new figure.
plt.subplots(): Creates a figure with subplots.
2. Plotting
plt.plot(x, y, fmt): Creates a line plot.
plt.scatter(x, y, s): Creates a scatter plot.
plt.bar(x, height): Creates a bar plot.
plt.hist(data, bins): Plots a histogram.
plt.pie(sizes): Creates a pie chart.
3. Customizing Plots
plt.title("Title"): Sets the title of the plot.
plt.xlabel("Label"), plt.ylabel("Label"): Sets axis labels.
plt.legend(): Adds a legend.
plt.grid(True): Adds a grid.
# Data
x = [1, 2, 3, 4, 5]
y = [2, 3, 5, 7, 11]
# Creating a plot
plt.figure(figsize=(8, 5)) # Create a figure
plt.plot(x, y, label="Prime numbers", color="blue", marker="o")
# Customizing
plt.title("Line Plot Example")
plt.xlabel("X-axis Label")
plt.ylabel("Y-axis Label")
plt.legend()
plt.grid(True)
# Show plot
plt.show()
🔟 Miscellaneous Functions
Function Description Example
os.remove("file.txt")