Pandas 中的值范围
Range of values in Pandas
我需要显示我的数据集的一些参数,最后一个是范围,但我找不到任何有用的信息来编写它。如果我没看错的话是
range = max - min
这是我在 jupiter notebook 中的 pandas 函数:
data.groupby("DPI").agg({"SUM_ALL" :["count",pd.Series.mode,"mean","median","min","max"]})
my outcome
如何使用 range
值再添加一列?
您可以尝试定义一个自定义范围函数,例如:
def calc_range(x):
return np.max(x) - np.min(x)
然后将其作为函数传递给 agg:
data.groupby("DPI").agg({"SUM_ALL" :["count",pd.Series.mode,"mean","median","min","max", calc_range]})
您可以直接使用 numpy.ptp()
函数以获得更好的清晰度和效率:
numpy.ptp()
正在做您想要的:
Range of values (maximum - minimum) along an axis.
The name of the function comes from the acronym for ‘peak to peak’.
因此,插入您的代码,我们可以使用:
import numpy as np
data.groupby("DPI").agg({"SUM_ALL" :["count",pd.Series.mode,"mean","median","min","max", np.ptp]})
我需要显示我的数据集的一些参数,最后一个是范围,但我找不到任何有用的信息来编写它。如果我没看错的话是
range = max - min
这是我在 jupiter notebook 中的 pandas 函数:
data.groupby("DPI").agg({"SUM_ALL" :["count",pd.Series.mode,"mean","median","min","max"]})
my outcome
如何使用 range
值再添加一列?
您可以尝试定义一个自定义范围函数,例如:
def calc_range(x):
return np.max(x) - np.min(x)
然后将其作为函数传递给 agg:
data.groupby("DPI").agg({"SUM_ALL" :["count",pd.Series.mode,"mean","median","min","max", calc_range]})
您可以直接使用 numpy.ptp()
函数以获得更好的清晰度和效率:
numpy.ptp()
正在做您想要的:
Range of values (maximum - minimum) along an axis.
The name of the function comes from the acronym for ‘peak to peak’.
因此,插入您的代码,我们可以使用:
import numpy as np
data.groupby("DPI").agg({"SUM_ALL" :["count",pd.Series.mode,"mean","median","min","max", np.ptp]})