Я работаю в python3 - я пытаюсь определить среднее значение из измерений в словаре загрязняющих веществ в скважине JSON.Когда я возвращаю код, он показывает среднее значение данных для каждой строки.По сути, я хочу найти одно средство для всех результатов одного загрязнителя.Есть несколько результатов для одного и того же загрязнителя в течение каждого года.
for plants in data:
for year in ["2010", "2011", "2012", "2013", "2014":
arsenic_values = []
manganese_values = []
all_year_data = data[plants][year]
for measurement in all_year_data:
if measurement['contaminent'] == "arsenic":
arsenic_values.append(float(measurement["concentration"]))
arsenic_mean = statistics.mean(arsenic_values)
print(plants, year, arsenic_mean)
Вот пример того, как JSON выглядит в течение 2 лет.
"well1": {
"2010": [],
"2011": [
{
"contaminent": "arsenic",
"concentration": "0.0420000000"
},
{
"contaminent": "arsenic",
"concentration": "0.0200000000"
},
{
"contaminent": "arsenic",
"concentration": "0.0150000000"
},
{
"contaminent": "arsenic",
"concentration": "0.0320000000"
},
{
"contaminent": "manganese",
"concentration": "0.8700000000"
},
{
"contaminent": "manganese",
"concentration": "0.8400000000"
}
],
Example of what it returns with my notes in ()
well1 2011 0.042
well1 2011 0.031 (this is the mean of the measurement before)
well1 2011 0.025666666666666667 (this is the mean of the measurement before and before that)
well1 2011 0.0272 (**THIS IS WHAT I WANT** but I can't write like a counter function because the result I want is different for each well I am looking at.
IN summation:
There are multiple results for each year of the same containment and I want to find the average. But my code as it is written returns almost a triangular data that grows with each line. SO its finding's the average of each line for the containment rather than grouping all together and taking one average.