UNIT 5 – Introduction to built in packages in python
Pandas in Python: A Detailed Explanation
Introduction to Pandas
Pandas is a powerful open-source data manipulation and analysis library built on top of
Python. It provides high-performance, easy-to-use data structures and data analysis tools,
making it a fundamental tool for data science, machine learning, and real-world data
processing.
Pandas is widely used for:
✅ Handling structured data (like tables, Excel files, CSV files).
✅ Data cleaning and transformation (handling missing values, filtering, and reshaping).
✅ Statistical analysis and visualization (descriptive statistics, graphs).
It is built on NumPy and works seamlessly with other Python libraries like Matplotlib,
Seaborn, and Scikit-learn.
Installation
If you haven't installed Pandas, you can do so using:
pip install pandas
1. Use of Pandas
Definition
Pandas is used for data manipulation, analysis, and transformation, making it an essential
tool for handling structured data efficiently.
Key Uses of Pandas in Python
1️⃣ Data Loading and Storage
Read and write data from multiple formats like CSV, Excel, JSON, SQL, and HDF5.
Example:
import pandas as pd
df = pd.read_csv("data.csv") # Load data from a CSV file
print(df.head()) # Display first 5 rows
2️⃣ Data Cleaning and Handling Missing Values
Handles missing or incorrect data effectively.
Example:
df.fillna(0, inplace=True) # Replace NaN values with 0
3️⃣ Data Filtering and Selection
Extract required data based on conditions.
Example:
df_filtered = df[df['Age'] > 30] # Select rows where Age is greater than 30
4️⃣ Data Transformation
Convert, merge, concatenate, and reshape data efficiently.
Example:
df['Salary'] = df['Salary'] * 1.1 # Increase all salaries by 10%
5️⃣ Statistical and Descriptive Analysis
Generate statistical summaries like mean, median, and standard deviation.
Example:
print(df.describe()) # Shows summary statistics
6️⃣ Data Visualization
Works well with Matplotlib and Seaborn to create graphs.
Example:
import matplotlib.pyplot as plt
df['Age'].hist()
plt.show()
Advantages of Pandas
✅ Fast and efficient for data manipulation.
✅ Supports various file formats (CSV, Excel, JSON, SQL).
✅ Easy handling of missing data.
✅ Powerful group-by and aggregation functions.
✅ Integrates with other data science libraries like NumPy, Matplotlib, and Seaborn.
Disadvantages of Pandas
❌ Consumes high memory when working with large datasets.
❌ Not suitable for real-time applications due to single-thread execution.
❌ Complex syntax for beginners when dealing with multi-dimensional data.
2. Pandas Series
Definition
A Pandas Series is a one-dimensional labeled array that can hold any data type, such as
integers, floats, strings, and Python objects. It is similar to a column in an Excel spreadsheet
or a NumPy 1D array with labels.
Creating a Pandas Series
import pandas as pd
data = [10, 20, 30, 40]
series = pd.Series(data)
print(series)
Output:
go
0 10
1 20
2 30
3 40
dtype: int64
Here, the left column represents index values, and the right column represents actual data
values.
Creating a Series with Custom Index
data = [100, 200, 300]
index_labels = ['A', 'B', 'C']
series = pd.Series(data, index=index_labels)
print(series)
Output:
A 100
B 200
C 300
dtype: int64
Operations on Pandas Series
1️⃣ Accessing Elements
print(series['A']) # Output: 100
2️⃣ Performing Mathematical Operations
print(series * 2) # Multiplies each element by 2
3️⃣ Checking for Null Values
print(series.isnull())
Applications of Pandas Series
✅ Representing time-series data.
✅ Storing single-column data from large datasets.
✅ Performing vectorized operations on numeric data.
3. Pandas DataFrames
Definition
A Pandas DataFrame is a two-dimensional table-like data structure with labeled rows and
columns, similar to an Excel spreadsheet or SQL table. It allows for efficient data
manipulation, analysis, and visualization.
Creating a DataFrame
import pandas as pd
data = {'Name': ['Alice', 'Bob', 'Charlie'], 'Age': [25, 30, 35], 'Salary': [50000, 60000, 70000]}
df = pd.DataFrame(data)
print(df)
Output:
Name Age Salary
0 Alice 25 50000
1 Bob 30 60000
2 Charlie 35 70000
Operations on DataFrames
1️⃣ Selecting Columns
print(df['Age']) # Retrieves Age column
2️⃣ Selecting Rows
print(df.iloc[1]) # Retrieves the second row
3️⃣ Filtering Data
print(df[df['Salary'] > 55000]) # Shows employees with Salary > 55000
4️⃣ Adding a New Column
df['Bonus'] = df['Salary'] * 0.1 # Adds a 10% bonus column
Advantages of DataFrames
✅ Handles large datasets efficiently.
✅ Supports multiple data formats (numeric, categorical, string).
✅ Built-in statistical and aggregation methods.
4. Pandas Read CSV in Python
Definition
The read_csv() function in Pandas is used to read data from a CSV (Comma-Separated
Values) file into a DataFrame.
Reading a CSV File
import pandas as pd
df = pd.read_csv("data.csv")
print(df.head()) # Display first 5 rows
Reading a CSV File with Custom Delimiter
df = pd.read_csv("data.csv", delimiter=";") # If the file uses semicolons
Reading a CSV File with Missing Values Handling
df = pd.read_csv("data.csv", na_values=["NA", "Missing"]) # Treats 'NA' and 'Missing' as NaN
Saving a DataFrame to CSV
df.to_csv("output.csv", index=False) # Saves DataFrame to a CSV file
Applications of Reading CSV
✅ Importing large datasets for analysis.
✅ Data preprocessing for machine learning.
✅ Handling real-time stock market or financial data.
Creating GUI Using Tkinter in Python
Introduction to Tkinter
What is Tkinter?
Tkinter is the standard Python library used for creating Graphical User Interface (GUI)
applications. It provides a simple yet powerful way to develop desktop applications using
Python.
Why Use Tkinter?
Built-in: Comes pre-installed with Python.
Lightweight & Fast: Requires no additional dependencies.
Cross-Platform: Works on Windows, macOS, and Linux.
Easy to Learn: Uses Python’s object-oriented programming concepts.
Installing Tkinter (if not installed)
Since Tkinter is included with Python, you usually don’t need to install it separately.
However, on some systems, you may need to install it manually:
pip install tk
Basic Structure of a Tkinter Program
To create a simple Tkinter GUI application, follow these steps:
1. Import Tkinter
2. Create the main window
3. Add widgets (buttons, labels, text boxes, etc.)
4. Run the application loop
Example:
import tkinter as tk
# Create main window
root = tk.Tk()
# Set window title
root.title("My First GUI")
# Set window size
root.geometry("400x300")
# Run the application
root.mainloop()
This will open a blank GUI window.
Tkinter Widgets
1. Entry Widget (Text Input Field)
Definition
The Entry widget is used to take single-line input from the user, like entering a username or
password.
Creating an Entry Widget
import tkinter as tk
root = tk.Tk()
root.geometry("300x200")
entry = tk.Entry(root, width=30) # Creates an entry field
entry.pack(pady=10) # Places the entry field in the window
root.mainloop()
Fetching Data from Entry Widget
def get_text():
user_input = entry.get()
print("User entered:", user_input)
button = tk.Button(root, text="Get Text", command=get_text)
button.pack()
Advantages:
Accepts user input.
Can be customized (e.g., text size, background color).
Supports password fields.
Disadvantages:
Accepts only one line of text.
2. Label Widget (Displaying Text)
Definition
The Label widget is used to display text or images in the GUI.
Creating a Label
label = tk.Label(root, text="Welcome to Tkinter!", font=("Arial", 14))
label.pack(pady=10)
Advantages:
Displays text, images, or icons.
Customizable (font, color, size).
Disadvantages:
Cannot take user input.
3. Button Widget (Clickable Button)
Definition
The Button widget allows users to trigger an action when clicked.
Creating a Button
def say_hello():
print("Hello, Tkinter!")
button = tk.Button(root, text="Click Me", command=say_hello)
button.pack()
Advantages:
Allows user interaction.
Can trigger custom functions.
Disadvantages:
Limited styling options.
4. Radiobutton Widget (Single Selection Button)
Definition
The Radiobutton widget allows users to select only one option from a list.
Creating Radio Buttons
selected_option = tk.StringVar() # Holds selected value
rb1 = tk.Radiobutton(root, text="Option 1", variable=selected_option, value="Option 1")
rb2 = tk.Radiobutton(root, text="Option 2", variable=selected_option, value="Option 2")
rb1.pack()
rb2.pack()
Advantages:
Ensures single selection from multiple options.
Disadvantages:
Limited use for multiple selections.
5. Checkbutton Widget (Multiple Selection Box)
Definition
The Checkbutton widget allows users to select multiple options (like a checkbox).
Creating Check Buttons
var1 = tk.IntVar()
var2 = tk.IntVar()
cb1 = tk.Checkbutton(root, text="Option A", variable=var1)
cb2 = tk.Checkbutton(root, text="Option B", variable=var2)
cb1.pack()
cb2.pack()
Advantages:
Users can select multiple options.
Disadvantages:
Cannot enforce only one selection (use Radiobutton for that).
Creating a Simple GUI Application
Complete Example: Login Form in Tkinter
import tkinter as tk
from tkinter import messagebox
# Function to handle login button click
def login():
username = entry_username.get()
password = entry_password.get()
if username == "admin" and password == "password123":
messagebox.showinfo("Login Successful", "Welcome, Admin!")
else:
messagebox.showerror("Login Failed", "Invalid username or password.")
# Create main window
root = tk.Tk()
root.title("Login Form")
root.geometry("300x200")
# Username Label and Entry
label_username = tk.Label(root, text="Username:")
label_username.pack()
entry_username = tk.Entry(root)
entry_username.pack()
# Password Label and Entry
label_password = tk.Label(root, text="Password:")
label_password.pack()
entry_password = tk.Entry(root, show="*") # Hides password input
entry_password.pack()
# Login Button
button_login = tk.Button(root, text="Login", command=login)
button_login.pack(pady=10)
# Run the application
root.mainloop()
Explanation:
1. Creates a GUI window with the title “Login Form”.
2. Displays labels and entry fields for the username and password.
3. Checks login credentials when the button is clicked.
4. Displays a message box based on whether the login is successful or not.
Advantages of Tkinter GUI
Simple and easy to use.
Works on multiple platforms.
Requires minimal code to create applications.
Disadvantages of Tkinter GUI
Limited design and styling options.
Not suitable for advanced GUI applications (like modern web apps).
Connecting to Database Using MySQL in Python
1. Installing MySQL Connector
What is MySQL Connector?
MySQL Connector is a Python library that allows Python programs to connect and interact
with MySQL databases. It enables database operations such as inserting, updating, deleting,
and retrieving data.
Installing MySQL Connector
To use MySQL in Python, you need to install the MySQL connector package using the
following command:
pip install mysql-connector-python
This package allows Python to communicate with MySQL databases.
2. Connecting to MySQL Database
To connect to a MySQL database, use the mysql.connector module.
Basic Connection Example
python
CopyEdit
import mysql.connector
# Establish connection
conn = mysql.connector.connect(
host="localhost", # Server address (use "localhost" for local MySQL server)
user="root", # MySQL username
password="password", # MySQL password
database="college" # Database name
# Check if connection is successful
if conn.is_connected():
print("Connected to MySQL database")
# Close connection
conn.close()
Explanation:
1. mysql.connector.connect(): Connects Python with MySQL.
2. Connection parameters:
o host: The database server (local or remote).
o user: Your MySQL username.
o password: Your MySQL password.
o database: The specific database you want to connect to.
3. **`.is_connected
4o
You said:
connecting to databse using mysql : installing mysql connector , cursor() object , execute()
method , fetchall() method , creating a simple program to connect database explain each
point in detail like previous
ChatGPT said:
Connecting to Database Using MySQL in Python
1. Installing MySQL Connector
What is MySQL Connector?
MySQL Connector is a Python library that enables Python programs to communicate with
MySQL databases. It allows users to execute SQL queries and perform operations like
inserting, updating, deleting, and retrieving data from MySQL databases.
Why Use MySQL Connector?
Provides an easy way to connect Python applications with MySQL.
Supports various SQL operations such as SELECT, INSERT, UPDATE, DELETE.
Works well with large databases and supports transactions.
Compatible with various MySQL versions.
Installing MySQL Connector
To use MySQL with Python, you need to install the MySQL Connector package. Install it
using the following command:
pip install mysql-connector-python
This will download and install the required package to establish a connection between
Python and MySQL.
Verifying Installation
After installation, verify whether the package is installed by running:
import mysql.connector
print("MySQL Connector Installed Successfully")
If the script runs without errors, the installation was successful.
2. Connecting to MySQL Database
Establishing Connection
To interact with a MySQL database, you must first establish a connection. The
mysql.connector.connect() function is used to connect to a MySQL server.
Example: Connecting to MySQL
import mysql.connector
# Connecting to MySQL database
conn = mysql.connector.connect(
host="localhost", # MySQL server address (use "localhost" for local server)
user="root", # Your MySQL username
password="password", # Your MySQL password
database="college" # Name of the database to connect to
# Checking if the connection was successful
if conn.is_connected():
print("Connected to MySQL database")
# Closing the connection
conn.close()
Explanation:
1. mysql.connector.connect(): Establishes a connection to MySQL.
2. Connection Parameters:
o host: The server address (localhost for local database).
o user: MySQL username.
o password: Password for authentication.
o database: Name of the MySQL database to use.
3. .is_connected(): Checks whether the connection was successful.
4. .close(): Closes the connection to free up resources.
3. Using cursor() Object
What is cursor()?
A cursor is a Python object used to execute SQL queries. It allows you to perform CRUD
operations (Create, Read, Update, Delete) in the database.
Creating a Cursor Object
cursor = conn.cursor() # Creating a cursor object
Once a cursor object is created, you can execute SQL commands.
Why Use Cursors?
They allow the execution of multiple queries in the same session.
Help in fetching records from the database efficiently.
Provide error handling while executing queries.
4. Using execute() Method
What is execute()?
The execute() method runs SQL queries to interact with the database. It is used for:
Creating tables
Inserting records
Updating records
Deleting records
Retrieving records
Example: Creating a Table
cursor.execute("""
CREATE TABLE students (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255),
age INT
""")
print("Table Created Successfully")
This will create a "students" table with columns:
id (Auto-Incrementing Primary Key)
name (Text Field)
age (Integer Field)
Example: Inserting Data
cursor.execute("INSERT INTO students (name, age) VALUES ('John Doe', 22)")
conn.commit() # Saves changes to the database
print("Record Inserted Successfully")
Why Use execute()?
Executes SQL queries in Python.
Allows interaction with multiple tables.
Supports batch execution (executing multiple queries).
5. Using fetchall() Method
What is fetchall()?
The fetchall() method retrieves all rows from the database after executing a SELECT query. It
returns a list of tuples, where each tuple represents a row from the result set.
Example: Retrieving Data from the Database
cursor.execute("SELECT * FROM students") # Select all records
rows = cursor.fetchall() # Fetch all rows
# Display data
for row in rows:
print(row)
Example Output:
(1, 'John Doe', 22)
(2, 'Alice Smith', 20)
(3, 'Bob Johnson', 25)
Why Use fetchall()?
Retrieves multiple rows at once.
Efficient when dealing with large datasets.
6. Creating a Simple Program to Connect to a Database
Complete Example: Insert and Retrieve Data
import mysql.connector
# Connecting to MySQL database
conn = mysql.connector.connect(
host="localhost",
user="root",
password="password",
database="college"
cursor = conn.cursor() # Creating a cursor object
# Creating a table (if not exists)
cursor.execute("""
CREATE TABLE IF NOT EXISTS students (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255),
age INT
""")
# Inserting a record
cursor.execute("INSERT INTO students (name, age) VALUES ('Alice', 20)")
conn.commit() # Save changes
# Retrieving data
cursor.execute("SELECT * FROM students")
rows = cursor.fetchall()
# Printing records
print("Students List:")
for row in rows:
print(row)
# Closing connection
conn.close()
Explanation:
1. Connects to MySQL database.
2. Creates a table if it doesn’t exist.
3. Inserts a record into the table.
4. Fetches and displays all records.
5. Closes the connection after operations.
Advantages of Using MySQL with Python
✅ Easy to Use: Simple syntax to interact with MySQL.
✅ Fast Execution: Efficient data retrieval and manipulation.
✅ Secure: Supports authentication, encryption, and access control.
✅ Scalable: Can handle large amounts of data efficiently.
Disadvantages
❌ Requires MySQL Server: MySQL must be installed before use.
❌ Connection Overhead: Managing multiple connections may slow performance.
❌ Limited Features: Does not support advanced database features like stored procedures
directly.