How to get last value of column from a data frame

  pandas, python-3.x

I have a data frame like this

    ntil  ureach_x  ureach_y     awgt 
0      1         1        34  2204.25 
1      2        35        42  1700.25 
2      3        43        48   898.75 
3      4        49        53   160.25 

and an array of values like this

ulist = [41,57]

For each value in the list [41,57] I am trying to find if the values fall in between ureach_x and ureach_y and return the awgt value.

awt=[]
for u in ulist:
    for index,rows in df.iterrows():
                if (u >= rows['ureach_x'] and u <= rows['ureach_y']):
                        awt.append(rows['awgt'])

The above code works for within the value ranges of ureach_x and ureach_y. How do I check if the value in the list is greater than the last row of ureach_y. My data frame has dynamic shape with varying number of rows.

For example, The desired output for value 57 in the list is 160.25

I tried the following:

for u in ulist:
    for index,rows in df.iterrows():
                if (u >= rows['ureach_x'] and u <= rows['ureach_y']):
                        awt.append(rows['awgt'])
                elif (u >= rows['ureach_x'] and u > rows['ureach_y']):
                        awt.append(rows['awgt'])

However, this returns multiple values for 41 in the list. How do I refer only the last value in the column of reach_y in a iterrows loop.

The expected output is as follows:

for values in list:
[41,57]

the corresponding values from df has to be returned.
[1700.25 ,160.25]

Source: Python-3x Questions

LEAVE A COMMENT