10

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
  • 2,131
  • 6
  • 29
  • 61

3 Answers3

26

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
  • 893
  • 6
  • 13
5

You can take absolute value

d['Amount'].apply(abs)
koPytok
  • 3,453
  • 1
  • 14
  • 29
  • It appears I still have a lot to learn about the Python Standard Library, thanks! – Jasper Jan 01 '18 at 20:54
  • 2
    @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
  • 109
  • 6