亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

繪制 Panda 數據幀子集的均值

繪制 Panda 數據幀子集的均值

收到一只叮咚 2021-12-21 11:01:39
假設有一大組數據,例如   Height (m)  My data0          18      5.01          25      6.02          10      1.03          13      1.54          32      8.05          26      6.76          23      5.07           5      2.08           7      2.0我想繪制“我的數據”的平均值(如果可能的話,標準偏差)作為高度的函數,在范圍 [0,5),[5,10),[10,15) 和很快。任何的想法?我嘗試了不同的方法,但都不起作用
查看完整描述

2 回答

?
繁華開滿天機

TA貢獻1816條經驗 獲得超4個贊

如果我理解正確的話:


# Precompute bins for pd.cut

bins = list(range(0, df['Height (m)'].max() + 5, 5))


# Cut Height into intervals which exclude the right endpoint, 

# with bin edges at multiples of 5

df['HeightBin'] = pd.cut(df['Height (m)'], bins=bins, right=False)


# Within each bin, get mean, stdev (normalized by N-1 by default),

# and also show sample size to explain why some std values are NaN

df.groupby('HeightBin')['My data'].agg(['mean', 'std', 'count'])

            mean       std  count

HeightBin

[0, 5)       NaN       NaN      0

[5, 10)     2.00  0.000000      2

[10, 15)    1.25  0.353553      2

[15, 20)    5.00       NaN      1

[20, 25)    5.00       NaN      1

[25, 30)    6.35  0.494975      2

[30, 35)    8.00       NaN      1


查看完整回答
反對 回復 2021-12-21
?
慕俠2389804

TA貢獻1719條經驗 獲得超6個贊

如果我理解正確,這就是您想要做的:


import pandas as pd

import numpy as np


bins = np.arange(0, 30, 5) # adjust as desired


df_stats = pd.DataFrame(columns=['mean', 'st_dev']) # DataFrame for the results

df_stats['mean'] = df.groupby(pd.cut(df['Height (m)'], bins, right=False)).mean()['My data']

df_stats['st_dev'] = df.groupby(pd.cut(df['Height (m)'], bins, right=False)).std()['My data']


查看完整回答
反對 回復 2021-12-21
  • 2 回答
  • 0 關注
  • 161 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號