AssertionError: Gaps in blk ref_locs when attempting to drop rows with `np.nan`

Category: python pandas (1 Views)

I have a pandas DataFrame of shape 12k * 150. One of the columns is Lat.

import pandas as pd

df = pd.DataFrame({
                   'id': [0, 1, 2, 3],
                   'Lat': [83.21, np.nan, np.nan, 83.42],
                   'Lon' [-19.21, np.nan, np.nan, -20.56]
                 })

This is for illustration not a reproducible example, as my DataFrame is fairly large and I don't exactly know what's causing the issue.

I am trying to drop the rows with np.nan and get this error when I tried, drop.na, mask = (df['Lat'].isna()) and other methods.

🔴 No definitive solution yet

📌 Solution 1

use dropna with a subset attribute

if you need to include other columns to consider, add those in the subset list.

df.dropna(subset=['Lat'])

alternately,

df.loc[df['Lat'].notna()]
    id  Lat     Lon
0   0   83.21   -19.21
3   3   83.42   -20.56