0

I am trying to replace all the values in my dataframe (for specific columns) that are < 0 with a value of 0.

I have tried this code, but it does not seem to be working for me:

df.loc[df[['col_1','col_2','col_3']] < 0, 'col_1','col_2','col_3'] = 0

When using this line of code, I get the following error:

AttributeError: 'int' object has no attribute 'loc'

I am not sure whether it is the code I am using that is halting me from accomplishing what I want to do.

Therefore, could someone kindly point me in the right direction?

Thank you - please see sample data and expected output below.

Sample data:

col_1     col_2     col_3
--------------------------
  4         5        -1 
 -3        -4         5
  2        -2         2

Anticipated Result:

col_1     col_2     col_3
--------------------------
  4         5         0
  0         0         5
  2         0         2
  • When `df.loc[...]` gives you "'int' object has no attribute 'loc'", it means that `df` is an int at the time when the code has run. Have you checked `df`? – G. Anderson Jan 08 '21 at 21:26

1 Answers1

2

You could use pd.DataFrame.clip to set all values below 0 to 0 (or the threshold of your choice):

df[['col_1','col_2','col_3']] = df[['col_1','col_2','col_3']].clip(lower = 0)

On your sample data this gives:

In [45]: df
Out[45]: 
   col_1  col_2  col_3
0      4      5     -1
1     -3     -4      5
2      2     -2      2

In [46]: df[['col_1','col_2','col_3']] = df[['col_1','col_2','col_3']].clip(lower = 0)

In [47]: df
Out[47]: 
   col_1  col_2  col_3
0      4      5      0
1      0      0      5
2      2      0      2
sacuL
  • 49,704
  • 8
  • 81
  • 106
  • Thanks for the solution - but if I had like over 30 columns, and I wanted to use this method for only 3/36 columns, what would be the most efficient way of doing it? –  Jan 08 '21 at 21:47
  • 2
    I think the same: just include a list of the 3/36 columns, instead of all of them. for example, in your dataframe if you wanted to only clip `col_1` and `col_3`, leaving the rest intact, you'd do: `df[['col_1', 'col_3']] = df[['col_1', 'col_3']].clip(lower=0)` – sacuL Jan 08 '21 at 22:01
  • thank you very much for your help! It has been very helpful, thanks. :) –  Jan 08 '21 at 23:48