Hello, in the previous lecture, you were
able to build this map with multiple
markers. This map happens to have only
two markers, but you get the idea, and
the script was this one here, so you can
add as many coordinate pairs as you want in here.
But in this lecture, I want to show you how to add
pairs of coordinates out of a text file,
which is this one here, a Volcanoes.txt.
[No audio]
If you like, you can just rename the name of this
from Volcanoes.txt to Volcanoes.csv
then you can open it in a
program such as Excel, if you like, you
can show it on a plain text file, as I'm
doing in here. Python can read both CSV
and text files. So what do we have here, here
are some data about volcanoes.
Basically, we have a number of the
volcano, which is some sort of ID. So
VOLCANX020, we have yet another NUMBER then.
[No audio]
And as you see every column, so we have columns
in this data file, and every column is separated
by a comma. This is the name of the
first column, which is which the
first value of this first column is this
one here, then is the name of the second
column. The first value of the second
column is this one here, then the second
value of the second column is this, and so
no, and then we have the name column,
the name of the volcano in here. Sorry,
the name is here, Baker, and then the comma
starts here, which means that
location is the some near US-Washington,
and then you have the STATUS, STATUS,
Historical for the first volcano
and then we have the elevation, which is
this one in here. I'm not sure whether
these are in feet or meters. But you
can find that out. If you're curious,
you can do a research for these particular
volcano and compare the elevation.
Anyway, then we have a TYPE
[No audio]
of the volcano, and we have a TIMEFRAME,
which is D3, I'm not sure what
this means a D4, and so, and then
lastly, this is where we are interested
about, we have the LAT and LON columns.
So this is the lat which means latitude, and
this is longitude of the volcano.
[No audio]
So you need these two numeric values in order
to map the features of a volcanoes into
a map, in this case into a folium.Map.
So we have as around 63, actually
62, 63, including the header of the data.
Also how do we load this file into Python?
Well, to load that file into Python,
let me clear the terminal and you would
first need to install a very useful
library called pandas. We're going to
use this library later on. So I'm not
going to explain pandas in detail for
now, because we were just using one or
two of its functions, which I'll explain
in this lecture. So go ahead and install
pandas, if you don't have pandas,
pip install pandas or pip3 install pandas,
depending on how you have
configured Python, and then you can go
ahead and open a Python session.
import pandas and the way to load a file with
pandas is by pointing to pandas.read_csv
[No audio]
Volcanoes.txt, make sure
that your Python session has been opened
in the directory where your Volcanoes.txt
file is. So for instance, my session is
in the same mapping folder, which is this
one here. So I can just pass the file name.
[No audio]
Otherwise, you may have to pass absolute path
of the file which starts from like C//
and so on. Depending on what operating
system you're in, and execute that
and call data and you get the data printed out.
Just like that.
[No audio]
And so pandas is able, actually the read_csv method
is able to distinguish between these commas where
the commas are, and so it creates a well
structured data format with columns. So
if you go down here, so you have some
columns here, column, column, column, and
then up to the end, and then you have other
columns, because Python cannot print
out. Actually, the terminal doesn't
allow you to have all the columns in one,
inside all of this area here, because it'd be
too much. Anyway, you have STATUS,
ELEVATION, TYPE, TIMEFRAME, LAT,
[No audio]
and LON. So that's about the data.
Now you need to figure out a way to
iterate through that dataframe. So this
is called a dataframe.
[No audio]
So the data object is called a dataframe,
you can check its type, actually,
[No audio]
pandas.core.frame.DataFrame.
So what I'm thinking of is to actually create
two lists out of this dataframe columns. So to put
the latitude column in a Python list,
and the longitude column in another
Python list, and so that we have a native
Python object, which is a list, and then
we can iterate through that list using
the for loop. So let me try to do that.
[No audio]
import
[Author typing]
pandas
[No audio]
and then create the dataframe
object. So loading the data, read_csv
Volcanoes.txt and yeah, that's it.
So how do we convert a dataframe column
into list. Well, the way to do that is
by just doing, let's say, l or lat
equals to data and then the name of
the column, which is lat.
[No audio]
So you can get a list of columns, by the way,
by doing data.columns.
[No audio]
Here, you see you have a LAT and a LON column,
then you do lat equals to list and data
[No audio]
LON
[No audio]
in square brackets, close it, and so
what you did here is data with a LON
attribute there, will return a series
object, but that series object, we are
converting it into a native list. So
that will be a Python list.
[No audio]
This will also show up as a similar object.
[No audio]
So this is a series object and this is
a list. But I prefer to work on a list
because working on lists is faster
than working on dataframe series. So the
idea is to have two lists here.
[No audio]
lat equals to list data LON, copy that
paste it here, and the same thing goes for
longitude. So lon, lat and lon list.
[No audio]
So now we need to be careful here
on how we iterate through these two
lists. Because you know, what we have is
we have two lists, and each of them has
62 items, you can check that using
len here, down here, lat.
So it has 62 items, and the same goes for the
longitude list. That means that the
first item of the latitude list
corresponds to the second item of the
latitude list, sorry, of the longitude
list. So the first two items make up the
first location of the first marker. That
means a lat, in the first iteration, we
need to extract both locations. So
actually, this sort of structure won't
be appropriate anymore. What you need to
do is you need to go through, let's say
lat and lon variables in, when you
iterate through two lists at the same
time, you need to use the zip function, and
then goes lat and lon. So basically what this
function does is like this, and let's say
for i, j in zip.
Let's say the first list is 1, 2, 3,
[No audio]
and the second is 4, 5, 6.
[Author typing]
print i and j.
[Author typing]
So this is what happens, this loop
will go through this to lists at the same
time. So what happens is that I will go
through the first item, or the first
list, while j will go through the first
item of the second list, and so in the first
iteration 1 and 4 are extracted,
so, 1 here and 4 here.
[No audio]
And you can do such an extraction by using
the zip function. Otherwise, you won't be able
to do that if you don't use the zip
function. So, zip sort of distributes
the items one by one, and so the same goes here,
lt will get the first item of this
list, lon will get the first item
of this list and then here, what you need
to do is in the constructor list with lt
and ln, and that's it, save the script, go
to the terminal to execute. Let's type here
in the terminal, I'll just exit that.
python3 map1.py execute.
No errors, go to Firefox and reload.
[No audio]
And I have got no markers for some reason but
I didn't get an error. So this is one of
those scenarios where you don't get an
error in the terminal. So in that case,
you need to double check your code
carefully. So data pandas.read_csv, lat
list data LON here is there. So what's
happening is that I'm assigning the
wrong column to the latitude list.
So let me change the order and try again.
So don't get intimidated by
errors, errors happen all the time. You
just need to be cold blooded and read
the errors if you get any error here,
and also look at the code carefully.
[No audio]
Let me reload. I've got some markers now.
[No audio]
So these are the locations of volcanoes
in North in US actually, United States, okay.
[No audio]
They also have this popup, which
is working well, but I assume you don't
like it very much because it's not
showing any information. So what we need
to do here is to actually make this popup
dynamic so that it shows some actual
information, and I'll show you how to
[No audio]
create dynamic popups even next video.
Talk to you there.