Search code examples
pandasjupyter-notebookkeyerror

pd.drop working outside a function but not in it


Working on creating a data frame from correlating two separate ones. Everything is working smoothly, on it's own and in Jupyter where I check it independently.

I just added line another condition to drop a line from my dataframe - if it's a duplicate of the "current_edge['street_name']. This line works in Jupyter notebook when I test, but not when I put it into the function.

# current_edge is a dictionary of values, edge_az is a float
# edge is my dataframe

for row in edge.index:

    if abs(current_edge['edgeAzimuth_deg'] - edge_az) <= turn_angle:
        edge = edge.drop(row)

    # drop same street names
    if edge['street_name'][row] == current_edge['street_name']:
        edge = edge.drop(row)

I'd expect it to drop one line from my current dataframe, but instead I'm receiving a KeyError = 0 on the condition line of code - "if edge['street_name']...."

This is honestly beyond me :-) Any ideas why it would work outside the function but not once it's called?


Solution

  • I suspect that on row = 0, this condition is true

    if abs(current_edge['edgeAzimuth_deg'] - edge_az) <= turn_angle:
            edge = edge.drop(row)
    

    so, the row of index# 0 was dropped. The 2nd if condition tried to access index# 0, so it gives KeyError

    if edge['street_name'][row] == current_edge['street_name']:
            edge = edge.drop(row)
    

    Insert print(edge.index) between these 2 if to see whether index# 0 still existing after 1st if