Итерируя по годам - PullRequest
1 голос
/ 14 марта 2020

Я хочу задать вопрос еще раз. У меня есть список списков, как показано ниже.

[('2017-12-01', ['5', '6', '0', False]), 
 ('2017-12-02', ['5', '7', '0', False]), 
 ('2017-12-03', ['6', '7', '0.5', True]), 
 ('2017-12-04', ['6', '7', '0.5', True]), 
 ('2017-12-05', ['5', '6', '0.4', True]), 
 ('2018-01-01', ['5', '6', '0', False]), 
 ('2018-01-02', ['5', '6', '0', False])]

индекс 0 - это дата. Я хотел создать словарь для каждого года, который показывает среднее значение первый и второй столбец, где ответ будет выглядеть как {2017:[5.4,6.6]2018:[5,6]}

1 Ответ

1 голос
/ 14 марта 2020

вы можете использовать collection.defaultdict с statistics.mean :

from collections import defaultdict
from statistics import mean

l = [('2017-12-01', ['5', '6', '0', False]), 
     ('2017-12-02', ['5', '7', '0', False]), 
     ('2017-12-03', ['6', '7', '0.5', True]), 
     ('2017-12-04', ['6', '7', '0.5', True]), 
     ('2017-12-05', ['5', '6', '0.4', True]), 
     ('2018-01-01', ['5', '6', '0', False]), 
     ('2018-01-02', ['5', '6', '0', False])]

my_dict = defaultdict(lambda : [[], []])

for d, v  in l:
    y = int(d[:4])
    my_dict[y][0].append(float(v[0]))
    my_dict[y][1].append(float(v[1]))

result = {k: [mean(e) for e in v] for k, v in my_dict.items()}
result

вывод:

{2017: [5.4, 6.6], 2018: [5.0, 6.0]}

также , вы можете сначала использовать pandas

1) , вам необходимо преобразовать данные в pandas.DataFrame:

import pandas as pd

df = pd.DataFrame([[f, *map(float, s[:2])] for f, s in l], columns=['date', 'col0', 'col1'])
df['date']= pd.to_datetime(df['date']) 
df

:

enter image description here

2 ) теперь вы можете манипулировать своим pd.DataFrame, используя pandas .Dataframe.groupby чтобы получить желаемый результат:

df.groupby(df.date.dt.year).mean().transpose().to_dict('l')

выход:

{2017: [5.4, 6.6], 2018: [5.0, 6.0]}

, поскольку вам требуется более простой подход, который вы можете использовать:

# group col0 and col1 values base on the year
year_cols = {}
for date, cols in l:
    # the year is in the first 4 characters so using a slice will get the year
    # then convert to integer
    year = int(date[:4])

    col0 = cols[0]
    col1 = cols[1]

    # store the values from column 0 and column 1 base on the year
    if year in year_cols: # check if new element/year
        # if not a new elemnt
        year_cols[year]['col0'].append(float(col0)) # convert to float to be able to compute the average
        year_cols[year]['col1'].append(float(col1)) # convert to float to be able to compute the average
    else: # in case of a new element/year
        col01_data = {'col0': [float(col0)], 'col1': [float(col1)]}
        year_cols[year] = col01_data


# get the average for each year on each column 
result = {}
for year, col0_col1 in year_cols.items():
    col0 = col0_col1['col0']
    col1 = col0_col1['col1']

    # compute the average for each column
    # average formula: sum of all elements divided by the number of elemetns
    result[year] = [sum(col0) / len(col0), sum(col1) / len(col1)]

result

вывод:

{2017: [5.4, 6.6], 2018: [5.0, 6.0]}
...