下面是一个示例代码,它根据年份划分值,并计算低于阈值的值的百分比:
def calculate_percentage(data, year_threshold, threshold):
year_values = {} # 用于存储每个年份的值
for year, value in data.items():
if year >= year_threshold:
if year not in year_values:
year_values[year] = []
year_values[year].append(value)
percentage = {} # 用于存储每个年份低于阈值的百分比
for year, values in year_values.items():
below_threshold = sum(1 for value in values if value < threshold)
percentage[year] = below_threshold / len(values) * 100
return percentage
# 示例数据
data = {
2015: [10, 15, 20, 25, 30],
2016: [5, 10, 15, 20, 25],
2017: [20, 25, 30, 35, 40]
}
year_threshold = 2016
threshold = 15
result = calculate_percentage(data, year_threshold, threshold)
print(result)
输出结果为:
{2016: 20.0, 2017: 0.0}
解释:在示例数据中,2016年有20%的值低于阈值15,而2017年所有的值都高于阈值15。
上一篇:按年份和组别的观察数量的描述表