我试图修改一列的值(如果它们介于以下时间之间(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
python参考方案
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
R'relaimpo'软件包的Python端口 - python我需要计算Lindeman-Merenda-Gold(LMG)分数,以进行回归分析。我发现R语言的relaimpo包下有该文件。不幸的是,我对R没有任何经验。我检查了互联网,但找不到。这个程序包有python端口吗?如果不存在,是否可以通过python使用该包? python参考方案 最近,我遇到了pingouin库。
Python:如何根据另一列元素明智地查找一列中的空单元格计数? - pythondf = pd.DataFrame({'user': ['Bob', 'Jane', 'Alice','Jane', 'Alice','Bob', 'Alice'], 'income…
Python pytz时区函数返回的时区为9分钟 - python由于某些原因,我无法从以下代码中找出原因:>>> from pytz import timezone >>> timezone('America/Chicago') 我得到:<DstTzInfo 'America/Chicago' LMT-1 day, 18:09:00 STD…
将字符串分配给numpy.zeros数组[重复] - pythonThis question already has answers here: Weird behaviour initializing a numpy array of string data (4个答案) …
用大写字母拆分字符串,但忽略AAA Python Regex - python我的正则表达式:vendor = "MyNameIsJoe. I'mWorkerInAAAinc." ven = re.split(r'(?<=[a-z])[A-Z]|[A-Z](?=[a-z])', vendor) 以大写字母分割字符串,例如:'我的名字是乔。 I'mWorkerInAAAinc”变成…