【问题标题】:Iterating through the years岁月迭代
【发布时间】:2020-03-14 09:47:23
【问题描述】:

我想再问一次。我有一个如下列表。

[('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]}

【问题讨论】:

    标签: python list iteration


    【解决方案1】:

    您可以将collections.defaultdictstatistics.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
    

    输出:

    2) 现在您可以使用pandas.Dataframe.groupby 操作您的pd.DataFrame 以获得所需的输出:

    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]}
    

    【讨论】:

    • 谢谢你。有没有其他简单的方法来回答它,因为我是入门级的
    • @NuruahmedJemal 我添加了一个更简单的解决方案,它适合你的水平吗?
    • @NuruahmedJemal 如果这解决了您的问题,您可以接受答案,谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-08-01
    • 1970-01-01
    • 2021-10-20
    • 2015-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多