[英]Get most frequent value and std based on precentile data in a pandas dataframe
這一半的統計題其實,
我有一個由 9 列組成的數據框。 根據多次測量,每一個都表示一個蘋果的不同直徑。 行是百分比:
55 60 65 70 75 80 85 90 95
0 0.0, 0.0 10.73 39.13 8.35 8.63 33.16 0.0 0.0
1 3.42 0.0 1.28 26.09 31.3 26.73 8.07 3.11 0.0
我正在嘗試繪制某種分布圖,同時獲取帶有 STD 的蘋果的平均直徑。 自己實在想不出好辦法。 有任何想法嗎?
編輯:
barplot 生成了一個不錯的圖表,但我希望能夠獲得最頻繁的值和標准偏差。
b=sns.barplot(data=df)
我嘗試使用模式,但它不起作用。
df.mode()
這是我用於模式計算的例程,也許它對您也有用。 請注意,它在一個范圍內尋找模式(均值偏移、均值+偏移),因此不適用於非常偏斜的分布(由shift定義的容差)。
from scipy.stats.kde import gaussian_kde
def get_mode(s0: pd.Series):
"""Get mode from non-parametric Gaussian fit."""
# Non-parametric pdf.
s = s0.copy()
mean = s.mean()
shift = 20
lower, upper = mean - shift, mean + shift
x = np.linspace(lower, upper, 250)
# Cut-off the upper and lower margins.
s = s.dropna()[(s < upper) & (s > lower)]
try:
nparam_density = gaussian_kde(s.ravel())
except ValueError:
# Not enough data: return mean.
return mean
nparam_density_vec = nparam_density(x)
max_hist = x[nparam_density_vec.argmax()]
return max_hist
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.