📜  Python|熊猫 dataframe.rtruediv()

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

Python|熊猫 dataframe.rtruediv()

Python是一种用于进行数据分析的出色语言,主要是因为以数据为中心的Python包的奇妙生态系统。 Pandas就是其中之一,它使导入和分析数据变得更加容易。

Pandas dataframe.rtruediv()函数用于查找数据帧和其他元素的浮点除法(二元运算符rfloordiv)。此函数与执行 other / dataframe 基本相同,但支持替换其中一个输入中的缺失数据。

示例 #1:使用 rtruediv()函数将系列的每个元素划分为列轴上数据框中的相应值。

Python3
# importing pandas as pd
import pandas as pd
 
# Creating the dataframe
df = pd.DataFrame({"A":[1, 5, 3, 4, 2],
                   "B":[3, 2, 4, 3, 4],
                   "C":[2, 2, 7, 3, 4],
                   "D":[4, 3, 6, 12, 7]},
                    index =["A1", "A2", "A3", "A4", "A5"])
 
# Print the dataframe
df


Python3
# importing pandas as pd
import pandas as pd
 
# Create the series
sr = pd.Series([12, 25, 64, 18], index =["A", "B", "C", "D"])
 
# Print the series
sr


Python3
# equivalent to sr / df
df.rtruediv(sr, axis = 1)


Python3
# importing pandas as pd
import pandas as pd
 
# Creating the first dataframe
df1 = pd.DataFrame({"A":[1, 5, 3, 4, 2],
                    "B":[3, 2, 4, 3, 4],
                    "C":[2, 2, 7, 3, 4],
                    "D":[4, 3, 6, 12, 7]},
                     index =["A1", "A2", "A3", "A4", "A5"])
 
# Creating the second dataframe
df2 = pd.DataFrame({"A":[10, 11, 7, 8, 5],
                    "B":[21, 5, 32, 4, 6],
                    "C":[11, 21, 23, 7, 9],
                    "D":[1, 5, 3, 8, 6]},
                     index =["A1", "A2", "A3", "A4", "A5"])
 
# Print the first dataframe
print(df1)
 
# Print the second dataframe
print(df2)


Python3
# divide df2 by df1
df1.rtruediv(df2)


让我们创建系列

Python3

# importing pandas as pd
import pandas as pd
 
# Create the series
sr = pd.Series([12, 25, 64, 18], index =["A", "B", "C", "D"])
 
# Print the series
sr

让我们使用 dataframe.rtruediv()函数将系列中的每个元素与数据框中的相应元素分开。

Python3

# equivalent to sr / df
df.rtruediv(sr, axis = 1)

输出 :

示例 #2:使用 rtruediv()函数将数据框中的每个元素与其他数据框中的相应元素相除

Python3

# importing pandas as pd
import pandas as pd
 
# Creating the first dataframe
df1 = pd.DataFrame({"A":[1, 5, 3, 4, 2],
                    "B":[3, 2, 4, 3, 4],
                    "C":[2, 2, 7, 3, 4],
                    "D":[4, 3, 6, 12, 7]},
                     index =["A1", "A2", "A3", "A4", "A5"])
 
# Creating the second dataframe
df2 = pd.DataFrame({"A":[10, 11, 7, 8, 5],
                    "B":[21, 5, 32, 4, 6],
                    "C":[11, 21, 23, 7, 9],
                    "D":[1, 5, 3, 8, 6]},
                     index =["A1", "A2", "A3", "A4", "A5"])
 
# Print the first dataframe
print(df1)
 
# Print the second dataframe
print(df2)

让我们执行 df2 / df1

Python3

# divide df2 by df1
df1.rtruediv(df2)

输出 :