Archive

Archive for August 18, 2019

Python: Pandas Lesson 12

August 18, 2019 1 comment


Learning : Pandas Lesson 12
Subject: Zoo Management System – P6

In this last post of Zoo Managment System series we will cover two of most important functions after the Adding Records, they are Editing a Record and Deleting Record, both will be in there simplest form with no validations code or try … Exception block.

Editing Record: To edit a record we need to identify it first, so we will use the id then the function will ask the user for each column if he want to change/update the data, if the user select Yes (y) then he will get the prompt to enter the new data, and if he select No (n) then the application will ask him about the other column. Here is the code ..

Edit a Record

def edit_record(df):

clear() # To clear the terminal.

print(‘\n We are in a editing mode, for each column if you want to edit select ”y” if not select ”n”\n’)

# First we will fitch the columns from the df

col_list = []

for each in df.columns :

col_list.append(each)

print(df.to_string(index=False))

print(‘\n Select the id of the rescord you want to edit:’)

edit_id = input(‘ Enter the id : (c for cancel ) ‘)

if edit_id in [‘c’,’C’] :

input(‘ You select to cancel the editing… \n ** Press any key to continue .. . . ‘)

return

else:

for each in col_list:

if each !=’id’:

chose_edit= input(‘\n Do you want to change {} (y or n)’.format(each))

if chose_edit in [‘y’,’Y’]:

you_change = input(‘ Enter the new value: ‘)

df.loc[df[‘id’] == int(edit_id), [each]] = you_change

else:

input(‘\n You select not to change {} press Enter to change another column.’.format(each))

print(‘\n You finish Editing. .. \n ‘)

input(‘\n\n\n\n ** Press any key to continue .. . . ‘)


Here is a screen shot of the code …

Deleting a Record:A last function we will work on is deleting a record. To do this first we will show the user all the rows in the DataFrame then he will select the id of the column to be deleted. Here is the code..

Delete a Record

def delete_record(df1):

clear()

print(‘\n’,df1.to_string(index=False))

del_record = input(‘\n Enter the ”ID” for the record you want to delete: ‘)

#Delete the row with id = del_record 

df1 = df1[df1.id != int(del_record)]

print(‘\n Record been deleted … .. \n ‘)

global df # Reset the df as global variable

df = df1

input(‘\n\n\n\n ** Press any key to continue .. . . ‘)


Now we just finish a simple application to manage our zoo file. When we start writing pandas lessons I select a simple data frame csv file fond on the web, and we work on it as simple as we can to go through a most important functions in any application we plan to write. We did’t cover any validations part or any mistakes the user may do. So to avoid all this I am planning to start working on another project that we will cover any missing part here.

Also I am working on publishing the source code of this application on my download page here..




:: Pandas Lessons Post ::

Lesson 1 Lesson 2 Lesson 3 Lesson 4
Lesson 5 Lesson 6 Lesson 7 Lesson 8
Lesson 9 Lesson 10 Lesson 11 Lesson 12



Follow me on Twitter..




Advertisements