Pandas - 将混合正/负数列变为正数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/48053139/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 05:00:04  来源:igfitidea点击:

Pandas - Turning column of mixed positive / negative numbers to positive

pythonpandas

提问by Jasper

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.

我想把所有数字 Df['Amount'] 变成正数。我想过将所有数字乘以 *-1。但是,尽管这会将负数变为正数,但也会反过来。

Is there a better way to do this?

有一个更好的方法吗?

回答by Tom Lynch

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()

回答by koPytok

You can take absolute value

你可以取绝对值

d['Amount'].apply(abs)

回答by Zae Zoxol

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

abs() 是获取绝对值的标准方法。