Documents 12-28 081644
Documents 12-28 081644
Hardware Requirement
PC/Laptop/MacBook with
Intel core/i3/i5/i7 or any equivalent
With at least 2 GB RAM
10 MB free space on Hard Disk
LCD/LED
import pandas as pd
import matplotlib.pyplot as plt
def plot_data(df):
# Visualize salary distribution
plt.figure(figsize=(8, 6))
plt.bar(df['Name'], df['Salary'], color='skyblue')
plt.title('Salary Distribution', fontsize=16)
plt.xlabel('Name', fontsize=12)
plt.ylabel('Salary', fontsize=12)
plt.grid(axis='y', linestyle='--', alpha=0.7)
plt.show()
def authenticate(users):
while True:
print("Welcome to the Data Center!")
user_name = input("Enter your name: ")
def add_entry(df):
name = input("Enter name: ")
age = int(input("Enter age: "))
salary = float(input("Enter salary: "))
new_entry = {'Name': name, 'Age': age, 'Salary': salary}
df = df.append(new_entry, ignore_index=True)
print(f"Added entry: {new_entry}")
return df
def delete_entry(df):
name_to_delete = input("Enter the name of the person to delete: ")
if name_to_delete in df['Name'].values:
df = df[df['Name'] != name_to_delete]
print(f"Deleted entry with name: {name_to_delete}")
else:
print(f"No entry found with name: {name_to_delete}")
return df
def update_entry(df):
name_to_update = input("Enter the name of the person to update: ")
if name_to_update in df['Name'].values:
index = df[df['Name'] == name_to_update].index[0]
print(f"Updating entry for {name_to_update}:")
new_age = int(input("Enter new age: "))
new_salary = float(input("Enter new salary: "))
df.at[index, 'Age'] = new_age
df.at[index, 'Salary'] = new_salary
print(f"Updated entry: {df.loc[index]}")
else:
print(f"No entry found with name: {name_to_update}")
return df
def view_data(df):
print("\nCurrent Dataframe:")
print(df)
def main():
# Define allowed users and their passwords
users = {"Avnish Nunia": "avnish123",
"Aditya Raj": "aditya456",
"Aditya Janu": "aditya789"}
while True:
print("\nOptions:")
print("1. View Data")
print("2. Add Entry")
print("3. Delete Entry")
print("4. Update Entry")
print("5. Visualize Data")
print("6. Exit")
if choice == "1":
view_data(df)
elif choice == "2":
df = add_entry(df)
elif choice == "3":
df = delete_entry(df)
elif choice == "4":
df = update_entry(df)
elif choice == "5":
plot_data(df)
elif choice == "6":
print("Exiting the Data Center. Goodbye!")
print("Made By Avnish Nunia, Aditya Janu and Aditya Raj.")
break
else:
print("Invalid choice. Please try again.")
if __name__ == "__main__":
main()
Code
Explanation
Import Libraries
import pandas as pd import matplotlib.pyplot as plt
· pandas: Used for data manipulation and analysis. In this program, it's used to
create and manage a DataFrame.
· matplotlib.pyplot: Used for data visualization. It creates the bar chart for the
salary distribution.
Dummy Data Creation
def create_dataframe():
data = {
'Name': ['Alice', 'Bob', 'Charlie', 'David'],
'Age': [25, 30, 35, 40],
'Salary': [50000, 60000, 70000, 80000],
}
df = pd.DataFrame(data)
return df
· Creates a dictionary with data about employees: their names, ages, and
salaries.
· Converts this dictionary into a pandas DataFrame.
· Returns the DataFrame, which will act as the core data structure for this
program.
Plotting Data
def plot_data(df):
plt.figure(figsize=(8, 6))
plt.bar(df['Name'], df['Salary'], color='skyblue')
plt.title('Salary Distribution', fontsize=16)
plt.xlabel('Name', fontsize=12)
plt.ylabel('Salary', fontsize=12)
plt.grid(axis='y', linestyle='--', alpha=0.7)
plt.show()
· plt.figure(): Creates a figure with specified dimensions (8x6 inches).
· plt.bar(): Plots a bar chart with names on the x-axis and salaries on the y-axis.
· Styling:
o Adds a title and labels for x and y axes.
o Adds gridlines for better readability.
· plt.show(): Displays the chart to the user.
User Authentication
def authenticate(users):
while True:
print("Welcome to the Data Center!")
user_name = input("Enter your name: ")
if user_password != users[user_name]:
print("Error: Incorrect password. Try again.")
continue
print(f"Login successful! Welcome, {user_name}!")
break
1. Takes a dictionary userscontaining allowed usernames and passwords.
2. while True: Ensures the program remains in the authentication loop until
valid credentials are provided.
3. Checks if the entered name exists in the usersdictionary.
o If not, it prompts again.
4. Validates the password corresponding to the username.
o If the password is incorrect, it prompts again.
5. Once both name and password are validated, the user is welcomed, and the
loop exits.
Add an Entry
def add_entry(df):
name = input("Enter name: ")
age = int(input("Enter age: "))
salary = float(input("Enter salary: "))
new_entry = {'Name': name, 'Age': age, 'Salary': salary}
df = df.append(new_entry, ignore_index=True)
print(f"Added entry: {new_entry}")
return df
· Prompts the user to input a new entry's name, age, and salary.
· Creates a dictionary (new_entry) for the new row.
· df.append(): Adds the new entry to the DataFrame.
· Returns the updated DataFrame.
Delete an Entry
def delete_entry(df):
name_to_delete = input("Enter the name of the person to delete: ")
if name_to_delete in df['Name'].values:
df = df[df['Name'] != name_to_delete]
print(f"Deleted entry with name: {name_to_delete}")
else:
print(f"No entry found with name: {name_to_delete}")
return df
· Asks for the name of the person to delete.
· Checks if the name exists in the DataFrame.
· Deletes the row: Keeps all rows except the one with the specified name using
filtering.
· If the name doesn't exist, it shows an error message.
Update an Entry
def update_entry(df):
name_to_update = input("Enter the name of the person to update: ")
if name_to_update in df['Name'].values:
index = df[df['Name'] == name_to_update].index[0]
print(f"Updating entry for {name_to_update}:")
new_age = int(input("Enter new age: "))
new_salary = float(input("Enter new salary: "))
df.at[index, 'Age'] = new_age
df.at[index, 'Salary'] = new_salary
print(f"Updated entry: {df.loc[index]}")
else:
print(f"No entry found with name: {name_to_update}")
return df
· Finds the row corresponding to the name provided.
· Updates the Ageand Salarycolumns with new values.
· If the name is not found, it displays an error message.
View the Data
def view_data(df): print("\nCurrent Dataframe:") print(df)
· Displays the current state of the DataFrame.
Main Function
def main():
users = {
"Avnish Nunia": "avnish123",
"Aditya Raj": "aditya456",
"Aditya Janu": "aditya789"
}
authenticate(users)
df = create_dataframe()
while True:
print("\nOptions:")
print("1. View Data")
print("2. Add Entry")
print("3. Delete Entry")
print("4. Update Entry")
print("5. Visualize Data")
print("6. Exit")
if choice == "1":
view_data(df)
elif choice == "2":
df = add_entry(df)
elif choice == "3":
df = delete_entry(df)
elif choice == "4":
df = update_entry(df)
elif choice == "5":
plot_data(df)
elif choice == "6":
print("Exiting the Data Center. Goodbye!")
print("Made By Avnish Nunia, Aditya Janu and Aditya Raj.")
break
else:
print("Invalid choice. Please try again.")
break else:
print("Invalid choice. Please try again.")
· User Authentication: Calls authenticate()to ensure only valid users can
proceed.
· Creates the initial DataFrame using create_dataframe().
· Offers a menu of options for interacting with the DataFrame.
Run the Program
if __name__ == "__main__": main()
· Ensures the main()function runs only when the script is executed directly.
Screenshot
s
Authentication Process
When you run the script, it will prompt for a username and password.
Example:
If you enter an incorrect name or password, it will keep asking until you provide
the correct credentials.
Options:
Options:
Output Example:
Option 2: Add Entry
Output:
Output:
Output:
Option 6: Exit