Python Pandas DataFrame.truediv

Python Pandas DataFrame.truediv

Pandas DataFrame是一个二维的大小可变的、可能是异质的表格数据结构,具有标记的axis(行和列)。算术操作在行和列的标签上对齐。它可以被认为是一个类似于Dict的系列对象的容器。这是Pandas的主要数据结构。

Pandas DataFrame.truediv()函数对dataframe和其他元素进行浮动分割。它等同于dataframe/other,但支持用fill_value来替代其中一个输入中的缺失数据。

语法: DataFrame.truediv(other, axis=’columns’, level=None, fill_value=None)

参数:
other:标量、序列、系列或数据框架
axis: {0或’索引’,1或’列’}
level :在一个级别上广播,与通过的MultiIndex级别上的Index值匹配。
fill_value : 填补现有的缺失(NaN)值,以及为成功调整DataFrame所需的任何新元素。

返回:算术运算的结果。

示例#1 :使用DataFrame.truediv()函数对给定的数据框进行元素除法。同时在所有缺失值的位置上填充100。

# importing pandas as pd
import pandas as pd
  
# Creating the DataFrame
df = pd.DataFrame({"A":[12, 4, 5, None, 1], 
                   "B":[7, 2, 54, 3, None], 
                   "C":[20, 16, 11, 3, 8], 
                   "D":[14, 3, None, 2, 6]}) 
  
# Create the index
index_ = ['Row_1', 'Row_2', 'Row_3', 'Row_4', 'Row_5']
  
# Set the index
df.index = index_
  
# Print the DataFrame
print(df)
Python

输出 :
Python Pandas DataFrame.truediv

现在我们将使用DataFrame.truediv()函数对给定的数据框架进行除以2的除法,元素。我们将在这个数据框架中的所有缺失值的位置上填充100。

# divide by 2 element-wise
# fill 100 at the place of missing values
result = df.truediv(other = 2, fill_value = 100)
  
# Print the result
print(result)
Python

输出 :
Python Pandas DataFrame.truediv
正如我们在输出中看到的,DataFrame.truediv()函数已经成功地对给定的数据框架进行了除以标量。

示例#2 :使用DataFrame.truediv()函数,使用列表对给定的数据框架进行分割。

# importing pandas as pd
import pandas as pd
  
# Creating the DataFrame
df = pd.DataFrame({"A":[12, 4, 5, None, 1], 
                   "B":[7, 2, 54, 3, None], 
                   "C":[20, 16, 11, 3, 8], 
                   "D":[14, 3, None, 2, 6]}) 
  
# Create the index
index_ = ['Row_1', 'Row_2', 'Row_3', 'Row_4', 'Row_5']
  
# Set the index
df.index = index_
  
# Print the DataFrame
print(df)
Python

输出 :

Python Pandas DataFrame.truediv

现在我们将使用DataFrame.truediv()函数对给定的数据框架用列表进行分割。

# divide using a list
# across the column axis
result = df.truediv(other = [10, 4, 8, 3], axis = 1)
  
# Print the result
print(result)
Python

输出 :

Python Pandas DataFrame.truediv

正如我们在输出中看到的,DataFrame.truediv()函数已经成功地对给定的数据框架进行了列表除法。

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程

登录

注册