I want to get rid of some records with NaNs. This works perfectly:
df.dropna(axis=0, how='any',inplace=True)
However, it changes the shape of my dataframe, and the index is no longer uniformly spaced. Therefore, I'd like to replace all items in these rows with np.nan
. Is there a simple way to do this?
I was thinking about resampling the dataframe after dropna
, but that only seems to work with a prescribed interval, whereas I would rather use the original index. Another approach would be to loop over the dataframe with iterrows
, but that also feels cumbersome.
下面的命令选择具有等于 Nan 的任何值的所有行,并将 NaN 分配给这些行的其余部分。
df.loc[df.isnull().any(axis=1), :] = np.nan
使用此代码也无需切片df = df.replace('nan',np.nan)
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.