Itterows Vis. Geo Data #10

Can someone walk me through 4 lines of code with itterows? I am just not sure why it written like it is. This is from Vis. Geo Data #10

for index, row in df.iterrows():
        end_lat, start_lat = row['end_lat'], row['start_lat']
        end_lon, start_lon = row['end_lon'], row['start_lon']

Thanks,
Kris

1 Like

Welcome to the community, @kris.

Below describe the four line of code. Hope it helps.

Line 1

According to the DataFrame.iterrow documentation, iterrows returns tuple (index, row) - that is, a pandas Series object for each row and index.

A tuple is (x,y). In our case (index, row)
You can assign variables to a tuple as long as you match the number values inside a tuple. Example, x, y = (1, 2). (1, 2) is a tuple.

Descriptive names to describe what the label hold:
index := the row number
row := the series data

df is a DataFrame object.

.iterrows() returns an iterator to the dataframe. That is, it returns the reference/pointer to the Series data per row.

index is a label that stores the index data returned by generator iterator object. This index is pair with each row of data. This indicates which row you are accessing. Index does not generates from the Series data, but generated from the iterator object.

row is a label that stores the df’s each row data in Series format returned by generator iterator object.

in belongs to
index and row comes from the df.iterrows()

Using for loop to iterate through all elements in df.iterrows().

Line 2
For each iteration, do the following:

Explain how end_lat, start_lat gets assigned:

Using tuple assign syntax:

x =  1
y =  2

is equivalent to

x, y = 1, 2

Then,

is shorthand for

end_lat = row['end_lat']
start_lat = row['start_lat'] 

Assign this row’s start latitude data to start_lat
Assign this row’s end latitude data to end_lat

Since there are no operation performed on the end_lat and start_lat, then for each iteration, end_lat and start_lat will overwrite the previous row’s data.

Line 3
For each iteration, do the following:

Explain how end_lon, start_lon` gets assigned:

Using tuple assign syntax:

x =  1
y =  2

is equivalent to

x, y = 1, 2

Then,

is shorthand for

end_lon = row['end_lon']
start_lon = row['start_lon'] 

Assign this row’s start longitude data to start_lon
Assign this row’s end longitude data to end_lon

Since there are no operation performed on the end_lon and start_lon, then for each iteration, end_lon and `start_lon will be overwrite the previous row’s data.

Line 4

Empty line to signify the end of loop

Once outside of the loop scope, the program do not have access to the local variable start_lat, end_lat, start_lon, and end_lon.

2 Likes

:rofl:
Technically correct.

1 Like

Thanks for the detailed reply.

Thanks for the reply.