2017-09-01 32 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)