Python|熊猫 dataframe.prod()
Python是一种用于进行数据分析的出色语言,主要是因为以数据为中心的Python包的奇妙生态系统。 Pandas就是其中之一,它使导入和分析数据变得更加容易。
Pandas dataframe.prod()
函数返回所请求轴的乘积值。它将请求轴上的所有元素相乘。默认选择索引轴。
Syntax: DataFrame.prod(axis=None, skipna=None, level=None, numeric_only=None, min_count=0, **kwargs)
Parameters :
axis : {index (0), columns (1)}
skipna : Exclude NA/null values when computing the result.
level : If the axis is a MultiIndex (hierarchical), count along a particular level, collapsing into a Series
numeric_only : Include only float, int, boolean columns. If None, will attempt to use everything, then use only numeric data. Not implemented for Series.
min_count : The required number of valid values to perform the operation. If fewer than min_count non-NA values are present the result will be NA.
Returns : prod : Series or DataFrame (if level specified)
示例 #1:使用prod()
函数查找数据框中列轴上所有元素的乘积。
# 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]})
# Print the dataframe
df
让我们使用dataframe.prod()
函数来查找数据框中每个元素在列轴上的乘积。
# find the product over the column axis
df.prod(axis = 1)
输出 :
示例 #2:使用prod()
函数查找数据框中任何轴的乘积。数据框包含NaN
值。
# importing pandas as pd
import pandas as pd
# Creating the first dataframe
df = pd.DataFrame({"A":[1, 5, 3, 4, 2],
"B":[3, None, 4, 3, 4],
"C":[2, 2, 7, None, 4],
"D":[None, 3, 6, 12, 7]})
# using prod() function to raise each element
# in df1 to the power of corresponding element in df2
df.prod(axis = 1, skipna = True)
输出 :