Python pandas - 将一天的最后一小时 00 更改为 24
Python pandas - change last hour of a day 00 with 24
我目前正在设置 pandas 数据框。我有一个日期时间列,应该拆分为日期和时间列。
我已将此列拆分为日期 + 时间列,如下所示:
Date Hour
22 20200205 23
23 20200205 00
我想要的是将 00 值替换为 24。我知道 Python 不会将第 24 小时识别为最后一个小时,但出于我的目的,我需要包括“24”小时。
我是 python 和 pandas 的新手,我对替换这个值感到困惑。
我尝试使用下面的代码行但没有运气:
帧= OMP 数据
OMPdata['Hour'] = OMPdata['Hour'].str.replace(00,24, case= False)
或函数:
def h24 (row):
if row['Hour'] == "00":
return "24"
else:
return row['Hour']
请帮我解决这个问题。
非常感谢!
尝试先将列转换为字符串,然后替换:
df['Hour'] = df['Hour'].astype(str)
df['Hour'] = df['Hour'].str.replace("00","24", case= False)
我目前正在设置 pandas 数据框。我有一个日期时间列,应该拆分为日期和时间列。 我已将此列拆分为日期 + 时间列,如下所示:
Date Hour
22 20200205 23
23 20200205 00
我想要的是将 00 值替换为 24。我知道 Python 不会将第 24 小时识别为最后一个小时,但出于我的目的,我需要包括“24”小时。
我是 python 和 pandas 的新手,我对替换这个值感到困惑。 我尝试使用下面的代码行但没有运气: 帧= OMP 数据
OMPdata['Hour'] = OMPdata['Hour'].str.replace(00,24, case= False)
或函数:
def h24 (row):
if row['Hour'] == "00":
return "24"
else:
return row['Hour']
请帮我解决这个问题。
非常感谢!
尝试先将列转换为字符串,然后替换:
df['Hour'] = df['Hour'].astype(str)
df['Hour'] = df['Hour'].str.replace("00","24", case= False)