bra*_*hes 1 python indexing loops pandas
我有一个数据框 myDF,我希望使用其他列的条件组合将其中的一列设置为零,并使用第二个数据框 CriteriaDF 进行索引。
myDF.head():
DateTime GrossPowerMW USDateTime_string DateTime_timestamp \
0 01/01/1998 00:00 17.804 01/01/1998 00:00 1998-01-01 00:00:00
1 01/01/1998 01:00 18.751 01/01/1998 01:00 1998-01-01 01:00:00
2 01/01/1998 02:00 20.501 01/01/1998 02:00 1998-01-01 02:00:00
3 01/01/1998 03:00 22.222 01/01/1998 03:00 1998-01-01 03:00:00
4 01/01/1998 04:00 24.437 01/01/1998 04:00 1998-01-01 04:00:00
Month Day Hour GrossPowerMW_Shutdown
0 1 3 0 17.804
1 1 3 1 18.751
2 1 3 2 20.501
3 1 3 3 22.222
4 1 3 4 24.437
Run Code Online (Sandbox Code Playgroud)
标准DF:
STARTTIME ENDTIME
Month
1 9.0 12.0
2 9.0 14.0
3 9.0 14.0
4 9.0 14.0
5 9.0 13.0
6 9.0 14.0
7 9.0 13.0
8 9.0 12.0
9 9.0 14.0
10 9.0 13.0
11 9.0 13.0
12 9.0 11.0
Run Code Online (Sandbox Code Playgroud)
然后通过以下 for 循环运行 myDF:
month = 1
for month in range (1, 13):
shutdown_hours = range(int(criteriaDF.iloc[month]['STARTTIME']), int(criteriaDF.iloc[month]['ENDTIME']))
myDF.loc[(myDF["Month"].isin([month])) & (myDF["Hour"].isin(shutdown_hours)) & (myDF["Day"].isin(shutdown_days)), "GrossPowerMW_Shutdown"] *= 0
month = month + 1
Run Code Online (Sandbox Code Playgroud)
这给出了以下错误:
回溯(最近一次调用最后一次):
文件 "", line 1, in runfile('myscript.py', wdir='C:myscript')
文件“C:\ProgramData\Anaconda2\lib\site-packages\spyder\utils\site\sitecustomize.py”,第 880 行,在运行文件 execfile(filename, namespace) 中
文件“C:\ProgramData\Anaconda2\lib\site-packages\spyder\utils\site\sitecustomize.py”,第 87 行,在 execfile exec(compile(scripttext, filename, 'exec'), glob, loc)
文件“myscript.py”,第 111 行,在gross_yield、curtailed_yield、shutdown_loss、df_testing = calculate_loss(input_file, input_shutdownbymonth, shutdown_days) #Returning df 仅用于测试/询问。完成后删除。
文件“myscript.py”,第 79 行,在calculate_loss shutdown_hours = range(int(criteriaDF.iloc[month]['STARTTIME']), int(criteriaDF.iloc[month]['ENDTIME']))
文件“C:\ProgramData\Anaconda2\lib\site-packages\pandas\core\indexing.py”,第 1328 行,在 __getitem__ 中返回 self._getitem_axis(key,axis=0)
文件“C:\ProgramData\Anaconda2\lib\site-packages\pandas\core\indexing.py”,第 1749 行,在 _getitem_axis self._is_valid_integer(key, axis)
文件“C:\ProgramData\Anaconda2\lib\site-packages\pandas\core\indexing.py”,第 1638 行,在 _is_valid_integer 中引发 IndexError(“单个位置索引器越界”)
IndexError:单个位置索引器越界
但是,如果我设置了脚本,则该脚本有效
month = 0
for month in range (0, 12)
Run Code Online (Sandbox Code Playgroud)
但是,这不符合我的数据框在 Column ['Month'] 上的索引,它运行 1 - 12 而不是 0 -> 11。
确认我的理解是
range (1, 13)
Run Code Online (Sandbox Code Playgroud)
返回
[1,2,3,4,5,6,7,8,9,10,11,12].
Run Code Online (Sandbox Code Playgroud)
我也试过手动运行代码和 for 循环中的代码,月 = 12。所以我不确定为什么在愤怒中使用月 (1, 13) 不起作用,注意到 12 是最大的整数列表范围 (1,13)。
我的代码或我的方法有什么错误?