10

If I have the following DataFrame:

>>> df_1 = pd.DataFrame({'A':[-1, 2,  3],
                         'B':[4, -5,  6],
                         'C':[7,  8, -9]}, 
                          index=pd.to_datetime(['2017-01-01 00:01:00', '2017-01-01 00:02:00', '2017-01-02 00:01:00']))

>>> df_1
                      A   B   C
2017-01-01 00:01:00  -1   4   7
2017-01-01 00:02:00   2  -5   8
2017-01-02 00:01:00   3   6  -9

How would I replace all of the negative values in a specific column with something else? For example, if I want to replace all of the negative values in the 'B' column but none of the others with, say 0, the following would be my result.

>>> df_2
                      A   B   C
2017-01-01 00:01:00  -1   4   7
2017-01-01 00:02:00   2   0   8
2017-01-02 00:01:00   3   6  -9
1
  • 5
    Does df_1['B'][df_1['B'] < 0] = 0 work for you? Commented Apr 5, 2018 at 21:01

5 Answers 5

10

I think you can using mask

df_1.B=df_1.B.mask(df_1.B.lt(0),0)
df_1
Out[1437]: 
                     A  B  C
2017-01-01 00:01:00 -1  4  7
2017-01-01 00:02:00  2  0  8
2017-01-02 00:01:00  3  6 -9

If we combine with fillna ()Assuming different columns should fill will different value)

df_1.mask(df_1.lt(0)).fillna({'A':9999,'B':0,'C':-9999})
Out[1440]: 
                          A    B       C
2017-01-01 00:01:00  9999.0  4.0     7.0
2017-01-01 00:02:00     2.0  0.0     8.0
2017-01-02 00:01:00     3.0  6.0 -9999.0
Sign up to request clarification or add additional context in comments.

Comments

9

If the values in column B are to be replaced with 0:

df_1.loc[df_1['B']<0,'B']=0

Comments

3

You can use numpy.where for this. Add the following to your code to change values of column B for example:

df_1.B = np.where(df_1.B < 0, 0, df_1.B)

print(df_1)

Comments

2

Another way, very similar to what @bhansa 's answer was going for, but using pandas.Series.lt:

df_1.B[df_1.B.lt(0)] = 0

>>> df_1
                     A  B  C
2017-01-01 00:01:00 -1  4  7
2017-01-01 00:02:00  2  0  8
2017-01-02 00:01:00  3  6 -9

Comments

-2

Normal conditional will work for you:

df_1[df_1['B'] < 0] = 0


                     A  B   C
2017-01-01 00:01:00 -1  4   7
2017-01-01 00:02:00  2  0   8
2017-01-02 00:01:00  3  6   -9

2 Comments

This will not work, I believe. If I understand correctly, this will mask the second row, then set all of the entries in that row to 0, instead of just the 'B' column.
df_1['B'][df_1['B'] < 0] = 0 would fix it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.