Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
22 views3 pages

Coding

This Python code defines a module for managing grocery items stored in a CSV file. It imports Pandas and Matplotlib libraries and defines a grocery data frame (dfgrocery) loaded from the CSV. The main loop displays a menu with options to add, update, delete, view grocery items or view charts. Based on the selection, it performs the corresponding action like updating an item record in the CSV file or plotting bar/line charts of items by company.

Uploaded by

ZONE OF CUBE
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)
22 views3 pages

Coding

This Python code defines a module for managing grocery items stored in a CSV file. It imports Pandas and Matplotlib libraries and defines a grocery data frame (dfgrocery) loaded from the CSV. The main loop displays a menu with options to add, update, delete, view grocery items or view charts. Based on the selection, it performs the corresponding action like updating an item record in the CSV file or plotting bar/line charts of items by company.

Uploaded by

ZONE OF CUBE
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
You are on page 1/ 3

import pandas as pd

import matplotlib.pyplot as plt

print('++++++++++++++++++++++++++++++++++++++++++++++++++')
print('+ +')
print('+ Welcome to Grocery Items Module +')
print('+ +')
print('++++++++++++++++++++++++++++++++++++++++++++++++++')
dfgrocery = pd.read_csv('grocery.csv',
header=None,
names=['itemno','name','company','qty','mrp'])
choice = 0
while(choice != 6):
print('Choices for the module')
print('1. Add a new Item')
print('2. Update Item')
print('3. Delete Item')
print('4. View grocery')
print('5. View Charts')
print('6. Exit')
choice = int(input('Enter your choice : '))

if(choice == 1):
itemno = int(input('Input Item Number : '))
name = input('Input Item Name : ')
company = input('Input Company : ')
qty = input('Input qty : ')
mrp = input('Input MRP (In Rs) : ')
noofgrocery = len(dfgrocery)
dfgrocery.loc[noofgrocery] = [itemno,name,company,qty,mrp]
print(dfgrocery)
dfgrocery.to_csv('grocery.csv',header=False,index=False)
elif (choice == 2):
itemno = int(input('Enter grocery itemno : '))
print(dfgrocery.loc[dfgrocery['itemno'] == itemno])
indexno = dfgrocery.loc[dfgrocery['itemno'] == itemno].index
print('1. Update grocery itemno')
print('2. Update Name')
print('3. Update Company')
print('4. Update Qty')
print('5. Update MRP')
updatechoice = int(input('Enter your choice for update : '))
if (updatechoice == 1):
newitemno = int(input('Enter new grocery itemno : '))
dfgrocery.loc[indexno,'itemno'] = newitemno
dfgrocery.to_csv('grocery.csv',header=False,index=False)
elif (updatechoice == 2):
newname = input('Enter updated grocery Name : ')
dfgrocery.loc[indexno,'name'] = newname
dfgrocery.to_csv('grocery.csv',header=False,index=False)
elif (updatechoice == 3):
newcompany = input('Enter updated grocery company : ')
dfgrocery.loc[indexno,'company'] = newcompany
dfgrocery.to_csv('grocery.csv',header=False,index=False)
elif (updatechoice == 4):
newqty = input('Enter updated grocery qty : ')
dfgrocery.loc[indexno,'qty'] = newqty
dfgrocery.to_csv('grocery.csv',header=False,index=False)
elif (updatechoice == 5):
newmrp = input('Enter updated grocery mrp : ')
dfgrocery.loc[indexno,'mrp'] = newmrp
dfgrocery.to_csv('grocery.csv',header=False,index=False)
print(dfgrocery.loc[dfgrocery['itemno'] == itemno])
print('----grocery Record Updated----')

elif (choice == 3):


itemno = int(input('Enter grocery itemno : '))
print(dfgrocery.loc[dfgrocery['itemno'] == itemno])
indexno = dfgrocery.loc[dfgrocery['itemno'] == itemno].index
confirm = input('Are you sure you want to delete it (Y/N) : ')
if (confirm == 'Y' or confirm == 'y'):
dfgrocery = dfgrocery.drop(indexno)
dfgrocery.to_csv('grocery.csv',header=False,index=False)
print('---------- Record Deleted ---------')

elif (choice == 4):


print('1. Display all ')
print('2. Display top ___ records')
print('3. Display bottom ___ records')
print('4. Display on the basis of Company')
print('5. Display on the basis of Item Name')
dispchoice = int(input('Enter your choice : '))
if (dispchoice == 1):
print(dfgrocery)
elif (dispchoice == 2):
top = int(input('Enter how many top records to be displayed : '))
print(dfgrocery.head(top))
elif (dispchoice == 3):
bottom = int(input('Enter how many bottom records to be displayed : '))
print(dfgrocery.tail(bottom))
elif (dispchoice == 4):
company = input('Enter company to search : ')
print(dfgrocery.loc[dfgrocery['company'] == company])
elif (dispchoice == 5):
name = input('Enter item to search (item name) : ')
print(dfgrocery.loc[dfgrocery['name'] == name])
elif (choice == 5):
print('1. Bar Chart - Company wise total items')
print('2. Line Chart - Company wise total items')
print('3. Back Menu')
chartchoice = int(input('Enter your choice : '))
if (chartchoice == 1):
dftemp = dfgrocery.groupby ('company').aggregate('count')
company = dftemp.index.to_list()
itemcount = dftemp['itemno']
plt.bar(company,itemcount)
plt.xlabel('Company')
plt.ylabel('Number of items')
plt.title('Company wise total items')
plt.show()
elif (chartchoice == 2):
dftemp = dfgrocery.groupby ('company').aggregate('count')
company = dftemp.index.to_list()
itemcount = dftemp['itemno']
plt.plot(company,itemcount)
plt.xlabel('Company')
plt.ylabel('Number of items')
plt.title('Company wise total items')
plt.show()

print('---------------Thanks for using Grocery Module-----------------------')

You might also like