Я пытаюсь добавить «Ставку» в новый столбец в fills_df. fills_df имеет вкладку «Ликвидность», которая соответствует коду Liq в liq_rate_df, с которым связана ставка. Я хочу добавить ставки в fills_df, используя код Liq из liq_rate_df, который соответствует ликвидности в fills_df.
import pandas as pd
liq_rate_df = pd.read_excel(r'/content/gdrive/My Drive/fills_today_mh - 2023-2024 (1).xlsx', sheet_name='liq_rate')
fills_df = pd.read_excel(r'/content/gdrive/My Drive/fills_today_mh - 2023-2024 (1).xlsx', sheet_name='fills')
# Create a dictionary mapping of liquidity codes to rates
rate_mapping = dict(zip(liq_rate_df['Liq Code'], liq_rate_df['Rate']))
# Create a new column 'rate' in fills_df by mapping the 'liquidity_code' in fills_df to rate_mapping
fills_df['rate'] = fills_df['Liq Code'].map(rate_mapping)
Ошибка:
KeyError Traceback (most recent call last)
/usr/local/lib/python3.10/dist-packages/pandas/core/indexes/base.py in get_loc(self, key)
3790 try:
-> 3791 return self._engine.get_loc(casted_key)
3792 except KeyError as err:
index.pyx in pandas._libs.index.IndexEngine.get_loc()
index.pyx in pandas._libs.index.IndexEngine.get_loc()
pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()
pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()
KeyError: 'Liq Code'
The above exception was the direct cause of the following exception:
KeyError Traceback (most recent call last)
2 frames
/usr/local/lib/python3.10/dist-packages/pandas/core/indexes/base.py in get_loc(self, key)
3796 ):
3797 raise InvalidIndexError(key)
-> 3798 raise KeyError(key) from err
3799 except TypeError:
3800 # If we have a listlike key, _check_indexing_error will raise
KeyError: 'Liq Code'
Вывод из liq_rate_df.head():
Liq Code Rate Rate Type
0 A -0.0001 Per Share
1 B -0.0001 Notional
2 C 0.0010 Per Share
3 D 0.0002 Per Share
Подробнее здесь: https://stackoverflow.com/questions/790 ... s-into-one