Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python pandas.DataFrame: Make whole row NaN according to condition

I want to make the whole row NaN according to a condition, based on a column. For example, if B > 5, I want to make the whole row NaN.

Unprocessed data frame looks like this:

   A  B
0  1  4
1  3  5
2  4  6
3  8  7

Make whole row NaN, if B > 5:

     A    B
0  1.0  4.0
1  3.0  5.0
2  NaN  NaN
3  NaN  NaN

Thank you.

like image 950
s900n Avatar asked Sep 29 '17 12:09

s900n


People also ask

How do I change row values based on conditions in pandas?

You can replace values of all or selected columns based on the condition of pandas DataFrame by using DataFrame. loc[ ] property. The loc[] is used to access a group of rows and columns by label(s) or a boolean array. It can access and can also manipulate the values of pandas DataFrame.

How do I get rid of rows on conditions in pandas?

Use pandas. DataFrame. drop() method to delete/remove rows with condition(s).


2 Answers

Use boolean indexing for assign value per condition:

df[df['B'] > 5] = np.nan
print (df)
     A    B
0  1.0  4.0
1  3.0  5.0
2  NaN  NaN
3  NaN  NaN

Or DataFrame.mask which add by default NaNs by condition:

df = df.mask(df['B'] > 5)
print (df)
     A    B
0  1.0  4.0
1  3.0  5.0
2  NaN  NaN
3  NaN  NaN

Thank you Bharath shetty:

df = df.where(~(df['B']>5))
like image 127
jezrael Avatar answered Nov 14 '22 23:11

jezrael


You can also use df.loc[df.B > 5, :] = np.nan


Example

In [14]: df
Out[14]: 
   A  B
0  1  4
1  3  5
2  4  6
3  8  7

In [15]: df.loc[df.B > 5, :] = np.nan 

In [16]: df
Out[16]: 
     A    B
0  1.0  4.0
1  3.0  5.0
2  NaN  NaN
3  NaN  NaN

in human language df.loc[df.B > 5, :] = np.nan can be translated to:

assign np.nan to any column (:) of the dataframe ( df ) where the condition df.B > 5 is valid.

like image 24
Mohamed Ali JAMAOUI Avatar answered Nov 14 '22 23:11

Mohamed Ali JAMAOUI