9

In my dataframe I have a column containing numbers, some positive, some negative. Example

    Amount
0  -500
1   659
3   -10
4   344

I want to turn all numbers Df['Amount'] into positive numbers. I thought about multiplying all numbers with *-1. But though this turns negative numbers positive, and also does the reverse.

Is there a better way to do this?

Jasper
  • 1,869
  • 5
  • 24
  • 52

3 Answers3

22

You can assign the result back to the original column:

df['Amount'] = df['Amount'].abs()

Or you can create a new column, instead:

df['AbsAmount'] = df['Amount'].abs()
Tom Lynch
  • 815
  • 5
  • 13
5

You can take absolute value

d['Amount'].apply(abs)
koPytok
  • 3,163
  • 1
  • 10
  • 26
  • It appears I still have a lot to learn about the Python Standard Library, thanks! – Jasper Jan 01 '18 at 20:54
  • 1
    @Jasper This is `pandas`, not the standard library. And once you go down the path of numpy/pandas, you'll never find the end of that path :P – roganjosh Jan 01 '18 at 20:59
2

abs() is the standard way to get absolute values.

Zae Zoxol
  • 99
  • 6