【问题标题】:Datetime to seasons日期时间到季节
【发布时间】:2021-07-25 22:31:51
【问题描述】:

我有一个示例数据:

datetime.            column1

2021-04-01 01:00.     11
2021-04-05 02:00.     10
2021-04-12 03:00.      1
2021-04-11 04:00.      5
2021-04-07 05:00.      20

我想创建一个名为 season 的新列,如果日期时间值介于 5 月和 9 月之间,则输出冷却季节,如果日期时间值介于 10 月和 4 月之间,则输出采暖季节。示例输出应如下所示:

datetime.            column1.  seasons

2021-01-01 01:00.     11.       Heating season
2021-05-01 02:00.     10.       Cooling season
2021-12-01 03:00.      1.       Heating season
2021-11-01 04:00.      5.       Heating season
2021-07-01 05:00.      20.      Cooling season

【问题讨论】:

    标签: python pandas numpy data-science


    【解决方案1】:

    有很多方法,

    df['datetime'] = pd.to_datetime(df.datetime)
    

    使用 np.select 和几个月的特定条件

    conditions = [
        df.datetime.dt.month.isin(np.arange(5,10)),
        (df.datetime.dt.month.isin(np.arange(1,5))) | (df.datetime.dt.month.isin(np.arange(10,13))),
    ]
    choices = ['Cooling season','Heating season']
    df['seasons'] = np.select(conditions, choices)
    df
    

    使用带有默认值的 np.select

    conditions = [
        df.datetime.dt.month.isin(np.arange(5,10)),
    ]
    choices = ['Cooling season']
    df['seasons'] = np.select(conditions, choices, default='Heating season')
    df
    

    使用 np.where

    df['seasons'] = np.where(df.datetime.dt.month.isin(np.arange(5,10)), 'Cooling season','Heating season')
    df
    

    输出

        datetime            column1     seasons
    0   2021-01-01 01:00:00 11      Heating season
    1   2021-05-01 02:00:00 10      Cooling season
    2   2021-12-01 03:00:00 1       Heating season
    3   2021-11-01 04:00:00 5       Heating season
    4   2021-07-01 05:00:00 20      Cooling season
    

    【讨论】:

      【解决方案2】:
      df["seasons"] = np.where((df["datetime"].dt.month >= 5) 
                                & (df["datetime"].dt.month <= 9),
                               "Cooling season", "Heating season")
      
      >>> df
                   datetime  column1         seasons
      0 2021-01-01 01:00:00       11  Heating season
      1 2021-05-05 02:00:00       10  Cooling season
      2 2021-12-12 03:00:00        1  Heating season
      3 2021-11-11 04:00:00        5  Heating season
      4 2021-07-07 05:00:00       20  Cooling season
      

      【讨论】:

      • 只是一个想法,使用 & 条件迭代 datetime 列两次可能会减慢速度。
      猜你喜欢
      • 2017-10-22
      • 1970-01-01
      • 2020-06-02
      • 2021-08-06
      • 2014-09-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多