Yehuda
Yehuda

Reputation: 1893

Remove np.nan from a pd.DataFrame index

I have a dataframe with accident data of some streets:

street accident data

I'd like to remove (or at least select) that first row that is indexed as np.nan. I tried streets.loc[np.nan,:] but that returns a KeyError: nan. I'm not sure how else to specifically select that record.

Other than using pd.DataFrame.iloc[0,:] (which is imprecise as it relies on location rather than index name) how can I select that specific record?

Upvotes: 1

Views: 102

Answers (3)

Tejas_hooray
Tejas_hooray

Reputation: 636

You can do df = df.dropna()

This will remove all rows with at least one nan value.

Optionally, you could also do df.dropna(inplace=True) The parameter inplace just means that you don't have to specify df = df.dropna() and it will modify the original var for you.

You can find more info on this here: pandas.DataFrame.dropna

Upvotes: 1

socially awkward
socially awkward

Reputation: 48

I think there are two options you can do.

  1. You can fill any random value to nan and then select it.

    df.fillna(value={'ON STREET NAME': 'random'}) streets.loc['random',:]

  2. assign another index column, but this can affect your dataframe later.

Upvotes: 1

BENY
BENY

Reputation: 323306

I will do

df = df[df.index.notna()]

Upvotes: 0

Related Questions