在 Pandas 数据框列中找到一个区间内值的频率
Find the ferquency of values within an interval in Pandas Dataframe Column
我有一个 pandas 数据框,数据框中的一列具有这些值。
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 字典中的计数?
[编辑]我的目标是得到这样的字典,然后用freq.keys()
替换NaN
,比例为freq.values()
谢谢
# 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()
我有一个 pandas 数据框,数据框中的一列具有这些值。
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 字典中的计数?
[编辑]我的目标是得到这样的字典,然后用freq.keys()
替换NaN
,比例为freq.values()
谢谢
# 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()