-3

I have one column df with mix of positive and negative number, as mentioned below:

Value
100
564
-789
3637
-999

I want to make a new df by adding the number 3600 to the ONLY negative value and my new df should have values like:

Value
100
564
2811
3637
2601

Please help me with the python code for this.

cs95
  • 379,657
  • 97
  • 704
  • 746
  • 2
    `np.where(df['Value'].lt(0), df['Value'].add(3600), df['Value'])` lots of ways to do this, not sure it warrants a question as its basic if/where filtering – Umar.H Jul 02 '20 at 21:21

4 Answers4

1
df.loc[df['a']<0, 'a'] += 3600
warped
  • 8,947
  • 3
  • 22
  • 49
1

It could be written as follow:

import numpy as np
x=np.array([100,564, -789, 3637, -999])
x[x < 0] = x[x < 0] + 3600
    
print(x)

Good luck

Pygin
  • 907
  • 6
  • 12
0

Here is how:

df['Value'] = [n+3600 if n < 0 else n for n in df['Value']]
Red
  • 26,798
  • 7
  • 36
  • 58
-1

To accomplish this we can loop through the list for each number and if the number is less than 0 then add in 3600. Here is the code for this:

values = [100, 564, -789, 3637, -999]
for value in values:
  if value < 0:
    value += 3600

This is the code. To print out the result, just run:

print(values)
Saiansh Singh
  • 583
  • 5
  • 16