【问题标题】:Python: Determining period of the day based on hour using a for loop and conditionalsPython:使用 for 循环和条件根据小时确定一天中的时段
【发布时间】:2021-10-06 17:08:05
【问题描述】:

我想根据每小时信息为我的数据框命名一天中的时段。

为此,我正在尝试以下方法:

day_period = []

for index,row in df.iterrows():
        
    hour_series = row["hour"]
    
    # Morning = 04:00-10:00
    #if hour_series >= 4 and hour_series < 10:
    if 4 >= hour_series < 10:
        day_period_str = "Morning"
        day_period.append(day_period_str)
    
    # Day = 10:00-16:00
    #if hour_series >= 10 and hour_series < 16:
    if 10 >= hour_series < 16:
        day_period_str = "Day"
        day_period.append(day_period_str)
        
    # Evening = 16:00-22:00
    #if hour_series >= 16 and hour_series < 22:
    if 16 >= hour_series < 22:
        day_period_str = "Evening"
        day_period.append(day_period_str)
        
    # Night = 22:00-04:00
    #if hour_series >= 22 and hour_series < 4:
    if 22 >= hour_series < 4:
        day_period_str = "Night"
        day_period.append(day_period_str)

但是,当仔细检查我的 day_period 列表的长度是否与我的数据框 (df) 的长度相同时......它们不同,它们不应该。我无法发现错误。如何修复代码?

len(day_period)
>21882

len(df)
>25696

以下是数据预览:

    timestamp               latitude    longitude   hour    weekday
0   2021-06-09 08:12:18.000 57.728867   11.949463   8   Wednesday
1   2021-06-09 08:12:18.000 57.728954   11.949368   8   Wednesday
2   2021-06-09 08:12:18.587 57.728867   11.949463   8   Wednesday
3   2021-06-09 08:12:18.716 57.728954   11.949368   8   Wednesday
4   2021-06-09 08:12:33.000 57.728905   11.949309   8   Wednesday

我的最终目标是将此列表附加到数据框。

【问题讨论】:

  • 如果您的目标是向数据框中添加一列,那么您真的应该使用 pandas 特定的方式,请参阅my answer。手动循环遍历行通常效率低下,被认为是一种不好的做法。

标签: python pandas datetime for-loop if-statement


【解决方案1】:

经过一番测试,问题似乎是 22-4 块,将它们分开可以解决此问题。

另外,我将&gt;= 更改为&lt;=

使用此代码,它可以按预期工作:

day_period = []


for index,row in df.iterrows():
    hour_series = row["hour"]

    # Night 1 = 00:00-04:00
    #if hour_series <= 0 and hour_series < 4:
    if 0 <= hour_series < 4:
        day_period_str = "Night"
        day_period.append(day_period_str)

    # Morning = 04:00-10:00
    #if hour_series <= 4 and hour_series < 10:
    elif 4 <= hour_series < 10:
        day_period_str = "Morning"
        day_period.append(day_period_str)
    
    # Day = 10:00-16:00
    #if hour_series <= 10 and hour_series < 16:
    elif 10 <= hour_series < 16:
        day_period_str = "Day"
        day_period.append(day_period_str)
        
    # Evening = 16:00-22:00
    #if hour_series <= 16 and hour_series < 22:
    elif 16 <= hour_series < 22:
        day_period_str = "Evening"
        day_period.append(day_period_str)
        
    # Night 2 = 22:00-24:00
    #if hour_series <= 22 and hour_series < 24:

    elif 22 <= hour_series < 24:
        day_period_str = "Night"
        day_period.append(day_period_str)

print(len(all_rows))
print(len(day_period)) # they should match now

【讨论】:

    【解决方案2】:

    当你使用pandas时,你应该使用pandas.cut

    输入:

    pd.DataFrame({'date': pd.date_range('2021-07-31', '2021-08-01', freq='2h')})
    
    ranges = {4: 'Night', 10: 'Morning', 16: 'Day', 22: 'Evening', 24: 'Night'}
    
    df['time_of_day'] = pd.cut(df['date'].dt.hour,
                               bins=[-1]+list(ranges),
                               labels=ranges.values(),
                               right=False,
                               ordered=False, # because label "Night" is duplicated
                              )
    

    输出:

                      date time_of_day
    0  2021-07-31 00:00:00       Night
    1  2021-07-31 02:00:00       Night
    2  2021-07-31 04:00:00     Morning
    3  2021-07-31 06:00:00     Morning
    4  2021-07-31 08:00:00     Morning
    5  2021-07-31 10:00:00         Day
    6  2021-07-31 12:00:00         Day
    7  2021-07-31 14:00:00         Day
    8  2021-07-31 16:00:00     Evening
    9  2021-07-31 18:00:00     Evening
    10 2021-07-31 20:00:00     Evening
    11 2021-07-31 22:00:00       Night
    12 2021-08-01 00:00:00       Night
    

    【讨论】:

      【解决方案3】:

      您的小于/大于不正确:

      要找到你想要的 4 到 10 个:

      if 4 <= hour_series < 10:
      

      【讨论】:

      • 是的,我在发帖后注意到了这个愚蠢的错误。我最初是以评论的方式进行的(这也不起作用)并且忘记了交换标志。
      猜你喜欢
      • 2017-12-13
      • 1970-01-01
      • 2022-01-01
      • 2022-06-22
      • 2016-02-21
      • 2017-05-04
      • 2019-11-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多