Itterows Vis. Geo Data #10 Again

Here is my original code(not debugged yet):

for index, row in df.iterrows(): 
        lat2, lat1 = df['end_lat'], df['start_lat']
        lon2, lon1 = df['end_lon'], df['start_lon']
        if abs(lat2-lat1) < 180 and abs(lon2-lon1) < 180:
                m.drawgreatcircle(lon1, lat1, lon2, lat2)

the solution is:

for index, row in df.iterrows(): 
    lat2, lat1 = row['end_lat'], row['start_lat']
    lon2, lon1 = row['end_lon'], row['start_lon']
    if abs(lat2-lat1) < 180: 
        if abs(lon2-lon1) < 180:
            m.drawgreatcircle(lon1, lat1, lon2, lat2)

1st question:
why to use nested if instead of using if con1 and con2?
It seems I created an infinite loop and it took so long to use the program.
But I did remember in python intermediate course, we did use “if a==x and b==y:” or something like that.

2nd question:
Why row[‘end_lat’], df[‘start_lat’]? not df.[‘end_lat’]?

I did go back to the previous exercise, for geo_routes.csv we created. I do have to go iterate thru each row and retrieve the value(element) of lon1, lon2, lat1, lat2.
so df (afa. dateframe) here means something like an n-D array, while row here is like a list or 1-d array, am I right?

Why would you choose to use df.iterrows but not make use of the index, row variables generated?
Replacing the your first line with for useless_var in range(df.shape[0]) would produce the same result since you are not using index or row.
You are extracting a column of the dataframe out in each iteration, unlike the answer extracting a single value from a single row.
What the answer sends as arguments to drawgreatcircle are 4 single numbers while what you send are 4 series of numbers. Sometimes you may get errors doing that but seems like drawgreatcircle coincidentally accepts series for each of its arguments too.

That nested if is the same as using and just more readable and may be better for solving merge conflicts when using version control when they tell you line by line what’s changed and you may want 1 line to contain 1 condition only

1 Like

@hanqi Thank you for very detailed explanation. I got it. :clap:

Hey @yoyotooie!

Do you mind marking this post as solved? This puts the correct answer (here this would be @hanqi’s answer) in the original post!

Here’s a quick gif tutorial on how to do this:

@Mary Done. Thank you for letting me know.