Я пишу скрипт на python, который зацикливает две таблицы друг на друга. Если условие выполнено, скрипт выполнит обновление зависимой таблицы.
Мой код на Python:
def updatedata():
for y in range(updatetable.shape[0]):
for x in range(mastertable.shape[0]):
if updatetable[y].s_date <= mastertable[x].index <= updatetable[y].e_date:
mastertable[x].field2 = updatetable[y]. field2
mastertable[y].field3 = updatetable[y]. field3
У меня также есть этот метод итерации:
for index, row in mastertable.iterrows():
print (row['Value'], index)
for index, row in updatetable.iterrows():
print (row['field1'], row['field2'])
Я слежу за тем, как написать это в VBA:
For x = 1 to lastrow_update
for y = 1 to lastrow_master
if update(x,1) <= master(y,1) and master(y,1) <= update(x,2) then
master (y,2) = update(x,3)
Я получаю ошибки с кодом Python.
1) как мне создать две управляющие переменные для цикла "for"
2) как мне выйти из внутреннего цикла после матча, чтобы сократить время выполнения
ошибка для def updatedata ()
Traceback (most recent call last):
File "/anaconda3/lib/python3.6/site-packages/pandas/core/indexes/base.py", line 2525, in get_loc
return self._engine.get_loc(key)
File "pandas/_libs/index.pyx", line 117, in pandas._libs.index.IndexEngine.get_loc
File "pandas/_libs/index.pyx", line 139, in pandas._libs.index.IndexEngine.get_loc
File "pandas/_libs/hashtable_class_helper.pxi", line 1265, in pandas._libs.hashtable.PyObjectHashTable.get_item
File "pandas/_libs/hashtable_class_helper.pxi", line 1273, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: 0
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/Users/user1/Desktop/project4.py", line 41, in <module>
updatedata()
File "/Users/user1/Desktop/project4.py", line 20, in updatedata
if presidents_data[y].tookoffice <= sp500[x].index <= presidents_data[y].leftoffice:
File "/anaconda3/lib/python3.6/site-packages/pandas/core/frame.py", line 2139, in __getitem__
return self._getitem_column(key)
File "/anaconda3/lib/python3.6/site-packages/pandas/core/frame.py", line 2146, in _getitem_column
return self._get_item_cache(key)
File "/anaconda3/lib/python3.6/site-packages/pandas/core/generic.py", line 1842, in _get_item_cache
values = self._data.get(item)
File "/anaconda3/lib/python3.6/site-packages/pandas/core/internals.py", line 3843, in get
loc = self.items.get_loc(item)
File "/anaconda3/lib/python3.6/site-packages/pandas/core/indexes/base.py", line 2527, in get_loc
return self._engine.get_loc(self._maybe_cast_indexer(key))
File "pandas/_libs/index.pyx", line 117, in pandas._libs.index.IndexEngine.get_loc
File "pandas/_libs/index.pyx", line 139, in pandas._libs.index.IndexEngine.get_loc
File "pandas/_libs/hashtable_class_helper.pxi", line 1265, in pandas._libs.hashtable.PyObjectHashTable.get_item
File "pandas/_libs/hashtable_class_helper.pxi", line 1273, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: 0
образец данных:
president tookoffice leftoffice party
0 Lyndon B. Johnson 1963-11-22 1969-01-20 Democratic
1 Franklin D. Roosevelt 1933-03-04 1945-04-12 Democratic
2 Herbert Hoover 1929-03-04 1933-03-04 Republican
3 Warren G. Harding 1921-03-04 1923-08-02 Republican
4 Barack Obama 2009-01-20 2017-01-20 Democratic
Value president party_of_president
Date
1871-01-01 4.44 president party_of_president
1871-02-01 4.50 president party_of_president
1871-03-01 4.61 president party_of_president
1871-04-01 4.74 president party_of_president
1871-05-01 4.86 president party_of_president