1

I have a DataFrame (20k rows) with 2 columns I would like to update if the first column (latitude) row entry is NaN. I wanted to use the code below as it might be a fast way of doing it, but I'm not sure how to update this line msk = [isinstance(row, float) for row in df['latitude'].tolist()] to get the rows that are NaN only. The latitude column I am doing the check on is float, so this line of code returns all rows.

def boolean_mask_loop(df):

    msk = [isinstance(row, float) for row in df['latitude'].tolist()]

    out = []
    for target in df.loc[msk, 'address'].tolist():
        dict_temp = geocoding(target)
        out.append([dict_temp['lat'], dict_temp['long']])

    df.loc[msk, ['latitude', 'longitude']] = out
    
    return df
id address latitude longitude
1 addr1 NaN NaN
2 addr2 NaN NaN
3 addr3 40.7526 -74.0016
3
  • 2
    This question is similar to: How to find which columns contain any NaN value in Pandas dataframe. If you believe it’s different, please edit the question, make it clear how it’s different and/or how the answers on that question are not helpful for your problem. Commented Oct 26, 2024 at 19:42
  • Thank you. Would that work through the code in relation to this line: msk = [isinstance(row, float) for row in df['latitude'].tolist()] Commented Oct 26, 2024 at 20:09
  • Test it and see if it works! Commented Oct 26, 2024 at 20:46

1 Answer 1

0

I amended this line of code msk = [isinstance(row, float) for row in df['latitude'].tolist()] to msk = df['latitude'].isnull().tolist() which essentially identifies those rows that have NaN values in the latitude column that I wanted to update. Thank you for the advice from @Anerdw.

Sign up to request clarification or add additional context in comments.

2 Comments

If the duplicate target solved your problem, I would rather you accept the duplicate than self-answer. But glad you found a solution!
I'm not sure it really did, but maybe it helped a little. Just wanted to give some credit.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.