Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas replace all items in a row with NaN if one value is NaN

Tags:

python

pandas

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.

like image 370
Peter9192 Avatar asked May 02 '16 15:05

Peter9192


People also ask

Does Dropna work on NaN?

You can remove missing values ( NaN ) from pandas. DataFrame , Series with dropna() .


2 Answers

The command below selects all rows with any value equal to Nan, and assigns NaNs to the rest of those rows.

df.loc[df.isnull().any(axis=1), :] = np.nan
like image 125
Alexander Avatar answered Nov 06 '22 13:11

Alexander


using this code also works without slicingdf = df.replace('nan',np.nan)

like image 44
Nii Joshua Avatar answered Nov 06 '22 14:11

Nii Joshua