在时间范围内更改列的值

时间:2018-11-08 13:47:04

标签: python pandas datetime dataframe indexing

如果一栏的值介于以下时间(17:00至23:00)之间,我将尝试对其进行修改,否则,它们必须保持相同的值。这是我的代码:

lclstd['Response KWH/hh (per half hour) ']=lclstd['KWH/hh (per half hour) '].astype(float)

从日期时间列中提取时间

lclstd['Time']=pd.to_datetime(lclstd['DateTime']).dt.strftime ('%H:%M:%S')

仅在17:00至23:00的高峰时段分配响应

lclstd.loc[(lclstd['Time'] == '17:00:00') | (lclstd['Time'] == '17:30:00') | (lclstd['Time'] == '18:00:00') | (lclstd['Time'] == '18:30:00') | (lclstd['Time'] == '19:00:00') | (lclstd['Time'] == '19:30:00') | (lclstd['Time'] == '20:00:00') | (lclstd['Time'] == '20:30:00') | (lclstd['Time'] == '21:00:00') | (lclstd['Time'] == '21:30:00') | (lclstd['Time'] == '22:00:00') | (lclstd['Time'] == '22:30:00') | (lclstd['Time'] == '23:00:00') , 'Response KWH/hh (per half hour) '] = 0.9*lclstd['Response KWH/hh (per half hour) ']

但出现以下错误

ValueError: cannot reindex from a duplicate axis

1 个答案:

答案 0 :(得分:0)

pd.Series.dt.time返回datetime.time个对象。因此,您可以通过pd.Series.between进行比较以创建遮罩。然后输入loc

from datetime import time

time1, time2 = time(17), time(23)                # 17:00, 23:00

df['Time'] = pd.to_datetime(df['Time'])          # convert to datetime if necessary
mask = df['Time'].dt.time.between(time1, time2)  # inclusive of boundaries by default

df.loc[mask, 'Response KWH/hh (per half hour) '] *= 0.9