-1

I have a dataframe that has negative values. I would like to replace the negative values to NaN. The dataframe contains different column types (Object, float, int..) and has over 15 numeric columns so I am looking for a solution that would identify the numeric columns then change the negative values to NaN.

name                sodium   potass    .....
Natural Bran         130      280      .....
All-Bran             -1       330      .....
Almond Delight      200       -5       .....
Tex                 120       -2       ..... 

Thank you

user11427018
  • 129
  • 1
  • 6

2 Answers2

2

Using pandas.mask could solve your problem.

df = df.mask(df < 0) # default replaced value is nan when the condition is fulfilled.

To only apply it on numeric columns, we can select those columns by checking their dtypes.

df.select_dtypes(include=np.number)
np.issubdtype(x.dtype, np.number)

So the final solution is:

import pandas as pd
import numpy as np

df = pd.DataFrame({"a": [-1, 3, 4], "b": [3.2, 2.3, -2.3], "c": ["a", "b", "c"]})
df2 = df.apply(lambda x: x.apply(lambda y: np.nan if y < 0 else y) if np.issubdtype(x.dtype, np.number) else x)
print(df2)

output:

     a    b  c
0  NaN  3.2  a
1  3.0  2.3  b
2  4.0  NaN  c
hide1nbush
  • 759
  • 2
  • 14
  • See this [thread](https://stackoverflow.com/questions/27759084/how-to-replace-negative-numbers-in-pandas-data-frame-by-zero) for more solutions – hide1nbush Nov 06 '22 at 06:20
  • Hello! thanks for your help. I initially tried the above code and df[df < 0] = np.nan. However, I get the following error: '<' not supported between instances of 'str' and 'int' . Is there a way to only look at numeric columns? – user11427018 Nov 06 '22 at 16:55
  • @user11427018 have updated the answer, hope it helps. – hide1nbush Nov 07 '22 at 02:43
1

This also works

import numpy as np

df[df < 0] = np.nan

if you want to make changes in particular cols

df[df['sodium'] < 0] = np.nan
  • When I apply this code, the entire row where there is a negative value changes to nan. Is there a way to avoid that? – user11427018 Nov 06 '22 at 17:48