Python Handling and Sorting Data Importing data lists

  • Slides: 10
Download presentation
Python: Handling and Sorting Data Importing data, lists, performing calculations and sorting data. V

Python: Handling and Sorting Data Importing data, lists, performing calculations and sorting data. V 3

Scenario: Met Office Data Analysis The Met Office capture weather and temperature data in

Scenario: Met Office Data Analysis The Met Office capture weather and temperature data in the UK. You have been given a. csv file which contains four separate temperature readings (in Celsius) for each month. You have been asked to: ● Import the data ● Calculate the minimum temperature reading for each month. ● Calculate the maximum temperature reading for each month. ● Calculate the average temperature reading for each month. ● Sort the data to show the lowest temperature by month ● Sort the data to show the highest average temperature by month.

Importing the data The data we have been given is a. csv file (comma-separated

Importing the data The data we have been given is a. csv file (comma-separated values). Python has a csv library which will help us handle the data in this file type. First we need to import the data contained within the file.

Datatypes The data has now been copied from the csv file into newlist. To

Datatypes The data has now been copied from the csv file into newlist. To be able to perform the min, max and average calculations we need to consider the types of data that are currently in our newlist. The ‘’ around each number indicate that they are strings. To be able to use the data to perform calculations such as min, max and average we need to cast (convert) the numbers in the list to integers.

Calculations: Minimum In this example we will use the min function. This function is

Calculations: Minimum In this example we will use the min function. This function is built into python and will output the smallest of two or more arguments. The lowest temperature will then be added (duplicated) to the end of newlist in position [5].

Calculations: Maximum We are going to use the same principle to add the maximum

Calculations: Maximum We are going to use the same principle to add the maximum temperature to the end of newlist. Like the min function Python also has a max function which will return the largest of two or more arguments. N. B. Remember you will need to increase the range of items your are appending into the newlist.

Calculations: Average We are going to use the same principle of adding the average

Calculations: Average We are going to use the same principle of adding the average temperature to the end of newlist. Python does not have an average function but we can easily find the average by using the sum function to add the four temperatures and then divide them by four. This will give us the mean average of the temperatures that month. To keep the data looking neat I will be rounding the average to the nearest whole number using the round function. N. B. Remember you will need to increase the range of items your are appending into the newlist.

newlist recap We currently have a 2 D (dimensional) list, which is a list

newlist recap We currently have a 2 D (dimensional) list, which is a list within a list. This is how our current inner list looks like in newlist. Items [5] is the minimum value of [1: 4] Items [0: 4] is the data we copied from the csv file. Format: [Month, Temp 1, Temp 2, Temp 3, Temp 4] Items [7] is the average value of [1: 4] Items [6] is the maximum value of [1: 4]

Sorting Data: Lowest Temp by Month newlist now holds all of the information we

Sorting Data: Lowest Temp by Month newlist now holds all of the information we need to complete the task but we now need to sort the data in newlist to complete the tasks. The ‘sorted’ function will sort the data in any given list (but only on item [0] of that list). So that we only have the lowest temp reading and month we will create a new list (low. Temp) which will contain only these two items (min temp [5] and month [0]), remember we need to make sure the data we are sorting on is in position [0] of low. Temp. In this scenario we are sorting by the lowest temp by month. The first line of code uses list comprehension you can click on the link to find out more information.

Sorting Data: Highest Average Temperature by Month This method uses the same principle as

Sorting Data: Highest Average Temperature by Month This method uses the same principle as sorting the data by the lowest temperature. Except this time I have included the average temperature (item [7] in newlist) instead of the lowest temperature. Note that the average temp is in position [0] in the new sorted list average. Temp. Also to order them in descending order use the ‘reverse = True’ within the sorted function. This changes the sort from Ascending to Descending.