Archive

Archive for August 11, 2019

Python: Pandas Lesson 9

August 11, 2019 5 comments


Learning : Pandas Commands
Subject: Zoo Management System – P3

In this lesson we will write two functions in our Zoo Management System-ZMS, first one to load the data file (we assume we have our data file in the same directory of the .py file.) and the second one is to display a sample 5 rows from the data. We will write each function and test it then we will add it to our main app user_choice() section and will try it from there. At the end of this post you can find a screen shot of the user_choice() code with two function in place.
Also I would like to emphasize that I am not using any try .. exception block in this system, so I am expecting that the user will enter a valid data.

Load New File: As far we want to be simple in this lesson, we will assume that we have a file named ‘data_file_zoo.csv’ and that it stored in the same directory as our source file .py, so we will not cover the [find file code] to change or select other file in other directory.(maybe in version 2.0 of this app if we want to upgrade it. ).

Enhancement ides:
1. Giving the user the ability to change the path and the file.
2. User can start form new empty file and create the columns and data of his new data file.

def load_file()
def load_file() :

filename=’data_file_zoo.csv’

global df

df = pd.read_csv(filename)

print(‘\n\n *** File Been Loaded *** \n’)

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


Show sample data: After loading the data into a df, and we set the df as a global variable so we can use it any-where in our application. So to print-out a sample data we will use coming code.

get_sample_data(df2)
def get_sample_data(df2):

clear() # To clear the terminal.

print(‘\n\n Sample Data from .. \n’,df2.sample(5))

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



Here is the two function in the def user_coice(u_choice):



Summary: At this point we have the menu and two functions, each time we run the app we need to select first number 1 to load the data into a df, the idea behind this was to give the user the opportunity to select any data file from any path, but in this stage the data file will be static in the code. this feature will be in version 2.0
After loading the data we can select number 2 from the menu ( Show sample data ).
And here is a run screen shot..

In coming Next Post .. in coming post we will cover more functioins in our system. We will check the columns name, seeing the data type of each column and sorting the data in the data frame.




:: Pandas Lessons Post ::

Lesson 1 Lesson 2 Lesson 3 Lesson 4
Lesson 5 Lesson 6 Lesson 7 Lesson 8
Lesson 9



Follow me on Twitter..




Advertisements