Python|熊猫 dataframe.mul()
Python是一种用于进行数据分析的出色语言,主要是因为以数据为中心的Python包的奇妙生态系统。 Pandas就是其中之一,它使导入和分析数据变得更加容易。
Pandas dataframe.mul()函数返回数据帧和其他元素的乘法。此函数本质上与数据帧 * other 执行相同的操作,但它提供了额外的支持来处理其中一个输入中的缺失值。
Syntax: DataFrame.mul(other, axis=’columns’, level=None, fill_value=None)
Parameters :
other : Series, DataFrame, or constant
axis : For Series input, axis to match Series index on
level : Broadcast across a level, matching Index values on the passed MultiIndex level
fill_value : Fill existing missing (NaN) values, and any new element needed for successful DataFrame alignment, with this value before computation. If data in both corresponding DataFrame locations is missing the result will be missing
Returns : result : DataFrame
示例 #1:使用 mul()函数查找数据帧与序列的乘积。
注意:对于与系列相乘,用于相乘的数据框轴必须与系列索引相匹配。
Python3
# importing pandas as pd
import pandas as pd
# Creating the first dataframe
df1=pd.DataFrame({"A":[14,4,5,4,1],
"B":[5,2,54,3,2],
"C":[20,20,7,3,8],
"D":[14,3,6,2,6]})
# Print the dataframe
df1
Python3
# importing pandas as pd
import pandas as pd
# create series
sr = pd.Series([3, 2, 4, 5, 6])
# Print series
sr
Python3
# find multiplication over the index axis
df1.mul(sr, axis = 0)
Python3
# importing pandas as pd
import pandas as pd
# Creating the first dataframe
df1=pd.DataFrame({"A":[14,4,5,4,1],
"B":[5,2,54,3,2],
"C":[20,20,7,3,8],
"D":[14,3,6,2,6]})
# Creating the second dataframe with Na
value
df2=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]})
# Print the second dataframe
df2
Python3
# fill the missing values with 100
df1.mul(df2, fill_value = 100)
让我们创建系列
Python3
# importing pandas as pd
import pandas as pd
# create series
sr = pd.Series([3, 2, 4, 5, 6])
# Print series
sr
让我们使用 dataframe.mul()函数来执行乘法
Python3
# find multiplication over the index axis
df1.mul(sr, axis = 0)
输出 :
示例 #2:使用 mul()函数查找两个数据帧的乘积。一个数据帧包含 NA 值。
Python3
# importing pandas as pd
import pandas as pd
# Creating the first dataframe
df1=pd.DataFrame({"A":[14,4,5,4,1],
"B":[5,2,54,3,2],
"C":[20,20,7,3,8],
"D":[14,3,6,2,6]})
# Creating the second dataframe with Na
value
df2=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]})
# Print the second dataframe
df2
让我们使用 dataframe.mul()函数来查找两个数据帧的乘积,同时处理缺失值。
Python3
# fill the missing values with 100
df1.mul(df2, fill_value = 100)
输出 :
请注意,所有缺失值单元格在乘法之前都已填充 100