Home > Learning, Lesson, Problem, Projects/Experiments, Python > Python: Pandas Lesson 10

Python: Pandas Lesson 10



Learning : Pandas Commands
Subject: Zoo Management System – P4

In the last lesson, we wrote the first two function (loading the file and showing some sample data), now we will show the columns in our data file, showing the data type of each column and sorting the data based on certain column.

Show Columns: In most or all cases, we need to take a look to what we have in our data file and columns name will give us a fast idea of what we should expect. To do this we will write this code..

Show Columns

def show_column(df):

clear() # To clear the terminal.

#first we will count the column.

print(‘\n\n There is {} column in the DataFrame. ‘.format(len(df.columns)))

print(‘\n The columns name: ‘,df.columns)

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



Enhancement: If we want to add more flexibility to this function, we may give the user the opportunity to change the columns name, but we want this application to be as simple as we can.

Data Type: Knowing the Data type of our fields is important, in case we want to apply the data validation on user entries we will use the data type, so here is the function to do this.

def data_type()

def data_type(df):

clear() # To clear the terminal.

print(‘\n\n The data type of each columns in the DataFrame is :\n’)

print(df.dtypes)

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




Run Time ..


Third function we want to write is Sort, sorting the dataframe and in this function we will give the user the ability to select the column to sort the data based on it.
To do this, we first will list the clomns in the date frame then we will ask the use to select a column of sort.

Here is the code

Sorting based on user selection

def sort_df(df):

clear() # To clear the terminal.

x = 1

print(‘\n\n The list of the columns in the DataFrame are:’)

for each in df.columns : # List the columns in DataFrame

print(‘ ‘,x,each)

x +=1

u_sele=input(‘\n Enter the column number you want to sort the data based on: ‘)

print(‘\n\n Data sorted on ‘,df.columns[int(u_sele)-1])

print(‘\n’,df.sort_values(df.columns[int(u_sele)-1]))

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

Screen shot of the code.
After selecting option No.5 (Sort) from the main menu, we will have a list of columns in the DataFrame to select from.
DataFrame sorted based on our selection.
Another try of sorting.



Enhancement: In sorting we can add to sort Descending or Ascending, but we will keep this for next version.

Wrapping Up: At this point we finised five functions of our system, I will upload the code file pandas_zoo_app.py at the end of this lesson section, the file will be on my Download Page.




:: 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
  1. No comments yet.
  1. August 15, 2019 at 8:34 am
  2. August 18, 2019 at 8:41 am

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s