2017-09-01 33 views
0

我有一个数据帧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 

criteriaDF:

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 

这给出了下面的错误:

 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循环以下运行

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) 

但是这不符合我的数据帧的索引适合在列[“月”]它运行1脚本作品 - 12不为0 - > 11

要确认我的理解是,

range (1, 13) 

回报

[1,2,3,4,5,6,7,8,9,10,11,12]. 

我也尝试手动运行代码行中的for循环与月= 12的代码行。所以我不确定为什么使用月在愤怒(1,13)不起作用,指出12是最高整数在列表范围(1,13)中。

我的代码或我的方法有什么错误?

回答

2

您正在使用iloc这是“纯粹基于整数位置的索引以供按位置选择”。所以它只是计数你的行从0到11 你应该使用loc看看你的指数的价值(所​​以1到12)