我有一個熊貓資料框,資料框中的一列具有這些值。
df['column'] = [84.0, 85.0, 75.0, nan, 51.0, 50.0, 70.0, 85.0 ... ]
我正在嘗試獲取在間隔之間獲取值的頻率,例如
freq = {
15 : 40, # number of values between 10 and 20 were 40. (mean taken to be 15)
25 : 47, # number of values between 20 and 30 were 47. (mean taken to be 25)
...
}
pandas 中是否有任何特定功能可以執行這種操作,而不是創建一個 for 回圈并檢查每個值并增加 freq 字典中的計數?
[編輯]我的目標是得到這樣的字典,然后NaN用freq.keys()freq.values()
謝謝
uj5u.com熱心網友回復:
# create intervals
bins = pd.interval_range(0, 100, freq=10)
# assign each value in df["column"] to bin and count bin occurences
counts = pd.cut(df["column"], bins).value_counts()
# create a Series, indexed by interval midpoints and convert to dictionary
pd.Series(counts.values, index=bins.mid).to_dict()
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/435758.html
標籤:Python 熊猫 数据框 数数 熊猫-groupby
