简体   繁体   中英

Remove np.nan from a pd.DataFrame index

I have a dataframe with accident data of some streets:

街道事故数据

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?

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.

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

我会做

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM