Python Pandas Project: Database Handling System
This project demonstrates how to implement a simple Database Handling System using Python
Pandas. It supports CRUD (Create, Read, Update, Delete) operations along with saving and
loading data from a CSV file. The database is simulated using a Pandas DataFrame.
Features of this Project: Insert Record → Add new employee data Display Records → Show all
entries Search Record → Find by ID Update Record → Modify existing data Delete Record →
Remove entry Save/Load Database → Store data in a CSV file (acts as a database)
import pandas as pd
# Initialize empty DataFrame (like a database table)
database = pd.DataFrame(columns=["ID", "Name", "Age", "Department", "Salary"])
# Function to display menu
def menu():
print("\n=== Database Handling Project ===")
print("1. Insert Record")
print("2. Display Records")
print("3. Search Record")
print("4. Update Record")
print("5. Delete Record")
print("6. Save Database to CSV")
print("7. Load Database from CSV")
print("8. Exit")
# Insert record
def insert_record():
global database
try:
id_ = int(input("Enter ID: "))
name = input("Enter Name: ")
age = int(input("Enter Age: "))
dept = input("Enter Department: ")
salary = float(input("Enter Salary: "))
# Append new record
new_record = {"ID": id_, "Name": name, "Age": age, "Department": dept, "Salary": salary}
database = pd.concat([database, pd.DataFrame([new_record])], ignore_index=True)
print("Record inserted successfully!")
except:
print("Invalid Input! Try again.")
# Display all records
def display_records():
if database.empty:
print("No records found!")
else:
print("\nDatabase Records:")
print(database)
# Search by ID
def search_record():
try:
id_ = int(input("Enter ID to search: "))
result = database[database["ID"] == id_]
if result.empty:
print("Record not found!")
else:
print("Search Result:\n", result)
except:
print("Invalid Input!")
# Update record
def update_record():
try:
id_ = int(input("Enter ID to update: "))
index = database[database["ID"] == id_].index
if index.empty:
print("Record not found!")
else:
name = input("Enter new Name: ")
age = int(input("Enter new Age: ")
dept = input("Enter new Department: ")
salary = float(input("Enter new Salary: "))
database.loc[index, ["Name", "Age", "Department", "Salary"]] = [name, age, dept, salary]
print("Record updated successfully!")
except:
print("Invalid Input!")
# Delete record
def delete_record():
try:
id_ = int(input("Enter ID to delete: "))
global database
database = database[database["ID"] != id_]
print("Record deleted successfully!")
except:
print("Invalid Input!")
# Save to CSV
def save_database():
database.to_csv("database.csv", index=False)
print("Database saved to 'database.csv'.")
# Load from CSV
def load_database():
global database
try:
database = pd.read_csv("database.csv")
print("Database loaded successfully!")
except:
print("No saved database found!")
# Main program loop
while True:
menu()
choice = input("Enter your choice: ")
if choice == "1":
insert_record()
elif choice == "2":
display_records()
elif choice == "3":
search_record()
elif choice == "4":
update_record()
elif choice == "5":
delete_record()
elif choice == "6":
save_database()
elif choice == "7":
load_database()
elif choice == "8":
print("Exiting program...")
break
else:
print("Invalid choice! Please try again.")