我有一个数据框 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
标准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
myDF 然后通过以下 for 循环运行:
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
这给出了以下错误:
Traceback (most recent call last):
File "", line 1, in runfile('myscript.py', wdir='C:myscript')
File "C:\ProgramData\Anaconda2\lib\site-packages\spyder\utils\site\sitecustomize.py", line 880, in runfile execfile(filename, namespace)
File "C:\ProgramData\Anaconda2\lib\site-packages\spyder\utils\site\sitecustomize.py", line 87, in execfile exec(compile(scripttext, filename, 'exec'), glob, loc)
File "myscript.py", line 111, in gross_yield, curtailed_yield, shutdown_loss, df_testing = calculate_loss(input_file, input_shutdownbymonth, shutdown_days) #Returning df for testing/interrogation only. Delete once finished.
File "myscript.py", line 79, in calculate_loss shutdown_hours = range(int(criteriaDF.iloc[month]['STARTTIME']), int(criteriaDF.iloc[month]['ENDTIME']))
File "C:\ProgramData\Anaconda2\lib\site-packages\pandas\core\indexing.py", line 1328, in __getitem__ return self._getitem_axis(key, axis=0)
File "C:\ProgramData\Anaconda2\lib\site-packages\pandas\core\indexing.py", line 1749, in _getitem_axis self._is_valid_integer(key, axis)
File "C:\ProgramData\Anaconda2\lib\site-packages\pandas\core\indexing.py", line 1638, in _is_valid_integer raise IndexError("single positional indexer is out-of-bounds")
IndexError: single positional indexer is out-of-bounds
但是如果我设置脚本就可以运行
month = 0
for month in range (0, 12)
但是,这不符合我的数据框在列 ['Month'] 上的索引,它运行 1 - 12 而不是 0 -> 11。
确认我的理解是
range (1, 13)
返回
[1,2,3,4,5,6,7,8,9,10,11,12].
我也尝试过手动逐行运行代码,其中 for 循环中的代码带有 month = 12。所以我不确定为什么使用 month in rage (1, 13) 不起作用,注意到 12 是最高的列表范围 (1,13) 中的整数。
我的代码或方法有什么错误?
最佳答案
关于python - 单位置索引器越界迭代 Pandas 数据框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45997553/