📜  Python|熊猫 DataFrame.abs()

📅  最后修改于: 2022-05-13 01:55:29.086000             🧑  作者: Mango

Python|熊猫 DataFrame.abs()

Python是一种用于进行数据分析的出色语言,主要是因为以数据为中心的Python包的奇妙生态系统。 Pandas就是其中之一,它使导入和分析数据变得更加容易。
dataframe.abs()是最简单的 pandas 数据框函数之一。它返回一个取绝对值的对象,它只适用于所有数字的对象。它也不适用于任何Nan值。 abs()函数也可以与复数一起使用以找到它们的绝对值。
对于复数,绝对值定义为:

Syntax:  DataFrame.abs()
Returns: type of caller

有关代码中使用的 CSV 文件的链接,请单击此处
示例 #1:在 nba.csv 文件中将球队“波士顿凯尔特人队”替换为“欧米茄勇士队”

Python3
# importing pandas as pd
import pandas as pd
 
# Making data frame from the csv file
df = pd.read_csv("nba.csv")
 
# Printing the first 10 rows of the
# data frame for visualization
df[:10]


Python3
# This will set the Number column
# to be all negative.
df.Number = df.Number*(-1)


Python3
# Applying abs() value on one column only
df.Number.abs()


Python3
# Importing pandas as pd
import pandas as pd
 
# Creating a series
ser = pd.Series([1.2 + 1j, 2 + 5j, 1 + 8j, 3.2 + 2j])
 
# let's print the values in series
ser


Python3
# Using abs() function to find the
# absolute value of the complex numbers
absolute_values = s.abs()
 
# Print the absolute values of all complex numbers
absolute_values


为了找到绝对值,我们还需要在数据框中有负值。因此,为了演示目的,让我们将一些值更改为负数。

Python3

# This will set the Number column
# to be all negative.
df.Number = df.Number*(-1)

输出:

现在让我们使用 abs()函数仅查找 Number 列的绝对值。

Python3

# Applying abs() value on one column only
df.Number.abs()

输出:


示例 #2:将 abs() 应用于复数序列。

Python3

# Importing pandas as pd
import pandas as pd
 
# Creating a series
ser = pd.Series([1.2 + 1j, 2 + 5j, 1 + 8j, 3.2 + 2j])
 
# let's print the values in series
ser

Python3

# Using abs() function to find the
# absolute value of the complex numbers
absolute_values = s.abs()
 
# Print the absolute values of all complex numbers
absolute_values