Sunday 9 August 2015

Working with csv files in Python

Introduction:

In this post we will see how to use csv files in python.For this purpose we will use flight data from
flight data.Save this file as a csv file and then import using the csv module in python.
Number of airports in each city:
Let's try and find out the number of airports in each city.

Now let's see how to get the output for a specific country say for Australia.

Explanation of the above code:
1.) We create an empty dictionary called Airport.
2.) After that each record is appended to an array line[].
3.) The in the if statement we check the third column (containing country name) and the first column (airport name).If the dictionary has country name as key in it we would append the value to it, else  create a new key as a new country.
4.) Now we print the airports in Australia from the airport dictionary.

Airline Route Histogram:
Lets see how to plot a histogram showing the geographical distribution of each flight.We need to do the following:

1.) Read in the airport file and create a dictionary mapping the unique ID of each airport to latitude and longitude.

2.) Read in the route files and get the IDs of the source & destination airports.Using the latitude and longitude of each flight we calculate the distance of each and append it to a list of all route lengths.

Now in order to measure the distance we need a new module called"geo_distance"

Code:

All the code that has been used:

Explanation :

Airport file (airports.dat) is used to build a dictionary mapping the unique airport ID to the geographical coordinates.
Routes file (routes.dat) is used to get the IDs of the source and destination airports. And then look up for the latitude and longitude based on the ID . Finally, using these coordinates, calculate the length of the route and append it to a list distances = []of all route lengths.

Now let's draw a histogram showing the distribution of different flight lengths.
Output : 





No comments:

Post a Comment