【问题标题】:Computing the days value to monthly wise in pandas for datsets [duplicate]为数据集计算 Pandas 中每月明智的天数 [重复]
【发布时间】:2019-07-30 09:03:40
【问题描述】:

我是熊猫新手。我有一个大型数据集,其中包含每天的温度值。我需要按月计算温度,即

这是我的数据集 csv 结构:

我需要转换成以下 csv 结构:

我正在考虑以下方法:

for(year=2012;year<=2018;year++)
   for(month=1;month<=12;month++)
      for(day=1;day<=31;day++)
         summax+=Temp_max[day]
         summin+=Temp_min[day]
      summax/=day
      summin/=day
      print(summax,summin)

但我不知道如何在 pandas/python 中执行此操作,如何在循环中获取列值,以及如何处理 2 月天(如 28 天、30 天、31 天)并带来预期的输出或类似的输出。任何帮助,将不胜感激。谢谢!!

【问题讨论】:

    标签: python pandas numpy dataframe


    【解决方案1】:

    在 pandas 中使用 read_csv 读取您的 csv 文件

    供您使用groupby

    import pandas as pd
    
    data = {'year': [*np.repeat(2012, 9), 2018],
            'month': [*np.repeat(1, 4), *np.repeat(2, 3), *np.repeat(3, 2), 12],
            'day': [1, 2, 3, 31, 1, 2, 28, 1, 2, 31],
            'Temp max': [28, 26, 27, 26, 27, 26, 26, 26, 25, 26],
            'Temp min': [19, 18, 17, 19, 18, 18, 18, 18, 18, 28]}
    
    df = pd.DataFrame(data)
    # df = pd.read_csv('file.csv')
    
    df2 = df.groupby(['year', 'month'])['Temp max', 'Temp min'].mean()
    print(df2)
    

    输出:

                 Temp max  Temp min
    year month                     
    2012 1      26.750000     18.25
         2      26.333333     18.00
         3      25.500000     18.00
    2018 12     26.000000     28.00
    

    如果你想全年使用:

    df2 = df.groupby(['year', 'month'])['Temp max', 'Temp min'].mean().reset_index()
    
       year  month   Temp max  Temp min
    0  2012      1  26.750000     18.25
    1  2012      2  26.333333     18.00
    2  2012      3  25.500000     18.00
    3  2018     12  26.000000     28.00
    

    【讨论】:

    • 您应该将.mean() 替换为.agg({'Temp max': 'max', 'Temp min': 'min'}) 以获得所需的输出。
    • 是的,这是因为 arya 给出了那个月的平均温度方程 @muzzyq
    • ['Temp max', 'Temp min'] 也是多余的
    • 我的错误,我会更新我的答案。
    【解决方案2】:

    输入: 根据问题的样本数据

    import numpy as np
    import pandas as pd
    
    data = {'year': [*np.repeat(2012, 9), 2018],
            'month': [*np.repeat(1, 4), *np.repeat(2, 3), *np.repeat(3, 2), 12],
            'day': [1, 2, 3, 31, 1, 2, 28, 1, 2, 31],
            'Temp max': [28, 26, 27, 26, 27, 26, 26, 26, 25, 26],
            'Temp min': [19, 18, 17, 19, 18, 18, 18, 18, 18, 28]}
    
    df = pd.DataFrame(data)
    

    输出:结果数据帧

        year    month   day     Temp max    Temp min
    0   2012    1       1       28          19
    1   2012    1       2       26          18
    2   2012    1       3       7           17
    3   2012    1       31      26          19
    4   2012    2       1       27          18
    5   2012    2       2       26          18
    6   2012    2       28      26          18
    7   2012    3       1       26          18
    8   2012    3       2       25          18
    9   2018    12      31      26          28
    

    输入:创建数据透视表,计算“Temp max”列的最大值和“Temp min”列的最小值

    pivot = pd.pivot_table(data=df,
                           values=['Temp max', 'Temp min'],
                           index=['year', 'month'])
    
    pivot.columns = ['Monthly Temp max', 'Monthly Temp min']
    

    输出:结果数据帧

                    Monthly Temp max    Monthly Temp min
    year    month       
    2012    1       26.75               18.25
            2       26.33               18.00
            3       25.50               18.00
    2018    12      26.00               28.00
    

    或者:使用 pandas 的 groupby 方法

    grouped = (df
               .groupby(['year', 'month'])['Temp max', 'Temp min']
               .mean())
    
    grouped.columns = ['Monthly Temp max', 'Monthly Temp min']
    

    输出:结果数据帧

                    Monthly Temp max    Monthly Temp min
    year    month       
    2012    1       26.75               18.25
            2       26.33               18.00
            3       25.50               18.00
    2018    12      26.00               28.00
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-10-04
      • 2014-05-08
      • 2021-03-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多